#!/bin/bash
###############################################################
#
# bash script to build MRCC executables
#
###############################################################

###############################################################
#
# functions used in the build.mrcc script 
# to print messages
#
###############################################################

###############################################################
# (evident) called at the begining of build.mrcc
# if there are no command line parameters present
# ||
# ||
# \/

f_print_help(){ 


    echo "########################################################################"

#   echo "Usage:   build.mrcc compiler [-i32|64] [-pMPI|OMP] [-g] [-d] [-Q]"
    echo "Usage:"
    echo "   build.mrcc <compiler> [-i32|64] [-pMPI|OMP] [-g] [-d] [-f<folder>] [-l<library>]"
    echo "   where <compiler> can be:"
    echo "      Intel       - Intel compiler;"
    echo "      GNU         - GNU compiler;"
    echo "      PGF         - Portland Group Fortran compiler;"
    echo "      G95         - G95 Fortran 95 compiler and MKL;"
    echo "      PATH        - Pathscale compiler;"
    echo "      HP          - HP Fortran Compiler;"
    echo "      DEC         - Compaq Fortran Compiler (DEC machines);"
    echo "      XLF         - XL Fortran Compiler (IBM machines);"
    echo "      Solaris10   - Sun Solaris10 and Studio10 Fortran Compiler (AMD64)."
    echo
    echo "Optional arguments:"
    echo "   -i Length of integer variables: 32 or 64 bits. Default: 64 for 64-bit"
    echo "      machines; 32 otherwise."
    echo "   -p Generates parallel code using MPI (available with PGF, Intel, GNU"
    echo "      and Solaris10) or OpenMP (available with PGF, Intel, GNU, and HP)"
    echo "      technologies. Default: no parallelization."
    echo "   -g Codes are compiled with debugging option"
    echo "   -d Codes are compiled for development, no optimization is performed"
    echo "   -f Specifies the installation folder. Executables, basis set"
    echo "      libraries, etc. will be copied to directory <folder>"
    echo "   -l mrcc is linked with an external library, currently the only option"
    echo "      is libxc"
#   echo "   -Q The Q5Cost libraries are linked"
#   echo "   -h print out this message and exit"
    echo
    echo "Example:"
    echo "   To compile the program with Intel compiler for OpenMP parallel"
    echo "   execution and to install it to the /prog/mrcc directory run"
    echo "   build.mrcc as"
    echo "      build.mrcc Intel -pOMP -f/prog/mrcc"
    echo "########################################################################"    
}
###############################################################
# (evident) called to print out error messages
# mainly used when the not-supported combinations are checked
# ||
# ||
# \/

f_print_error(){
 echo "ERROR: $2 is not supported for $1! "
}


###############################################################
# source the function definitions used in this script
# ||
# ||
# \/

#. build.mrcc.functions.messages
. build.mrcc.config
clear
echo "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
echo 
echo -en "\\033[0;32m"
echo "                   MRCC installation                        "
echo -en "\\033[0;39m"
echo

if [ $# -lt 1 ]; # at least the compiler system is need to be specified
then
    f_print_help;
    exit;
fi

###############################################################
# set the default values
# ||
# ||
# \/

export bstat="yes" #build status
fls=""
para=""
debug=""
devel=""
q5cost=""
ompopt=""
intopt=""
extralibs=""
libxclib=""
libxcinc=""
xalloc="xalloc.c"
machine="unknown"
kernel="unknown"
test /bin/uname && machine="`uname -m`"
test /bin/uname && kernel="`uname -s`"

mrccstuff="mrcc goldstone lambda sacc pert combin mem qsorti xmrcc xlambda xpert xmem dcommunicate3 dmrcc flush integ ecp teint dfint dfintloc hrrsub_der df2intsubs dfint_triplets brasub intsub ellip orbloc mulli scf hessgrad diis ccsd uccsd minp ovirt prop propcore denschol pml bopu drpa ldrpa loccis drpagrad dft calcorb calcorbd calcorbh func laplace qmmod optim basopt cis geomopt z2c freqdrv assembly oneint oneint_sh oneint_shc nucint nucint_shc onein1 onein1_sh onein1_shc nuceq1 nuceq1_shc nucat1 nucat1_shc der_interface_subs intsub_der dfint_triplets_der brasub_der ketsub_der hrrspher"

mrccexe="dmrcc mrcc xmrcc goldstone integ scf orbloc ccsd uccsd ovirt minp prop drpa cis mulli qmmod dirac_mointegral_export"

if [ x$kernel == "xDarwin" ]; 
then machine="x86_64"
fi

if ([ x$machine == "xi386" ] || [ x$machine == "xi686" ] || [ x$machine == "xPower Macintosh" ]); 
then int="INT32"
else int="INT64"
fi

echo $* > build.info_new
rebuild="no"

#???
test -f build.info || rebuild="yes"
#rebuild="no"
#if [ -f build.info ] || [ x$rebuild == "xyes" ]; then echo "???"
#fi

if [ x$rebuild == "xno" ]; 
then
    if [ `diff build.info build.info_new | wc -l | sed "s/ //g"` != "0" ]; #???
    then rebuild="yes"
    fi
fi
mv build.info_new build.info

###############################################################
# processing the optional arguments
# ||
# ||
# \/

ii=0
for i in $*
do
    ii=`expr $ii + 1`
    if [ $ii -gt 1 ];
    then
        arv=`echo $i | sed 's/-.//g' | sed 's/\///g'`

        if [ x$arv == 'x' ]; 
        then arg=$i
        else arg=`echo $i | sed 's/\///g' | sed "s/$arv//g"`
        fi

        case $arg in
            "-i") int="INT"$arv;;
            "-p") para=$arv;;
            "-g") debug="-g";;
            "-d") devel="on";;
            "-f") instdir=`echo $i | sed 's/-.//g'`;;
            "-l") libxcinc="-DLIBXC -I"$LIBS_LIBXC"/include"; libxclib="-L"$LIBS_LIBXC"/lib -lxcf90 -lxc -lm"; test -d $LIBS_LIBXC/lib64 && libxclib="-L"$LIBS_LIBXC"/lib64 -lxcf90 -lxc -lm";;
            "-Q") q5cost="on";;
            "-h") print_help; exit;;
              * ) echo "Unknown option $i"; exit;;
        esac
    fi
done

if [ x$para == "xOpenMP" ];
then
   para="OMP"
fi

###############################################################
# setup options for the selected compiler system
# the appropriate functions are defined in
# the build.mrcc.functions.compilers file
# ||
# ||
# \/

case $1 in
    "GNU"       ) f_GNU;;
    "Intel"     ) f_Intel;;
    "PGF"       ) f_PGF;;
    "G95"       ) f_G95;;
    "PATH"      ) f_PATH;;
    "HP"        ) f_HP;;
    "DEC"       ) f_DEC;;
    "XLF"       ) f_XLF;;
    "Solaris10" ) f_Solaris10;;
      *         ) f_Unknown;;
esac

compileropts="$compileropts $debug -D$1 -D$int "
ccompileropts="$ccompileropts $debug -D$1 -D$int "

if [ x$para != "x" ]; 
then
    compileropts="$compileropts -D$para "
    ccompileropts="$ccompileropts -D$para "
fi

if [ x$q5cost != "x" ];
then
    compileropts="$compileropts -I$HDF5PATH -I$Q5COSTPATH/include -DQ5Cost "
    ccompileropts="$ccompileropts -DQ5Cost "
    extralibs="$extralibs -L$Q5COSTPATH/lib/ -lq5cost -L$HDF5PATH -lhdf5_fortran -lhdf5 "
fi

echo "Fortran compiler:       $fortran "
#echo `$fortran -dumpversion`
echo "C compiler:             $ccompiler "
#echo `$ccompiler -dumpversion`
echo "FC options:             $compileropts"
echo "CC options:             $ccompileropts"
if [ x$instdir != "x" ];
then
echo "Installation directory: $instdir"
fi
echo

###############################################################
# invalid and unsupported combinations of options are checked
# ||
# ||
# \/

# ERRORS ######################################################
if (([ x$int == "xINT64" ]) && ([ x$1 == "xDEC" ] || [ x$1 == "xXLF" ])); 
then
    f_print_error $1 "i64"; 
    exit;
fi

if ([ x$int == "xINT32" ] && [ x$1 == "xSolaris10" ]); 
then
    f_print_error $1 "i32";
    exit;
fi

if (([ x$para == "xOMP" ]) && ([ x$1 != "xPGF" ] && [ x$1 != "xIntel" ] &&  [ x$1 != "xHP" ] && [ x$1 != "xPATH" ] && [ x$1 != "xGNU" ])); 
then
    f_print_error $1 "OpenMP";
    exit;
fi

if (([ x$para == "xMPI" ]) && ([ x$1 != "xPGF" ] && [ x$1 != "xIntel" ] && [ x$1 != "xSolaris10" ] && [ x$1 != "xGNU" ])); 
then
    f_print_error $1 "MPI";
    exit;
fi

if (([ x$q5cost == "xon" ]) && ([ x$1 == "xGNU" ])); 
then
    echo "ERROR: The Q5Cost library cannot be used with Fortran 77 compilers! "
    exit;
fi

# WARNINGS ##############################################################
if (([ x$q5cost == "xon" ]) && ([ x$(`set | grep -i HDF5PATH`) == "x" ])); 
then
    echo "WARNING: please set the HDF5PATH environmental variable! "
    exit;
fi

if (([ x$q5cost == "xon" ]) && ([ x$(`set | grep -i Q5COSTPATH`) == "x" ])); 
then
    echo "WARNING: please set the Q5COSTPATH environmental variable! "
    exit;
fi

#if (([ x$para == "xOMP" ]) && ([ x$1 = "xGNU" ])); 
#then
    #echo "WARNING: OMP directives will be converted to lowercase, if it is necessary!"
    #./build.mrcc.changecase.sh tolower
#fi

# Compiling to create object files #####################################

for i in $mrccstuff
do
    ii=$i.f
    test -f $i.f90 && ii=$i.f90
    test -f $i.F90 && ii=$i.F90
    tt=$rebuild
    test $ii -nt $i.o -o MRCCCOMMON -nt $i.o -o build.mrcc -nt $i.o && tt="yes"
    test -f $i.o || tt="yes"
    test -f $ii || export bstat="no"
    if [ x$i == "xscf" ]; 
    then
     test SCFCOMMON -nt scf.o && tt="yes"
    fi
    if [ x$i == "xhessgrad" ]; 
    then
     test SCFCOMMON -nt hessgrad.o && tt="yes"
    fi
    if [ x$i == "xdfint" ]; 
    then
     test dfvariables_IN_ijk.f        -nt dfint.o && tt="yes"
     test dfvariables_OUT_ijk.f       -nt dfint.o && tt="yes"
     test dfvariables_GHP.f           -nt dfint.o && tt="yes"
     test dfvariables_GHP.f           -nt dfint.o && tt="yes"
     test dfvariables_der.f           -nt dfint.o && tt="yes"
     test df2intvars.f                -nt dfint.o && tt="yes"
     test df2intvars_uc.f             -nt dfint.o && tt="yes"
     test dfint_call.f                -nt dfint.o && tt="yes"
     test dfint_call_kc.f             -nt dfint.o && tt="yes"
     test dprimcalc_call.f            -nt dfint.o && tt="yes"
     test icontr_general.f            -nt dfint.o && tt="yes"
     test dfint_beg_GHP_ijk.f         -nt dfint_triplets.o && tt="yes"
     test dfint_beg_kc_GHP_ijk.f      -nt dfint_triplets.o && tt="yes"
     test dfint_mid_GHP_ijk.f         -nt dfint_triplets.o && tt="yes"
     test dfint_mid_kc_GHP_ijk.f      -nt dfint_triplets.o && tt="yes" 
     test dfint_end_kc_IN_ijk.f       -nt dfint_triplets.o && tt="yes"
     test dfint_end_IN_ijk.f          -nt dfint_triplets.o && tt="yes"
    fi
    if [ x$i == "xdfint_triplets" ]; 
    then
     test dfint_beg_kc_IN_ijk.f       -nt dfint_triplets.o && tt="yes"
     test dfint_beg_kc_OUT_ijk.f      -nt dfint_triplets.o && tt="yes"
     test dfint_mid_kc_IN_ijk.f       -nt dfint_triplets.o && tt="yes"
     test dfint_mid_kc_OUT_ijk.f      -nt dfint_triplets.o && tt="yes"
     test dfint_end_kc_IN_ijk.f       -nt dfint_triplets.o && tt="yes"
     test dfint_end_kc_OUT_ijk.f      -nt dfint_triplets.o && tt="yes"
     test dfint_beg_IN_ijk.f          -nt dfint_triplets.o && tt="yes"
     test dfint_beg_OUT_ijk.f         -nt dfint_triplets.o && tt="yes"
     test dfint_mid_IN_ijk.f          -nt dfint_triplets.o && tt="yes"
     test dfint_mid_OUT_ijk.f         -nt dfint_triplets.o && tt="yes"
     test dfint_end_IN_ijk.f          -nt dfint_triplets.o && tt="yes"
     test dfint_end_OUT_ijk.f         -nt dfint_triplets.o && tt="yes"
     test rysrw_3_sub.f               -nt dfint_triplets.o && tt="yes"
     test rysrw_3_vars.f              -nt dfint_triplets.o && tt="yes"
     test rysrw_4_sub.f               -nt dfint_triplets.o && tt="yes"
     test rysrw_4_vars.f              -nt dfint_triplets.o && tt="yes"
     test icontr_general.f            -nt dfint_triplets.o && tt="yes"
    fi
    if [ x$i == "xintsub" ]; 
    then
     test rysrw_3_sub.f               -nt intsub.o && tt="yes"
     test rysrw_3_vars.f              -nt intsub.o && tt="yes"
     test rysrw_4_sub.f               -nt intsub.o && tt="yes"
     test rysrw_4_vars.f              -nt intsub.o && tt="yes"
    fi
    if [ x$i == "xdfint_triplets_der" ]; 
    then
     test dfint_beg_der.f -nt dfint_triplets_der.o && tt="yes"
     test dfint_mid_der.f -nt dfint_triplets_der.o && tt="yes"
     test dfint_end_der.f -nt dfint_triplets_der.o && tt="yes"
    fi
    if [ x$i == "xdf2intsubs" ]; 
    then
     test dfint_beg_tc.f    -nt df2intsubs.o && tt="yes"
     test dfint_beg_tc_uc.f -nt df2intsubs.o && tt="yes"
     test dfint_mid_tc.f    -nt df2intsubs.o && tt="yes"
     test dfint_mid_tc_uc.f -nt df2intsubs.o && tt="yes"
     test dfint_end_tc.f    -nt df2intsubs.o && tt="yes"
     test dfint_end_tc_uc.f -nt df2intsubs.o && tt="yes"
    fi
    if [ x$i == "xuccsd" ]; 
    then
     test uccsd_taaa_beg.f -nt uccsd.o && tt="yes"
     test uccsd_taaa_end.f -nt uccsd.o && tt="yes"
     test uccsd_tabb_beg.f -nt uccsd.o && tt="yes"
     test uccsd_tabb_end.f -nt uccsd.o && tt="yes"
    fi
    if [ x$tt == "xyes" ]; 
    then
        if ([ x$i == "xdmrcc" ] || [ x$i == "xovirt" ] || [ x$i == "xoptim" ] || [ x$i == "xbasopt" ] || [ x$i == "xgeomopt" ]); 
        then
            echo "Compiling $i with options $preprocopt $compileropts"
            $fortran $preprocopt $compileropts -c $ii || export bstat="no"
        else
            echo "Compiling $i with options $preprocopt $compileropts $ompopt $libxcinc"
            $fortran $preprocopt $compileropts $ompopt $libxcinc -c $ii || export bstat="no"
        fi
    fi
done
echo

# C routines ###########################################################
test -f $xalloc || export bstat="no"
test -f MRCCCOMMON || export bstat="no"
tt=$rebuild
test $xalloc -nt xalloc.o -o build.mrcc -nt xalloc.o && tt="yes"
test -f xalloc.o || tt="yes"
if [ x$tt == "xyes" ]; 
then  
 echo "Compiling xalloc with options $ccompileropts"
 $ccompiler $ccompileropts -c $xalloc || export bstat="no"
fi

test -f intio.c || export bstat="no"
test -f MRCCCOMMON || export bstat="no"
tt=$rebuild
test intio.c -nt intio.o -o build.mrcc -nt intio.o && tt="yes"
test -f intio.o || tt="yes"
if [ x$tt == "xyes" ];
then
 echo "Compiling intio with options $ccompileropts"
 $ccompiler $ccompileropts -c intio.c || export bstat="no"
fi

echo 

#
#/bin/rm -f mrcc dmrcc xmrcc goldstone
#

# Linking goldstone ####################################################

tt=$rebuild
test -f goldstone || tt="yes"
for i in `echo "goldstone.o mem.o xalloc.o combin.o qsorti.o"`
do
    test $i -nt goldstone && tt="yes"
done

if [ x$tt == "xyes" ];
then
    if (([ "x$extralibs" != "x" ]) || ([ "x$linpacklib" != "x" ]));
    then echo "Linking goldstone with libraries: $linpacklib $extralibs"
    else echo "Linking goldstone"
    fi
    $fortran $compileropts $ompopt -o goldstone goldstone.o mem.o xalloc.o combin.o qsorti.o $linpacklib $extralibs || export bstat="no"
fi

# Linking mrcc #########################################################

tt=$rebuild
test -f mrcc || tt="yes"
for i in `echo "mrcc.o lambda.o pert.o mem.o xalloc.o combin.o $fls sacc.o dcommunicate3.o"`
do
    test $i -nt mrcc && tt="yes"
done

if [ x$tt == "xyes" ]; 
then
    if (([ "x$extralibs" != "x" ]) || ([ "x$linpacklib" != "x" ]));
    then echo "Linking mrcc with libraries: $linpacklib $extralibs"
    else echo "Linking mrcc"
    fi
    $fortran $compileropts $ompopt -o mrcc mrcc.o lambda.o pert.o mem.o xalloc.o combin.o $fls sacc.o dcommunicate3.o $linpacklib $extralibs || export bstat="no"
fi

# Linking integ ########################################################

tt=$rebuild
test -f integ || tt="yes"
for i in `echo "integ.o teint.o ecp.o dfint.o dfintloc.o df2intsubs.o hrrspher.o dfint_triplets.o brasub.o intsub.o der_interface_subs.o intsub_der.o dfint_triplets_der.o brasub_der.o ketsub_der.o hrrsub_der.o ellip.o calcorb.o mem.o xalloc.o combin.o intio.o z2c.o oneint.o oneint_sh.o oneint_shc.o nucint.o nucint_shc.o onein1.o onein1_sh.o onein1_shc.o nuceq1.o nuceq1_shc.o nucat1.o nucat1_shc.o $fls"`
do
    test $i -nt integ && tt="yes"
done

if [ x$tt == "xyes" ];
then
    if (([ "x$extralibs" != "x" ]) || ([ "x$linpacklib" != "x" ]) || ([ "x$libxclib" != "x" ]));
    then echo "Linking integ with libraries: $linpacklib $extralibs $libxclib"
    else echo "Linking integ"
    fi
    $fortran $compileropts $ompopt -o integ integ.o teint.o ecp.o dfint.o dfintloc.o df2intsubs.o hrrspher.o dfint_triplets.o brasub.o intsub.o der_interface_subs.o intsub_der.o dfint_triplets_der.o brasub_der.o ketsub_der.o hrrsub_der.o ellip.o calcorb.o mem.o xalloc.o combin.o intio.o z2c.o oneint.o oneint_sh.o oneint_shc.o nucint.o nucint_shc.o onein1.o onein1_sh.o onein1_shc.o nuceq1.o nuceq1_shc.o nucat1.o nucat1_shc.o $fls $linpacklib $extralibs $libxclib || export bstat="no"
fi

# Linking scf ##########################################################

tt=$rebuild
test -f scf || tt="yes"
for i in `echo "scf.o hessgrad.o diis.o teint.o ecp.o dfint.o dfintloc.o df2intsubs.o hrrspher.o dfint_triplets.o brasub.o intsub.o der_interface_subs.o intsub_der.o dfint_triplets_der.o brasub_der.o ketsub_der.o hrrsub_der.o ellip.o dft.o calcorb.o calcorbd.o calcorbh.o func.o mem.o xalloc.o combin.o intio.o denschol.o pml.o bopu.o onein1.o onein1_sh.o onein1_shc.o nuceq1.o nuceq1_shc.o nucat1.o nucat1_shc.o $fls"`
do
    test $i -nt scf && tt="yes"
done

if [ x$tt == "xyes" ];
then
    if (([ "x$extralibs" != "x" ]) || ([ "x$linpacklib" != "x" ]) || ([ "x$libxclib" != "x" ]));
    then echo "Linking scf with libraries: $linpacklib $extralibs $libxclib"
    else echo "Linking scf"
    fi
    $fortran $compileropts $ompopt -o scf scf.o hessgrad.o diis.o teint.o ecp.o dfint.o dfintloc.o df2intsubs.o hrrspher.o dfint_triplets.o brasub.o intsub.o der_interface_subs.o intsub_der.o dfint_triplets_der.o brasub_der.o ketsub_der.o hrrsub_der.o ellip.o dft.o calcorb.o calcorbd.o calcorbh.o func.o mem.o xalloc.o combin.o intio.o denschol.o pml.o bopu.o onein1.o onein1_sh.o onein1_shc.o nuceq1.o nuceq1_shc.o nucat1.o nucat1_shc.o $fls $linpacklib $extralibs $libxclib || export bstat="no"
fi

# Linking ovirt ########################################################

tt=$rebuild
test -f ovirt || tt="yes"
for i in `echo "ovirt.o intio.o combin.o $fls"`
do
    test $i -nt ovirt && tt="yes"
done

if [ x$tt == "xyes" ];
then
    if (([ "x$extralibs" != "x" ]) || ([ "x$linpacklib" != "x" ]));
    then echo "Linking ovirt with libraries: $linpacklib $extralibs"
    else echo "Linking ovirt"
    fi
    $fortran $compileropts $ompopt -o ovirt ovirt.o intio.o combin.o $fls $linpacklib $extralibs || export bstat="no"
fi

# Linking orbloc #######################################################

tt=$rebuild
test -f orbloc || tt="yes"
for i in `echo "orbloc.o mem.o xalloc.o combin.o denschol.o pml.o $fls"`
do
    test $i -nt orbloc && tt="yes"
done

if [ x$tt == "xyes" ];
then
    if (([ "x$extralibs" != "x" ]) || ([ "x$linpacklib" != "x" ]));
    then echo "Linking orbloc with libraries: $linpacklib $extralibs"
    else echo "Linking orbloc"
    fi
    $fortran $compileropts $ompopt -o orbloc orbloc.o mem.o xalloc.o combin.o denschol.o pml.o $fls $linpacklib $extralibs || export bstat="no"
fi

# Linking mulli #######################################################

tt=$rebuild
test -f mulli || tt="yes"
for i in `echo "mulli.o mem.o xalloc.o combin.o bopu.o $fls"`
do
    test $i -nt mulli && tt="yes"
done

if [ x$tt == "xyes" ];
then
    if (([ "x$extralibs" != "x" ]) || ([ "x$linpacklib" != "x" ]));
    then echo "Linking mulli with libraries: $linpacklib $extralibs"
    else echo "Linking mulli"
    fi
    $fortran $compileropts $ompopt -o mulli mulli.o mem.o xalloc.o combin.o bopu.o $fls $linpacklib $extralibs || export bstat="no"
fi

# Linking prop #########################################################

tt=$rebuild
test -f prop || tt="yes"
for i in `echo "prop.o propcore.o denschol.o diis.o teint.o ecp.o dfint.o dfintloc.o df2intsubs.o hrrspher.o dfint_triplets.o brasub.o intsub.o der_interface_subs.o intsub_der.o dfint_triplets_der.o brasub_der.o ketsub_der.o hrrsub_der.o ellip.o dft.o calcorb.o calcorbd.o calcorbh.o func.o pml.o mem.o xalloc.o combin.o intio.o onein1.o onein1_sh.o onein1_shc.o nuceq1.o nuceq1_shc.o nucat1.o nucat1_shc.o $fls"`
do
    test $i -nt prop && tt="yes"
done

if [ x$tt == "xyes" ];
then
    if (([ "x$extralibs" != "x" ]) || ([ "x$linpacklib" != "x" ]) || ([ "x$libxclib" != "x" ]));
    then echo "Linking prop with libraries: $linpacklib $extralibs $libxclib"
    else echo "Linking prop"
    fi
    $fortran $compileropts $ompopt -o prop prop.o propcore.o denschol.o diis.o teint.o ecp.o dfint.o dfintloc.o df2intsubs.o hrrspher.o dfint_triplets.o brasub.o intsub.o der_interface_subs.o intsub_der.o dfint_triplets_der.o brasub_der.o ketsub_der.o hrrsub_der.o ellip.o dft.o calcorb.o calcorbd.o calcorbh.o func.o pml.o mem.o xalloc.o combin.o intio.o onein1.o onein1_sh.o onein1_shc.o nuceq1.o nuceq1_shc.o nucat1.o nucat1_shc.o $fls $linpacklib $extralibs $libxclib || export bstat="no"
fi

# Linking ccsd #########################################################

tt=$rebuild
test -f ccsd || tt="yes"
for i in `echo "ccsd.o diis.o laplace.o mem.o xalloc.o combin.o assembly.o $fls"`
do
    test $i -nt ccsd && tt="yes"
done

if [ x$tt == "xyes" ];
then
    if (([ "x$extralibs" != "x" ]) || ([ "x$linpacklib" != "x" ]));
    then echo "Linking ccsd with libraries: $linpacklib $extralibs"
    else echo "Linking ccsd"
    fi
    $fortran $compileropts $ompopt -o ccsd ccsd.o diis.o laplace.o mem.o xalloc.o combin.o assembly.o $fls $linpacklib $extralibs || export bstat="no"
fi

# Linking uccsd ########################################################

tt=$rebuild
test -f uccsd || tt="yes"
for i in `echo "uccsd.o diis.o mem.o xalloc.o combin.o $fls"`
do
    test $i -nt uccsd && tt="yes"
done

if [ x$tt == "xyes" ];
then
    if (([ "x$extralibs" != "x" ]) || ([ "x$linpacklib" != "x" ]));
    then echo "Linking uccsd with libraries: $linpacklib $extralibs"
    else echo "Linking uccsd"
    fi
    $fortran $compileropts $ompopt -o uccsd uccsd.o diis.o mem.o xalloc.o combin.o $fls $linpacklib $extralibs || export bstat="no"
fi

# Linking xmrcc ########################################################

tt=$rebuild
test -f xmrcc || tt="yes"
for i in `echo "xmrcc.o xlambda.o xpert.o xmem.o combin.o xalloc.o $fls"`
do
    test $i -nt xmrcc && tt="yes"
done

if [ x$tt == "xyes" ]; 
then
    if (([ "x$extralibs" != "x" ]) || ([ "x$linpacklib" != "x" ]));
    then echo "Linking xmrcc with libraries: $linpacklib $extralibs"
    else echo "Linking xmrcc"
    fi
    $fortran $compileropts $ompopt -o xmrcc xmrcc.o xlambda.o xpert.o xmem.o combin.o xalloc.o $fls $linpacklib $extralibs || export bstat="no"
fi

# Linking drpa #########################################################

tt=$rebuild
test -f drpa || tt="yes"
for i in `echo "drpa.o ldrpa.o loccis.o drpagrad.o laplace.o teint.o ecp.o dfint.o dfintloc.o df2intsubs.o hrrspher.o dfint_triplets.o brasub.o intsub.o der_interface_subs.o intsub_der.o dfint_triplets_der.o brasub_der.o ketsub_der.o hrrsub_der.o ellip.o calcorb.o mem.o xalloc.o combin.o intio.o bopu.o assembly.o oneint.o oneint_sh.o oneint_shc.o onein1.o onein1_sh.o onein1_shc.o nuceq1.o nuceq1_shc.o nucat1.o nucat1_shc.o $fls"`
do
    test $i -nt drpa && tt="yes"
done

if [ x$tt == "xyes" ];
then
    if (([ "x$extralibs" != "x" ]) || ([ "x$linpacklib" != "x" ]));
    then echo "Linking drpa with libraries: $linpacklib $extralibs"
    else echo "Linking drpa"
    fi
    $fortran $compileropts $ompopt -o drpa drpa.o ldrpa.o loccis.o drpagrad.o laplace.o teint.o ecp.o dfint.o dfintloc.o df2intsubs.o hrrspher.o dfint_triplets.o brasub.o intsub.o der_interface_subs.o intsub_der.o dfint_triplets_der.o brasub_der.o ketsub_der.o hrrsub_der.o ellip.o calcorb.o mem.o xalloc.o combin.o intio.o bopu.o assembly.o oneint.o oneint_sh.o oneint_shc.o onein1.o onein1_sh.o onein1_shc.o nuceq1.o nuceq1_shc.o nucat1.o nucat1_shc.o $fls $linpacklib $extralibs || export bstat="no"
fi

# Linking cis #########################################################

tt=$rebuild
test -f cis || tt="yes"
for i in `echo "cis.o diis.o teint.o ecp.o dfint.o dfintloc.o df2intsubs.o hrrspher.o dfint_triplets.o brasub.o intsub.o der_interface_subs.o intsub_der.o dfint_triplets_der.o brasub_der.o ketsub_der.o hrrsub_der.o ellip.o calcorb.o mem.o xalloc.o combin.o intio.o onein1.o onein1_sh.o onein1_shc.o nuceq1.o nuceq1_shc.o nucat1.o nucat1_shc.o $fls"`
do
    test $i -nt cis && tt="yes"
done

if [ x$tt == "xyes" ];
then
    if (([ "x$extralibs" != "x" ]) || ([ "x$linpacklib" != "x" ]));
    then echo "Linking cis with libraries: $linpacklib $extralibs"
    else echo "Linking cis"
    fi
    $fortran $compileropts $ompopt -o cis cis.o diis.o teint.o ecp.o dfint.o dfintloc.o df2intsubs.o hrrspher.o dfint_triplets.o brasub.o intsub.o der_interface_subs.o intsub_der.o dfint_triplets_der.o brasub_der.o ketsub_der.o hrrsub_der.o ellip.o calcorb.o mem.o xalloc.o combin.o intio.o onein1.o onein1_sh.o onein1_shc.o nuceq1.o nuceq1_shc.o nucat1.o nucat1_shc.o $fls $linpacklib $extralibs || export bstat="no"
fi

# Linking minp #########################################################

tt=$rebuild
test -f minp || tt="yes"
for i in `echo "minp.o combin.o $fls"`
do
    test $i -nt minp && tt="yes"
done

if [ x$tt == "xyes" ];
then
    if (([ "x$extralibs" != "x" ]) || ([ "x$linpacklib" != "x" ]) || ([ "x$libxclib" != "x" ]));
    then echo "Linking minp with libraries: $linpacklib $extralibs $libxclib"
    else echo "Linking minp"
    fi
    $fortran $compileropts $ompopt -o minp minp.o combin.o $fls $linpacklib $extralibs $libxclib || export bstat="no"
fi

# Linking qmmod #########################################################

tt=$rebuild
test -f qmmod || tt="yes"
for i in `echo "qmmod.o combin.o mem.o xalloc.o bopu.o $fls"`
do
    test $i -nt qmmod && tt="yes"
done

if [ x$tt == "xyes" ];
then
    if (([ "x$extralibs" != "x" ]) || ([ "x$linpacklib" != "x" ]));
    then echo "Linking qmmod with libraries: $linpacklib $extralibs"
    else echo "Linking qmmod"
    fi
    $fortran $compileropts $ompopt -o qmmod qmmod.o combin.o mem.o xalloc.o bopu.o $fls $linpacklib $extralibs || export bstat="no"
fi

# Linking dirac_mointegral_export ######################################
tt=$rebuild
test dirac_mointegral_export.F90 -nt dirac_mointegral_export && tt="yes"
if [ x$tt == "xyes" ];
then
 $fortran $compileropts -o dirac_mointegral_export dirac_mointegral_export.F90
fi
# Linking dmrcc ########################################################

tt=$rebuild
test -f dmrcc || tt="yes"
for i in `echo "dmrcc.o basopt.o geomopt.o optim.o combin.o z2c.o freqdrv.o $fls"`
do
    test $i -nt dmrcc && tt="yes"
done

if [ x$tt == "xyes" ];
then
    if (([ "x$extralibs" != "x" ]) || ([ "x$linpacklib" != "x" ]));
    then echo "Linking dmrcc with libraries: $linpacklib $extralibs"
    else echo "Linking dmrcc"
    fi
    $fortran $compileropts -o dmrcc dmrcc.o basopt.o geomopt.o optim.o combin.o z2c.o freqdrv.o $fls $linpacklib $extralibs || export bstat="no"
fi

# Check if compilation is successful ###################################

for i in $mrccexe
do
 test -f $i || export bstat="no"
done


if [ x$bstat == "xyes" ];
then
    if [ x$instdir != "x" ];
    then
     test -d $instdir || mkdir $instdir
     for i in $mrccexe
     do
      cp $i $instdir
     done
     cp -r BASIS/ MTEST/ $instdir
    fi
    echo
    echo
    echo -en "\\033[0;32m"
    echo "           MRCC has been installed successfully!            "
    echo -en "\\033[0;39m"
    echo "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
    echo
else
    echo
    echo -en "\\033[0;31m"
    echo "               Installation of MRCC failed!                 "
    echo -en "\\033[0;39m"
    echo "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
    echo
fi

exit;

