#!/bin/bash
#************************************************************
#INSTALLATION
#   search the strings "???"
#   eventually modify the corresponding line
#************************************************************

###########################################################################

#
# create include files for the makfiles, and compile
#

COMP90=ifort #??? ifc ifort pgf xlf90 gfortran
BITS=64 #??? 32 64  (32 or 64 bit processor)
LAPACK=NONE #??? LOCAL PATH/lapack.a
BLAS=NONE #??? LOCAL PATH/blas.a
#(NONE: nor installed, LOCAL: local sources, 
#PATH: path where a compiled version exists)
MAXLONG=200 # ??? (in Gb) (max possible length for a file on your computer)
mkdir param
echo "      parameter (maxlong=$MAXLONG"000000" )" > param/maxlong.prm

###########################################################################


#mkdir bin
cd=$PWD/bin/
##modif iclock
#cd biblio
#echo "sed 's?<timebits.h>?<"$PWD"/timebits.h>?g' <iclock.0>iclock ">xiclock
#chmod +x xiclock
#./xiclock
#mv iclock iclock.c
#cd - >/dev/null
##fin modif iclock
cd source
#du

#
# create some files which depend on the environnement for noscf and lcasscf
#
echo NAME= ../../bin/lcasscf.exe > lcasscf/NAME
echo NAME= ../../bin/noscf.exe > noscf/NAME
echo NAME= ../../bin/noscfopenmolcas.exe > noscfopenmolcas/NAME
cp noscf/noscf ../bin
cp noscfopenmolcas/noscfopenmolcas ../bin
cp lcasscf/lcasscf ../bin
cp dolo/idolo ../bin

cd noscf
echo "      pgm=""'"$cd"'" > pgm.prm0
echo "      casdet=pgm(1:len_trim(pgm))//'casdet'" >> pgm.prm
echo "      casdi=pgm(1:len_trim(pgm))//'casdi'" >> pgm.prm
echo "      locnat=pgm(1:len_trim(pgm))//'locnats'" >> pgm.prm
echo "      molcost=pgm(1:len_trim(pgm))//'molcost'" >> pgm.prm
echo "      molcostopenmolcas=pgm(1:len_trim(pgm))//'molcostopenmolcas'" >> pgm.prm
echo "      reordens=pgm(1:len_trim(pgm))//'reordens'" >> pgm.prm
cd - > /dev/null
cd noscfopenmolcas
echo "      pgm=""'"$cd"'" > pgm.prm0
echo "      casdet=pgm(1:len_trim(pgm))//'casdet'" >> pgm.prm
echo "      casdi=pgm(1:len_trim(pgm))//'casdi'" >> pgm.prm
echo "      locnat=pgm(1:len_trim(pgm))//'locnats'" >> pgm.prm
echo "      molcost=pgm(1:len_trim(pgm))//'molcost'" >> pgm.prm
echo "      molcostopenmolcas=pgm(1:len_trim(pgm))//'molcostopenmolcas'" >> pgm.prm
echo "      reordens=pgm(1:len_trim(pgm))//'reordens'" >> pgm.prm
cd - > /dev/null

cd lcasscf
echo "      pgm=""'"$cd"'" > pgm.prm0
echo "      casdet=pgm(1:len_trim(pgm))//'casdet'" >> pgm.prm
echo "      casdi=pgm(1:len_trim(pgm))//'casdi'" >> pgm.prm
echo "      locnat=pgm(1:len_trim(pgm))//'locnats'" >> pgm.prm
echo "      molcost=pgm(1:len_trim(pgm))//'molcost'" >> pgm.prm
echo "      molcostopenmolcas=pgm(1:len_trim(pgm))//'molcostopenmolcas'" >> pgm.prm
cd - > /dev/null
cd loccasscf
echo "      pgm=""'"$cd"'" > pgm.prm0
cd - > /dev/null

###########################################################################

#
# Lists of Fort90 source files
#

FORT90_FILES=`cat ../FORT90_FILES`
if [ $BLAS = 'LOCAL' ] ; then
 FORT90_FILES=" blas "$FORT90_FILES
if [ $COMP90 = 'ifort' ]
then
echo F90= ifort  > blas/F90
echo FFLAGS90= -c -O2  > blas/FFLAGS90       # ???
fi
fi
if [ $LAPACK = 'LOCAL' ] ; then
 FORT90_FILES="lapack "$FORT90_FILES
if [ $COMP90 = 'ifort' ]
then
echo F90= ifort  > lapack/F90
 echo FFLAGS90= -c -O2  > lapack/FFLAGS90       # ???
fi
fi

###########################################################################

#
# create NAME file in each directory
#

for i in $FORT90_FILES ; do
echo NAME= ../../bin/${i} > $i/NAME
done

###########################################################################

# Add noscf, lcasscf  to FORT90_FILES
#FORT90_FILES="$FORT90_FILES noscf lcasscf"
 
###########################################################################

#
# 32 bits / 64 bits
#

pwd
if [ $BITS = '64' ]
then
echo "      y64=.true." > ../biblio/Y64
else
echo "      y64=.false." > ../biblio/Y64
fi


###########################################################################
#
# Fort 90 programs
#
###########################################################################

for i in $FORT90_FILES ; do 
echo "*******************************************"
echo $i
echo "*******************************************"
echo .

##### BLAS and LAPACK
if [ $LAPACK = 'LOCAL' ] ; then 
echo LAPACK="../lapack/lapack.a" > $i/LAPACK
elif [ $LAPACK != 'LOCAL' ] ; then 
echo LAPACK=$LAPACK > $i/LAPACK
fi
if [ $BLAS = 'LOCAL' ] ; then 
echo BLAS="../blas/blas.a" > $i/BLAS
elif [ $BLAS != 'LOCAL' ] ; then 
echo BLAS=$BLAS > $i/BLAS
fi
##### BLAS and LAPACK (fin)

if [ $COMP90 = 'gfortran' ]
then
echo F90= gfortran > $i/F90
echo CC= cc > $i/CC
echo FFLAGS90= -c -O2 > $i/FFLAGS90
echo FFLAGS90_free= -c -O2 -ffree-form > $i/FFLAGS90_free
echo LFLAGS90=  > $i/LFLAGS90
echo CFLAGS= -c > $i/CFLAGS
echo BIBLI= ../../biblio > $i/BIBLI
fi

if [ $COMP90 = 'ifort' ]
then
#echo F90= ifort  -Vaxlib > $i/F90
echo F90= ifort  > $i/F90
echo CC= cc > $i/CC
#echo FFLAGS90= -c -O2 -xP -ip > $i/FFLAGS90       # ???
echo FFLAGS90= -c -O2 -heap-arrays > $i/FFLAGS90       # ???
echo FFLAGS90_free= -c -O2 -free > $i/FFLAGS90_free
echo LFLAGS90=  > $i/LFLAGS90                     # ???
echo CFLAGS= -c -DINTEL > $i/CFLAGS               # ???
echo BIBLI= ../../biblio > $i/BIBLI
fi

if [ $COMP90 = 'pgf90' ] || [ $COMP90 = 'pgf' ]
then
echo F90= pgf90  > $i/F90
echo CC= pgcc > $i/CC
echo FFLAGS90= -c -O2 > $i/FFLAGS90               # ???
echo LFLAGS90= > $i/LFLAGS90                      # ???
echo CFLAGS= -c > $i/CFLAGS                       # ???
echo BIBLI= ../../biblio > $i/BIBLI
fi

#if [ $COMP90 = 'pgf' ]
#then
#echo F90= pgf90  > $i/F90
#echo CC= pgcc > $i/CC
#echo FFLAGS90= -c -O2 > $i/FFLAGS90
#echo LFLAGS90= > $i/LFLAGS90
#echo CFLAGS= -c > $i/CFLAGS
#echo BIBLI= ../../biblio > $i/BIBLI
#fi

if [ $COMP90 = 'xlf90' ]
then
echo F90= xlf90 > $i/F90
echo CC= cc > $i/CC
echo FFLAGS90= -c -qfixed -qmaxmem=8000 -O2 -qextname > $i/FFLAGS90  # ???
echo LFLAGS90= > $i/LFLAGS90                                         # ???
echo CFLAGS= -c > $i/CFLAGS                                          # ???
echo BIBLI= ../../biblio > $i/BIBLI
fi

if [ $i = 'lapack' ]  ; then
if [ $LAPACK = 'LOCAL' ] ; then 
ar -x -o lapack.a *.o 
fi
fi
if [ $i = 'blas' ] ; then
if [ $BLAS = 'LOCAL' ] ; then  
 ar -x -o blas.a *.o 
fi
fi



cd $i
make
cd -
done
