#! /bin/sh

cd `dirname $0`

# The clean script removes temporary and junk files that should not be
# committed to the source code repo.
if (test -e clean)
then
    ./clean
fi

if [ "$1" = "--skip-types" ]; then
    shift;
    dont_skip_types=0
else
    dont_skip_types=1
fi


rm -f install.log

st=0

# CRDS type specifications are configured in code as individual files in the
# project "specs" subdirectories.  This step creates combined versions of the
# specs files for faster loading at runtime by first removing the existing
# combined spec and then invoking the project package.  Importing the project
# packages triggers loading type specs which will automatically rebuild the
# (intentionally removed) combined .json file for faster loading in the future.
# Once the combined .json spec has been generated, it is committed and
# installed as code by setup.py and it will be loaded in preference to the
# individual specs to reduce impact on the file system.  Unless type specifications
# are being modified,  regenerating the comined_specs.json files should have
# no effect.   When types are added or modified,  the combined_specs.json file
# will also automatically be modified during ./install,  and the changes should be
# both committed to GitHub and later installed normally as source code by setup.py.
# (setup.py does not regenerate these combined spec files,  it is assumed that
# type developers will execute this ./install script at least once after adding
# or changing CRDS type specs,  after which the combined spec file is effectively
# source code.

if [ $dont_skip_types = 1 ]; then
    find . -name combined_specs.json | xargs rm

    find crds/hst/specs crds/jwst/specs crds/tobs/specs -name '*.rmap' | xargs python -m crds.refactoring.checksum
    
    python -m crds.hst
    python -m crds.jwst
    python -m crds.tobs
fi

python setup.py install $* --force  >> install.log
st=$st$?

# Optional CRDS test data is only installed by developers using ./install
# to install CRDS from source code.  To support testing the 
# setup_test_cache script should also be run to install other test files.
python setup_data.py install --force >>install.log
st=$st$?

if (test -e clean)
then
    ./clean
fi

echo final status $st
st=`echo $st | tr -d 0 | cut -c1-1`
exit $st
