#!/bin/bash

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

export install_path


#command line parsing
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 $install_path

if [ -d $install_path ]
then
echo "Directory $install_path already exists"
else
mkdir $install_path
fi

if [ -d $install_path/extra ]
then
echo "Directory $install_path/extra already exists"
else
mkdir $install_path/extra
fi

if [ -d $install_path/extra/lib ]
then
echo "Directory $install_path/extra/lib already exists"
else
mkdir $install_path/extra/lib
fi

#cp -r xerces/lib/* $install_path/extra/lib
cp -r lib/* $install_path/extra/lib

CWD=${PWD}
export CWD
#export xercesc="${CWD}/xerces" 
export binutils="${CWD}"
#export xercesc="$install_path/extra/xerces"
#export binutils="$install_path/extra"
echo $xercesc
echo $binutils


#running automake & friends
echo ""
echo "RUNNING AUTOMAKE & FRIENDS. PLEASE WAIT..."
echo ""
libtoolize --force --copy --automake
aclocal
autoheader
autoconf
automake --copy --add-missing
echo ""

#export LIB_PREFIX=/home/mswat/programs



if [ "$install_path" ] 
then
    echo "Install Path = $install_path"    
else
    install_path="/usr/local/CompuCell3D"    
    echo "In case of installation problems re-run this script with specified instalation path e.g. ./install /home/yourloginname/CompuCell3D"
    echo "WARNING: USING DEFEULT INSTALL PATH! = $install_path"    
fi

echo "RUNNING CONFIGURE. PLEASE WAIT..."
echo ""

./configure --prefix=${install_path} --with-basicutils=${binutils} --disable-libtool-lock 
cd src

echo "COMPILING. PLEASE WAIT..."
echo ""

if make; then
    make install
else
    echo "THERE WERE ERRORS DURING COMPILATION!"
    exit 1
fi
echo
echo "SUCCESS! CompuCellNew was sucessfully installed!"
echo "Now install CompuCellPlayer"
echo
touch success.lock
#make
#make install

#make clean
