#!/bin/bash

function usage
{
    # Temporary function stub
    echo "USAGE: ./install --install_path <directory where CompuCell is installed> --source_path <directory with CompuCell sources>"
    echo "USAGE: ./install -i <directory where CompuCell is installed> -s <directory with CompuCell sources>"    
}


export tmp_file=src/tmp
export pro_file=src/src.pro
export pro_file_tmp=src/src_tmp.pro
export pro_file_orig=src/src.pro.orig

touch tmp_file


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"
                                ;;
        -s | --source_path )    shift
				compucellnew_source_path=$1
				echo "compucellnew_source_path: $compucellnew_source_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

#get xercesc.config veriables
CWD=${PWD}
cd ../extralibs/

config_file=${PWD}/"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"


echo "compucellnew_source_path=$compucellnew_source_path" > $tmp_file
echo "install_path=$install_path" >> $tmp_file
echo "xercesc_lib_dir=$XERCESC_LIB_DIR">> $tmp_file
echo "xercesc_include_dir=$XERCESC_INCLUDE_DIR">> $tmp_file

if [ ! -e $pro_file_orig ]
then
    cp $pro_file $pro_file_orig
fi    

cat $tmp_file $pro_file_orig >$pro_file
#mv $pro_file_tmp $pro_file
rm $tmp_file 

if qmake CompuCellPlayer.pro; then
    echo "qmake call successful"
else
    echo "problems with qmake. Exiting..."
    exit 1    
fi

if make; then
    echo
    echo "CompuCellPlayer Sucessfully installed!"
    echo
    touch success.lock
else
    echo
    echo "Errors during compilation"
    exit 1
fi