#!/bin/sh



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


CWD=${PWD}
cd ..
top_dir=${PWD}

cd ${CWD}


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


export BasicUtils_CONFIG=${top_dir}/basicutils/basicutils_install/bin/BasicUtils-config

TOP_XERCES_UTIL_DIR=${PWD}
libtoolize --force --copy --automake
aclocal -I ${TOP_XERCES_UTIL_DIR}/config/m4 #tells where additional m4 macros are stored locally
autoheader
autoconf
automake --copy --add-missing

install_path=${top_dir}/xercesutils/xercesutils_install

#./configure --prefix=${install_path} --with-xercesc=${top_dir}/xercesc

#exit 1

./configure --prefix=${install_path} --disable-libtool-lock

configure_exit_value=$? #exit status from last command


echo "configure_exit_value = $configure_exit_value"



if test $configure_exit_value == 2; then
    echo
    echo
    echo "COULD NOT FIND CORRECT VERSION OF XERCESC . WILL TRY TO RECONFIGURE WITH PRECPOMPILED XERCESC"
    ./configure --prefix=${install_path} --with-xercesc=${top_dir}/xercesc
fi

if test $configure_exit_value == 3; then
    echo
    echo	
    echo "COULD NOT FIND  XERCESC AT ALL. WILL TRY TO RECONFIGURE WITH PRECPOMPILED XERCESC"
    ./configure --prefix=${install_path} --with-xercesc=${top_dir}/xercesc
fi



echo 
echo
echo "install_path=${install_path}"



if make; then

    if make install ; then
	echo "INSTALLATION OF XERCESUTILS SUCCESSFUL"
    else
	echo "PROBLEMS WITH INSTALLATION"
	echo "Check if you have permission to ${install_path} directory"
	exit 1
    fi
		
else
    echo "THERE WERE ERRORS DURING COMPILATION!"
    exit 1
fi

