#!/bin/bash
#
# source this file to setup orac environment
#

OLD_PATH=$PATH
orac_home=`pwd | sed "s?/etc??g"`
bin=$orac_home/bin
prima=$orac_home/tools/primadorac
prima_src=$orac_home/tools/primadorac/src
www=$orac_home/tools/primadorac/www
scripts=$orac_home/tools/scripts 
export PATH=$bin:$prima:$scripts:$prima_src:$OLD_PATH:

if [ ! -d $bin ]; then
    mkdir $bin 
fi
    
cd $bin

rm -fr * >& /dev/null    # remove all links

if [ ! -f orac ]; then
    if [ -f ../src/INTEL-FFTW-OMP/orac  ] ; then
	ln -s  ../src/INTEL-FFTW-OMP/orac .
    elif [ -f ../src/GNU-FFTW-OMP/orac  ] ; then 	
	ln -s  ../src/GNU-FFTW-OMP/orac .
    else
	echo " **WARNING: no OMP executable found in src dir."
	echo "            See INSTALL file in parent dir for compilation intructions" 
	echo " **WARNING: NO LINK to the orac executable established in bind dir" 
    fi
fi
if [ ! -f orac_mpi ]; then
    if [ -f ../src/INTEL-FFTW-OMP-MPI/orac  ] ; then
	ln -s  ../src/INTEL-FFTW-OMP-MPI/orac orac_mpi
    elif [ -f ../src/GNU-FFTW-OMP-MPI/orac  ] ; then 	
	ln -s  ../src/GNU-FFTW-OMP-MPI/orac orac_mpi
    else
	echo " **WARNING: no MPI/OMP executable found in src dir"
	echo "            See INSTALL file in parent dir for compilation intructions" 
	echo " **WARNING: NO LINK to the orac_mpi executable established in bind dir" 
    fi
fi

cd $prima_src
echo "... compiling primadorac..." $prima_src
make >& /dev/null
echo "... compiling orac-GNU (needed for primadorac)"
cd $orac_home/src
./configure >& config_gnu.log 
make >&  make.log

cd $www
echo "... compiling primadorac tools..."
gfortran -o new_rms new_rms.f

cd $prima
sed -i "s?\~/ORAC/trunk?$orac_home?g" primadorac.bash 
echo "check for mopac exe ..."
which MOPAC2016.exe >& /dev/null
testmo=`echo $?`

if [ $testmo == "0" ] ; then 
    echo "MOPAC2016.exe il locally installed in " $MOPAC_LICENSE 
else
    if [ ! -f /opt/mopac/MOPAC2016.exe ]; then
	echo "/opt/mopac/MOPAC2016.exe not found. Try to build mopac7.." 
	tar zxvf mopac7-1.11.tar.gz >& /dev/null
	cd mopac7-1.11
	ftoc=`which f2c`
	if [[ $ftoc != "/usr/bin/f2c" ]] ; then 
	    which   f2c || echo "no f2c; install f2c for mopac install"
	else
	    ./configure > config.log
	    sed -i "s/MAXHEV=60/MAXHEV=100/g" fortran/SIZES
	    sed -i "s/MAXLIT=60/MAXLIT=100/g" fortran/SIZES
	    make > mopac.log || echo ".. mopac compilkation failed"
	fi 
    else
	echo "/opt/mopac/MOPAC2016.exe found.."
    fi
fi
# 
cd $orac_home/tools/hist2d/src
echo "... compiling hist2d, analysis, em, qq, qtcluster, xyz2pdb, metafes "
make >& make.log 
cd $bin 
ln -s $orac_home/tools/hist2d/src/hist2d .

cd $orac_home/tools/analysis/src
make >& make.log 
cd $bin 
ln -s $orac_home/tools/analysis/src/analysis .

cd $orac_home/tools/em/src
make >& make.log 
cd $bin 
ln -s $orac_home/tools/em/src/em .

cd $orac_home/tools/bennett
make >& make.log
cd $bin
ln -s $orac_home/tools/bennett/bennett .

cd $orac_home/tools/metafes
make >& make.log
cd $bin
ln -s $orac_home/tools/metafes/metafes .

cd $orac_home/tools/QQ_test/
make >& make.log 
cd $bin
ln -s $orac_home/tools/QQ_test/qq . 

cd $orac_home/tools/qtcluster/src
make >& make.log 
cd $bin
ln -s $orac_home/tools/qtcluster/src/qtcluster .

cd $orac_home/tools/xyz2pdb/src
make xyz2pdb >& make.log 
make COMxyz2pdb >& make.log 
cd $bin
ln -s $orac_home/tools/xyz2pdb/src/xyz2pdb .
ln -s $orac_home/tools/xyz2pdb/src/COMxyz2pdb .

cd $orac_home/tools/orient
gfortran -o orient orient.f90
cd $bin
ln -s $orac_home/tools/orient/orient .

cd $orac_home/tools/uorient
gfortran -o uorient uorient.f90
cd $bin
ln -s $orac_home/tools/uorient/uorient .

cd $orac_home/tools/SI_bar/src
make >& make.log 
cd $bin
ln -s $orac_home/tools/SI_bar/src/mbar .

cd $orac_home/tools/alltrans
gfortran -o alltrans alltrans.f90
cd $bin
ln -s $orac_home/tools/alltrans/alltrans .

cd $orac_home/etc 

echo "New commands set in bin directory: "
ls $bin
