#!/bin/bash

function usage
{
    # Temporary function stub
    echo "USAGE: ./prepare_run_scritp.sh --install_path <directory where CompuCell will be installed>"
    echo "or"
    echo "USAGE: ./prepare_run_scritp.sh -i <directory where CompuCell will be installed>"
}


export install_path
#export compucellnew_source_path


while [ "$1" != "" ]; do
    case $1 in
        -i | --install_path )           shift
                                install_path=$1
                                echo "install_path: $install_path"
                                ;;
        -h | --help )           usage
                                exit
                                ;;
        * )                     usage
                                exit 1
    esac
    shift
done



echo "Installation path: $install_path"



if [ "$install_path" ]
then
    echo "Install Path = $install_path"
else
    install_path="/usr/local/CompuCell3D"
    echo "WARNING: USING DEFEULT INSTALL PATH! = $install_path"
    echo "In case of installation problems re-run this script with specified instalation path e.g. ./install /home/yourloginname/CompuCell3D"
    echo "OK to continue?(y/n)"
    read answer
    if [ $answer = 'y' ]
    then
        echo "Proceeding..."
    else
        echo "Please specify installation path"
        exit
    fi

fi

#if [ "$compucellnew_source_path" ]
#then
#    echo "CompuCellNew source path: $compucellnew_source_path"
#else
#    compucellnew_source_path="../"
#    echo "Using default path to COmpuCellNew source: $compucellnew_source_path"
#    echo "In case of installation problems try specyfying full path to CompuCellNew sources manually "
#    echo "This is the path to directory THAT CONTAINS  CompuCellNew subrirectory"
#    echo "It should be fine to continue. OK to continue?(y/n)"
#    read answer
#    if [ $answer = 'y' ]
#    then
#        echo "Proceeding..."
#    else
#        echo "Please specify path to CompuCellNew source directory"
#        echo "This is the path to directory THAT CONTAINS  CompuCellNew subrirectory"
#        exit
#    fi
#
#fi


#Here I will read xerces configuration variables that xercesutils came up with.
#CWD=${PWD}
#cd ../extralibs/

config_file=${PWD}/extralibs/"xercesc.config"

#cd $CWD

line=""

while test 1 == 1;
do
    read line || break
#    echo "$line"
    eval "$line"
done < $config_file

echo "XERCESC_LIB_DIR=$XERCESC_LIB_DIR"
echo "XERCESC_LIBS=$XERCESC_LIBS"
echo "XERCESC_INCLUDE=$XERCESC_INCLUDE"
echo "XERCESC_INCLUDE_DIR=$XERCESC_INCLUDE_DIR"
echo "XERCESC_VER=$XERCESC_VER"
echo "XERCESCROOT=$XERCESCROOT"



cd $install_path
export run_script=run_CompuCellNew.sh

touch $run_script 

echo "export PREFIX_CC3D=${install_path}" >$run_script 
#echo "export LIB_PREFIX=${compucellnew_source_path}/CompuCellNew" >> $run_script 

echo "export LIB_PREFIX=${install_path}/extra" >> $run_script 

#echo "export LD_LIBRARY_PATH=\${LIB_PREFIX}/xerces/lib:\$LD_LIBRARY_PATH">> $run_script 
echo "export LD_LIBRARY_PATH=\${LIB_PREFIX}/lib:\$LD_LIBRARY_PATH">> $run_script 
#xercesc lib path
if test $XERCESC_LIB_DIR} != ""; then
echo "export LD_LIBRARY_PATH=$XERCESC_LIB_DIR:\$LD_LIBRARY_PATH">> $run_script 
fi
echo "export LD_LIBRARY_PATH=\${PREFIX_CC3D}/lib/:\$LD_LIBRARY_PATH">> $run_script 
echo "export COMPUCELL3D_PLUGIN_PATH=\${PREFIX_CC3D}/lib">> $run_script 
echo "export PATH=\${PREFIX_CC3D}/bin:\${PATH}">> $run_script 

#echo "export LD_LIBRARY_PATH=/home/mswat/programs/lib:\$LD_LIBRARY_PATH" >>$run_script

echo $install_path/bin/CompuCellPlayer >> $run_script

chmod +x $install_path/$run_script