#!/bin/bash  


checkStatus()
{
	status=$1
	msg=$2
	if [ $status -ne 0 ]
	then
        echo -e "\033[1;31m $msg \033[0m"        
        exit $status
	fi
}

CURDIR=`pwd`

InstallLib()
{
	LIBDIR=$1
	LIBNAME=$2

	echo  -e "\033[1;34m===================================================================\033[0m"
	echo -e "\033[1;34m Installing $LIBNAME \033[0m"
	echo  -e "\033[1;34m===================================================================\033[0m"
	
	cd $CURDIR
	
	cd $LIBDIR
	echo -e "\033[1;34m Changed directory to `pwd`\033[0m"
	sleep 0.1
	
	./configure --prefix=$CURDIR
	status=`echo $?`
	checkStatus $status "***Error in configuring $LIBNAME library ***"
	echo -e "\033[1;34m--------------Completed configuring $LIBNAME library--------------\033[0m"
	sleep 0.1
	
	make clean
	make
	status=`echo $?`
	checkStatus $status "***Error in building $LIBNAME library***"
	echo  -e "\033[1;34m--------------Completed building $LIBNAME library------------------\033[0m"
	sleep 0.1

	make install
	status=`echo $?`
	checkStatus $status "***Error in installing $LIBNAME library***"

    make clean
	echo  -e "\033[1;34m-------------Completed installing $LIBNAME library-----------------\033[0m"
	sleep 0.1
}

if [ "$#" -eq 0 ]; then
    dospice=0
    dolegacy=0
fi 

if [ "$#" -eq 1 ]; then
    if [ $1 = legacy ]; then 
        dolegacy=1
        dospice=0
    else
        if [ $1 -eq 1 ]; then                
            dospice=1
            dolegacy=0
        fi    
    fi    
fi    

if [ "$#" -eq 2 ]; then
    if [ $1 = legacy ]; then
        dolegacy=1
    fi
    if [ $2 -eq 1 ]; then
        dospice=1
    fi 
fi

if [ $dolegacy -eq 1 ]; then 
    InstallLib libsrc/cfitsio_Legacy "CFITSIO"
else
    InstallLib libsrc/cfitsio "CFITSIO"
fi    

cd $CURDIR	

rm $CURDIR/bin/cookbook $CURDIR/bin/fitscopy $CURDIR/bin/fitsverify $CURDIR/bin/fpack $CURDIR/bin/funpack $CURDIR/bin/imcopy $CURDIR/bin/smem $CURDIR/bin/speed


echo  -e "\033[1;34m===================================================================\033[0m"
echo -e "\033[1;34m Installing PIL \033[0m"
echo  -e "\033[1;34m===================================================================\033[0m"

make -C libsrc/pil clean;make -C libsrc/pil
make -C libsrc/pil clean;
echo  -e "\033[1;34m-------------Completed installing PIL library-----------------\033[0m"


if [ $dospice -eq 1 ]; then

echo  -e "\033[1;34m===================================================================\033[0m"
echo -e "\033[1;34m Verifying CSPICE Library \033[0m"
echo  -e "\033[1;34m===================================================================\033[0m"

cd libsrc/cspice

make all

status=`echo $?`
checkStatus $status "
***Error with precompiled CSPICE library files.****  

Install CSPICE library from https://naif.jpl.nasa.gov/naif/toolkit_C.html 

and copy cspice.a and csupport.a to $xsmdas/lib

Re-run this script to ensure that they are working"

make clean

cd $CURDIR

echo  -e "\033[1;34m-------------CSPICE library is working fine-----------------\033[0m"

fi

echo  -e "\033[1;34m------------- Installing LIBS  completed successfully-----------------\033[0m"

if [ $dospice -eq 1 ]; then

echo  -e "\033[1;34m--------------Setting up path in SPICE Meta-Kernel file ----------------\033[0m"

echo "Kernel File is $xsmdas/caldb/xsm_metakernel.ker"

REP="PATH_VALUES = ('$xsmdas/caldb/kernels/')"
sed -i "s|.*PATH_VALUES.*|$REP|" $xsmdas/caldb/xsm_metakernel.ker
status=`echo $?`
checkStatus $status "
***Error: Unable to set PATH_VALUES in SPICE Meta kernel file ***

In file $xsmdas/caldb/xsm_metakernel.ker, manually CHANGE the line

PATH_VALUES = ('path_to_caldb') 

by replacing path_to_caldb with full path to caldb which is:

$xsmdas/caldb/kernels/

There is no need to re-run this script. Continue with next step 
of installation.
"

echo  -e "\033[1;34m---------Setting up path in SPICE Meta-Kernel file successful -----------\033[0m"

fi
