#!/bin/bash
#set -vx
# ================================================================================================================================
#  compile_orchidee_ol.sh
#
#  CONTACT      : orchidee-help _at_ listes.ipsl.fr
#
#  LICENCE      : IPSL (2006)
#  This software is governed by the CeCILL licence see ORCHIDEE/ORCHIDEE_CeCILL.LIC
#
#  DESCRIPTION	: This script is used to compile all components needed for the ORCHIDEE offline drivers. The following components 
#                 will be compiled: IOIPSL, XIOS and ORCHIDEE.
#                 Default compilation is set up for the machines irene/TGCC, jean-zay/IDRIS, obelix/LSCE and ciclad and 
#                 climserv/IPSL. For other machines compiling option needs to be specified for each of the components. 
#                 The script first sources a arch.env file, from folder ARCH, containing modules needed for the current machine. 
#                 This means that the modules do not need to be sourced before starting the compilation. Output from compilation 
#                 are stored in a separate file out_compile_orchidee.date. 
#                 For help about the script, use: ./compile_orchidee_ol.sh -h
#
# $HeadURL$
# $Date$
# $Revision$
# ================================================================================================================================

date

#### 1  Set up the compiling options
#### Define some directories
submitdir=$( pwd )
modipsl=$submitdir/../..
arch_path=$submitdir/ARCH 

#### Set default options 
# Optimization mode
# optmode=prod/dev/debug
optmode=prod

# fcm_arch
fcm_arch=default

# parallel: mpi or seq
parallel=mpi

export fcm_arch parallel xios
full_flag=""
full_xios=""
full_orch=""

# Compile with linking to XIOS: xios=xios (default)
# Use argument -without_xios for compilation without linking to XIOS: xios=noxios
xios=xios

# Compile other executables: if otherexec="-otherexec", then 
otherexec=""

# Default netcdf_lib is used for XIOS but can be change by argument
netcdf_lib=""


#### Read arguments 
# Loop over all arguments to modify default set up
while (($# > 0)) ; do
    case $1 in
	"-h") cat <<end_help
########################################################################
# Usage of the script compile_orchidee_ol.sh
#
########################################################################

./compile_orchidee_ol.sh [Options] 

Options: 
  [-full]: full recompilation of all components 
  [-full_orch], [-full_xios]: recompile only orchidee or xios component
  [-arch XXX]: Force to use arch-XXX.fcm and arch-XXX.path files with specific compilation options for current platform. The files must be 
   available in modles/ORCHIDEE/arch, modeles/XIOS/arch and modeles/IOIPSL/arch folders. If the file config/ORCHIDEE_OL/ARCH/arch-XXX.env 
   exist, it'll be sourced in the beginning of this compilation script.
  [-debug], [-dev], [-prod](default): use compilation options for debug, dev or production mode, only one option can be used
  [-all_exec]: also compile other exectables than the drivers (testrouting, forcesoil and teststomate)
  [-without_xios]: no compilation or linking to XIOS. Default compilation is done with XIOS
  [-parallel seq] / [-parallel mpi](default): compile without MPI for use in sequential mode / with MPI (default). 
   When using [-parallel seq], [-without_xios] must as well be set.
  [-netcdf_lib_seq]: option to tell XIOS that linking with netcdf4 sequential library will be used instead of default netcdf4 parallel library


Example 1: Default compilation of ORCHIDEE with XIOS and IOIPSL
./compile_orchidee_ol.sh

Example 2: Compile in debug mode
./compile_orchidee_ol.sh -debug

Example 3: Default compilation with full recompilation of all components
./compile_orchidee_ol.sh -full

Example 4: No compilation or linking to XIOS
./compile_orchidee_ol.sh -without_xios

Example 5: Compilation without MPI for sequential use only. Note that XIOS must be deactivated as well.
./compile_orchidee_ol.sh -parallel seq -without_xios


end_help
exit;;
	"-parallel")        parallel="$2" ; shift ; shift ;; 
	"-arch")	    fcm_arch="$2" ; shift ; shift ;;
	"-without_xios")    xios=noxios ; shift ;;
	"-debug")	    optmode=debug ; shift ;;
	"-dev") 	    optmode=dev ; shift ;;
	"-prod")	    optmode=prod ; shift ;;
	"-full")            full_flag="-full"; full_xios="--full" ; full_orch="-full" ; shift ;;
	"-full_xios")       full_xios="--full" ; shift ;;  # Note only full_xios is using double dash: --full
	"-full_orch")       full_orch="-full"  ; shift ;;
	"-all_exec")        otherexec="-otherexec" ; shift ;;
	"-netcdf_lib_seq")  netcdf_lib="--netcdf_lib netcdf4_seq"; shift ;;
	*)	            echo "unknown option "$1" , exiting..." ; exit 
    esac
done


# Create text output file
datestr=`LC_ALL=C date +"%Y%m%dT%H%M"`
outfile=$submitdir/out_compile_orchidee.$datestr
echo > $outfile
echo; echo "Text output from compilation will be stored in file out_compile_orchidee.$datestr"; echo 


echo "Following options are set in current compiling:" >> $outfile 
echo "   optmode = $optmode, parallel = $parallel, fcm_arch = $fcm_arch " >> $outfile 
echo "   full_flag=$full_flag, full_xios=$full_xios, full_orch=$full_orch" >> $outfile 
echo >> $outfile

if [ $parallel = seq ] && [ $xios = y ] ; then
    echo "It is not possible to compile ORCHIDEE in sequential mode using XIOS."
    echo "Option -without_xios must be set if option [-parallel seq] is set"
    echo "Option -without_xios must be set if option [-parallel seq] is set" >> $outfile  
    exit
fi

### Read host dependent default values
### These variables will not be changed if they were set as argument
###./host.sh $host
# Later : Following lines should be set in host.sh file
# begin host.sh
if [ $fcm_arch == default ] ; then
    # Find out current host and source specific paths and commands for the host
    case $( hostname -s ) in
	jean-zay*)
	    fcm_arch=X64_JEANZAY;;
        irene170|irene171|irene190|irene191|irene192|irene193)
            fcm_arch=X64_IRENE;;
        irene172|irene173|irene194|irene195)
            fcm_arch=X64_IRENE-AMD;;
	asterix*|obelix*)
	    fcm_arch=ifort_LSCE;;
	ciclad*|climserv*|loholt*|camelot*|merlin*)
	    fcm_arch=ifort_CICLAD;;
	spirit*)
	    fcm_arch=ifort_MESOIPSL;;
	*)
	    echo Current host is not known. You must use option -arch to specify which architecuture files to use.
	    echo Exit now.
	    exit
    esac
fi

echo "The fcm_arch has been changed to: $fcm_arch" >> $outfile 

# Set a link to arch.env if arch-${fcm_arch}.env file exist for current fcm_arch.
# The link arch.env is also set in config.card and will be used by libIGCM to ensure the same running environnement.
if [ -f ARCH/arch-${fcm_arch}.env ] ; then
    echo >> $outfile
    echo "The file ARCH/arch-${fcm_arch}.env will now be sourced with modules needed for compilation for all components."
    echo "Note that this new environement might be kept after compilation." 
    echo "If this is the case, source again your personal environment after compilation. "

    # Make a link to this file, to be used also in config.card
    rm -f ARCH/arch.env
    ln -s arch-${fcm_arch}.env ARCH/arch.env

    # Source the file
    source ARCH/arch.env   >> $outfile 2>&1 
    echo >> $outfile 
    echo "New module list after sourcing of ARCH/arch.env file:"    >> $outfile  
    module list   >> $outfile 2>&1 
else
    echo "No specific ARCH/arch-${fcm_arch}.env have been found. No change in environnement will be done."
    echo "Previous loaded module modules will be used."    >> $outfile  
    module list   >> $outfile 2>&1 
fi


#### 2 Do the compilation component by component
## 2.1 Compile ioipsl
cd $modipsl/modeles/IOIPSL
echo; echo "NOW COMPILE IOIPSL"
echo >> $outfile ; echo " NOW COMPILE IOIPSL"   >> $outfile 

# Check if compilation with fcm is included in IOIPSL
if [ ! -f makeioipsl_fcm ] ; then
  echo "The file makeiopsl_fcm do not exist. Probably this is not the right version of IOIPSL."
  echo "Version of IOIPSL with fcm compilation is needed to compile with this script. Stop now."
  exit
fi

echo ./makeioipsl_fcm -$optmode -arch ${fcm_arch} -arch_path $arch_path -j 8 $full_flag   >> $outfile 
     ./makeioipsl_fcm -$optmode -arch ${fcm_arch} -arch_path $arch_path -j 8 $full_flag   >> $outfile 2>&1
# Test if compiling succeded 
if [[ $? != 0 ]] ; then 
    echo "THERE IS A PROBLEM IN IOIPSL COMPILATION - STOP"
    exit
fi

## 2.2 Compile xios
if [ $xios == xios ] ; then
    cd $modipsl/modeles/XIOS
    echo; echo "NOW COMPILE XIOS"
    echo >> $outfile ; echo " NOW COMPILE XIOS"   >> $outfile 
    echo ./make_xios --$optmode --arch $fcm_arch --arch_path $arch_path ${netcdf_lib} --job 4 $full_xios   >> $outfile 
    ./make_xios --$optmode --arch $fcm_arch --arch_path $arch_path ${netcdf_lib} --job 4 $full_xios   >> $outfile 2>&1
    # Test if compiling succeded 
    if [[ $? != 0 ]] ; then 
	echo "THERE IS A PROBLEM IN XIOS COMPILATION - STOP"
	exit
    fi
    # Move executables to modipsl/bin
    if [ -f $modipsl/modeles/XIOS/bin/xios_server.exe ] ; then 
	mv $modipsl/modeles/XIOS/bin/xios_server.exe $modipsl/bin/xios_server_${optmode}.exe
    else
	echo "THERE IS A PROBLEM IN XIOS COMPILATION EXECUTABLE MISSING - STOP"
	exit
    fi
fi


## 2.3 Compile orchidee
cd $modipsl/modeles/ORCHIDEE
echo; echo "NOW COMPILE ORCHIDEE"
echo >> $outfile ; echo " NOW COMPILE ORCHIDEE"   >> $outfile 

# Check if the compilation of ORCHIDEE was previsouly interupted prematured. 
# In that case, the files fcm.bld.lock exist. If the file exist, it is here removed to avoid makeorchidee_fcm 
# to ask question and wait for interactivly answer from the user.
if [ -f build/fcm.bld.lock ] ; then
    echo >> $outfile
    echo " WARNING!! The file ORCHIDEE/build/fcm.bld.lock exist. "   >> $outfile
    echo "           This means that the compilation is either currently on going in another terminal was previous interupted before the end."   >> $outfile
    echo "           The file will now be removed. "   >> $outfile
    echo >> $outfile
    rm -f build/fcm.bld.lock
fi

echo 	./makeorchidee_fcm -j 8 -$xios -parallel $parallel -$optmode -arch ${fcm_arch} -arch_path $arch_path $full_orch -driver $otherexec   >> $outfile 
        ./makeorchidee_fcm -j 8 -$xios -parallel $parallel -$optmode -arch ${fcm_arch} -arch_path $arch_path $full_orch -driver $otherexec   >> $outfile 2>&1
# Test if compiling finished
if [[ $? != 0 ]] ; then
    echo "THERE IS A PROBLEM IN ORCHIDEE COMPILATION - STOP"
    exit
fi

# Rename executables to contain $optmode
if [ -f $modipsl/bin/orchidee_ol ] ; then mv $modipsl/bin/orchidee_ol  $modipsl/bin/orchidee_ol_${optmode} ; fi
if [ -f $modipsl/bin/orchideedriver ] ; then mv $modipsl/bin/orchideedriver  $modipsl/bin/orchideedriver_${optmode} ; fi
if [ -f $modipsl/bin/testrouting ] ; then mv $modipsl/bin/testrouting  $modipsl/bin/testrouting_${optmode} ; fi
if [ -f $modipsl/bin/forcesoil ] ; then mv $modipsl/bin/forcesoil  $modipsl/bin/forcesoil_${optmode} ; fi
if [ -f $modipsl/bin/teststomate ] ; then mv $modipsl/bin/teststomate  $modipsl/bin/teststomate_${optmode} ; fi



echo >>$outfile ; echo "ALL COMPILING FINISHED" >> $outfile
echo ls -lrt modipsl/bin >> $outfile
ls -lrt $modipsl/bin >> $outfile

echo; echo "ALL COMPILING FINISHED" ; echo
echo "Executables are found in modipsl/bin"
echo ls -lrt modipsl/bin
ls -lrt $modipsl/bin

date

exit

