#!/bin/bash
###############################################################
# Path for external libraries used by mrcc
if [ x$LIBS_LIBXC == "x" ];
then
export LIBS_LIBXC=.
fi

###############################################################
#
# functions used in the build.mrcc script
# to setup options for the compiler systems
#
###############################################################

###############################################################
# options for the GNU compiler
# ||
# ||
# \/

f_GNU(){
echo "GNU compiler collection specified"
fortran=""
ccompiler=""

which g77 > /dev/null 2>&1 && fortran="g77"
which gfortran > /dev/null 2>&1 && fortran="gfortran"
which gcc > /dev/null 2>&1 && ccompiler="gcc"
which gfortran44 > /dev/null 2>&1 && fortran="gfortran44"
which gcc44 > /dev/null 2>&1 && ccompiler="gcc44"

##################
ccompileropts=""
if [ x$fortran == "xg77" ];
then
    if [ x$para == "xOMP" ];
    then
        f_print_error "g77" "OpenMP";
        exit;
    fi	

    if [ x$int == "xINT64" ];
    then
        f_print_error "g77" "i64";
        exit;
    fi

    if [ x$devel == "xon" ];
    then compileropts=""
    else compileropts="-O4"
    fi
else
    if [ x$para == "xOMP" ];
    then ompopt="-fopenmp"
    fi

    if [ x$para == "xMPI" ];
    then fortran="mpif90"
    fi

    if [ x$int == "xINT64" ];
    then
        intopt="-fdefault-integer-8"
        ccompileropts="-m64"
    fi

    if [ x$devel == "xon" ];
    then compileropts="-Dgfortran $intopt"
    else compileropts="-O3 -Dgfortran $intopt"
    fi
fi
#which gfortran44 > /dev/null 2>&1 && compileropts="$compileropts -DMOLPRO_BACKSPACE"
preprocopt="-x f95-cpp-input -ffree-line-length-none"
linpacklib="-llapack -lblas"
}
###############################################################
# options for the Intel compiler
# ||
# ||
# \/

f_Intel(){

echo -n "Intel Fortran Compiler"

if [ x$int == "xINT64" ];
then intopt="-i8"
fi

fortran="ifc"
if [ x$devel == "xon" ];
# -check uninit  no MKL: -lblas -llapack !!!
then compileropts="-O0 $intopt -assume byterecl"
else compileropts="-O3 $intopt -assume byterecl"
#then compileropts="-O0 $intopt -assume byterecl -openmp-link=static -static -static-intel -static-libgcc -static-libstdc++"
#else compileropts="-O3 $intopt -assume byterecl -openmp-link=static -static -static-intel -static-libgcc -static-libstdc++"
fi

preprocopt="-fpp"
oldversion=`ifc -V 2>&1| grep Version | sed "s/.* Version//g" | sed "s/\..*//g"`
if [ "x$oldversion" == "x7" ];
then
    echo " Version $oldversion"
    linpacklib="-lmkl_lapack -lmkl_p4"
    extralibs="-lIEPCF90 -lpthread -lPEPCF90 -lguide"
else
    fortran="ifort"
    version=`ifort -v 2>&1 |cut -d " " -f2|cut -d "." -f1`
    if [ $version == "version" ];
    then
     version=`ifort -v 2>&1 |cut -d " " -f3|cut -d "." -f1`
    fi
    echo " Version $version"
    extralibs="-lpthread"
    if [ x$machine == "xx86_64" ];
    then
        case "x$version" in
             "x9" )
                    ldlib="`echo $LD_LIBRARY_PATH | sed 's/:/ -L/g'`";
                    linpacklib="-L$ldlib -lmkl_lapack -lmkl -lguide";; # works with 9.1 and MKL version 9.0
            "x10" ) linpacklib="-L$MKLROOT/lib/em64t    -lmkl_lapack -lmkl_intel_ilp64 -lmkl_sequential -lmkl_core -lguide";; # works with 10.1 and MKL version 10.1
            "x11" ) linpacklib="-L$MKLROOT/lib/em64t    -lmkl_blas95_ilp64 -lmkl_lapack95_ilp64 -lmkl_intel_ilp64 -lmkl_sequential -lmkl_core -lguide";; # works with 11.1 and MKL version 10.2
            "x12" ) linpacklib="-mkl=parallel";; # works with 12.0 and MKL version 10.3
#            "x12" ) linpacklib="-L$MKLROOT/lib/intel64  -lmkl_blas95_ilp64 -lmkl_lapack95_ilp64  -Wl,--start-group  $MKLROOT/lib/intel64/libmkl_intel_ilp64.a $MKLROOT/lib/intel64/libmkl_intel_thread.a $MKLROOT/lib/intel64/libmkl_core.a -Wl,--end-group";; # works with 12.0 and MKL version 10.3
#            "x12" ) linpacklib="-L$MKLROOT/lib/intel64  -lmkl_blas95_ilp64 -lmkl_lapack95_ilp64 -lmkl_intel_ilp64 -lmkl_sequential -lmkl_core";; # works with 12.0 and MKL version 10.3
            "x13" ) linpacklib="-mkl=parallel";;
            "x14" ) linpacklib="-mkl=parallel";;
            "x15" ) linpacklib="-mkl=parallel";;
            "x16" ) linpacklib="-mkl=parallel";;
            "x17" ) linpacklib="-mkl=parallel";;
            "x18" ) linpacklib="-mkl=parallel";;
            *     )
                    linpacklib="-mkl=parallel"; 
                    echo "This intel version has not been tested!" ;;
        esac
    elif [ x$machine == "xia64" ];
        then linpacklib="-lmkl_lapack -lmkl -lmkl_ipf"
        else linpacklib="-lmkl_lapack -lmkl"
    fi

    if [ x$kernel == "xDarwin" ];
    then
        ldlib="`echo $LD_LIBRARY_PATH | sed 's/:/ -L/g'`"
        linpacklib="-L$ldlib -lmkl_intel_ilp64 -lmkl_sequential -lmkl_core"
    fi
fi

if [ x$para == "xOMP" ];
then
  if ([ x$version == "x16" ] || [ x$version == "x17" ] || [ x$version == "x18" ]);
  then
    ompopt="-qopenmp"
  else
    ompopt="-openmp"
  fi
fi

if [ x$para == "xMPI" ];
then
    fortran="mpif77"
    preprocopt="-cpp"
fi

fls="flush.o"

which icc > /dev/null 2>&1
if [ $? -eq 0 ];
then
    ccompiler="icc"
else
    ccompiler="gcc"
#   ccompiler="$fortran"
#   xalloc="xalloc.f90"
fi

#ccompileropts="$intopt"
}
###############################################################
# options for the PGF compiler
# ||
# ||
# \/

f_PGF(){

echo "Portland Group Fortran compiler selected"
fortran="pgf90"
if [ x$int == "xINT64" ];
then intopt="-i8"
fi

if [ x$para == "xOMP" ];
then ompopt="-mp"
elif [ x$para == "xMPI" ];
    then fortran="mpif77"
fi

if ([ x$machine == "xx86_64" ] || [ x$machine == "xia64" ]); 
then ccompileropts="$intopt -fPIC -Mcache_align -fastsse -Mlarge_arrays -Mipa"
else ccompileropts="$intopt -fPIC -Mcache_align -O2 -Mipa"
fi

compileropts="$ccompileropts -Mrecursive"
ccompiler=$fortran
#ccompiler=pgcc
preprocopt="-Mpreprocess"
linpacklib="-lacml"
}
###############################################################
# options for the G95 compiler
# ||
# ||
# \/

f_G95(){

echo "G95 Fortran 95 compiler selected"
fortran="g95"
ccompiler=gcc
ccompileropts="-fPIC -m32"

if [ x$int == "xINT64" ];
then
    intopt="-i8"
    ccompileropts="-fPIC -m64"
fi

compileropts="$intopt -fno-second-underscore -w -fsloppy-char"
if [ x$devel == "xon" ];
then compileropts="-O0 $compileropts -ftrace=full"
else compileropts="-O3 $compileropts"
fi

preprocopt="-cpp"
#extralibs="-lguide -lpthread"
if [ x$machine == "xx86_64" ];
then linpacklib="-lmkl_lapack -lmkl"
elif [ x$machine == "xia64" ];
    then linpacklib="-lmkl_lapack -lmkl -lmkl_ipf"
    else linpacklib="-lmkl_lapack -lmkl"
fi
#linpacklib="-lblas -llapack"
fls="flush.o"
}
###############################################################
# options for the PATHSCALE compiler
# ||
# ||
# \/

f_PATH(){

echo "Pathscale compiler collection specified"
fortran="pathf90"
if [ x$int == "xINT64" ];
then intopt="-i8"
fi

if [ x$para == "xOMP" ];
then ompopt="-intrinsic=OMP -mp"
fi

if [ x$devel == "xon" ];
then compileropts="-w $intopt"
else compileropts="-O3 -w $intopt"
fi

# ccompiler="pcc"
ccompiler="pathcc"
# ccompiler="$fortran"
ccompileropts="$compileropts"
preprocopt="-ftpp"
linpacklib="-llapack -lblas"
# xalloc="xalloc.f90"
}
###############################################################
# options for the HP compiler
# ||
# ||
# \/

f_HP(){

echo "HP Fortran Compiler selected"
if [ x$int == "xINT64" ];
then
    intopt="+DD64 +i8"
    linpacklib="-L/opt/mlib/lib/hpux64 -llapack8 -lveclib8"
else linpacklib="-L/opt/mlib/lib/hpux -llapack -lveclib"
fi

if [ x$para == "xOMP" ];
then ompopt="+Oopenmp"
else ompopt="+Onoopenmp"
fi

fortran="f90"
compileropts="$intopt +U77 +O2"
ccompiler=$fortran
ccompileropts="$compileropts"
preprocopt="+cpp=yes"
xalloc="xalloc.f90"
}
###############################################################
# options for the COMPAQ compiler
# ||
# ||
# \/

f_DEC(){

echo "Compaq Fortran Compiler selected"
fortran="f77"
compileropts="-O4"
ccompiler=$fortran
ccompileropts=$compileropts
preprocopt="-cpp"
linpacklib="-ldxml"
}
###############################################################
# options for the XL compiler
# ||
# ||
# \/

f_XLF(){

echo "XL Fortran Compiler selected"
fortran="xlf"
compileropts="-O4"
ccompiler="xlc"
ccompileropts=$compileropts
preprocopt="-P"
linpacklib="-lblas -L/usr/prog/Columbus/Columbus.5.6 -llapack"
fls="flush.o"
}
###############################################################
# options for the SUN compiler
# ||
# ||
# \/

f_Solaris10(){

echo "Sun Solaris10 + Studio10 Fortran Compiler selected (AMD64)"
fortran="f95"
ccompiler="cc"
if [ x$para == "XMPI" ];
then
    fortran="mpif77"
    ccompiler="mpicc"
fi

compileropts="-w -xtypemap=real:64,double:64,integer:64 -O2 -unroll=6 -xtarget=opteron -xarch=amd64 -stackvar -xrecursive"
ccompileropts="-w -xtarget=opteron -xarch=amd64"
preprocopt="-fpp"
linpacklib="-xlic_lib=sunperf"
}
###############################################################
# evident
# ||
# ||
# \/

f_Unknown(){

echo "Unknown compiler system specified"
exit;
}

