#!/bin/bash
#set -vx
# Default options
#
#
#
date
#### 1  Set up the compiling options
#### Define some directories
submitdir=$( pwd )
modipsl=$submitdir/../..
arch_path=$submitdir/ARCH 
export ROOT=$modipsl/modeles/DYNAMICO

mysrc_path=$submitdir/SOURCES
sources_RH8_path=$submitdir/SOURCES/REDHAT8

#### Set default options 
# Optimization mode
# optmode=prod/dev/debug
optmode=prod
# Resolution if compiling LMDZ in regular mode without DYNAMICO
# Use for example following "./compile_icolmdzorinca.sh -regular_latlon 144x142x79"
regular_latlon=no
# fcm_arch
fcm_arch=default
# Default values to be overritten
parallel=mpi_omp
export fcm_arch parallel xios
full_flag=""
full_xios=""
full_lmdz=""
full_orch=""
full_dyna=""
ico_only=no
reg_only=no
ce0l=no
optchimie=GES
resol_atm="144x142x79"

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

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

#### 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_icolmdzorinca.sh
#
########################################################################

./compile_config [Options] 

Options: -full, -regular_latlon, -debug, -dev, -prod(default)

Example 1: Default compilation of DYNAMICO-LMDZ-ORCHIDEE with XIOS and IOIPSL
./compile_icolmdzorinca.sh

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

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

Example 4: Compilation of LMDZ in regular lat-lon for dimension 144x142x79. 
The dimension can be changed to any other 3d dimension. DYNAMICO is also compiled as default.
./compile_icolmdzorinca.sh -regular_latlon 144x142x79

Example 5: Compilation with a chosen chemistry, for example DUSS  (default: NMHC_AER_S)
./compile_icolmdzorinca.sh -chimie DUSS 

Example 6: Compile only ico grid 
./compile_icolmdzorinca.sh -ico_only

Example 7: Compile only reg grid 
./compile_icolmdzorinca.sh -reg_only

Example 8 : compile create_etat0_limit (and gcm.e) 
./compile_icolmdzorinca.sh -ce0l

end_help
	    exit;;

	"-parallel") 	    parallel=$2 ; shift ; shift ;; 
	"-arch")	    fcm_arch="$2" ; shift ; shift ;;
	"-xios")	    xios="$2" ; shift ; shift ;;
	"-debug")	    optmode=debug ; shift ;;
	"-dev")	            optmode=dev ; shift ;;
	"-prod")	    optmode=prod ; shift ;;
	"-regular_latlon")  regular_latlon=yes ; resol_atm=$2 ; shift ; shift ;;
	"-full")            full_flag="-full"; full_xios="--full" ; full_lmdz="-full" ; full_orch="-full" ; full_dyna="-full" ; shift ;;
	"-full_xios")       full_xios="--full" ; shift ;;  # Note only full_xios is using double dash: --full
	"-full_lmdz")       full_lmdz="-full"  ; shift ;;
	"-full_orch")       full_orch="-full"  ; shift ;;
	"-full_dyna")       full_dyna="-full"  ; shift ;;
	"-ico_only")        ico_only=yes  ; shift ;;
	"-reg_only")        reg_only=yes  ; shift ;;
	"-ce0l")            ce0l=yes  ; shift ;;
        "-netcdf_lib_seq")  netcdf_lib="--netcdf_lib netcdf4_seq"; shift ;;
	"-chimie") 	    optchimie=$2; shift ; shift;;

	*)       	    echo "unknown option "$1" , exiting..." ;  exit 
    esac
done

echo "Following options are set in current compiling:" >> $outfile 
echo "   regular_latlon=$regular_latlon (if yes, then resol_atm=${resol_atm})" >> $outfile 
echo "   optmode = $optmode, parallel = $parallel, fcm_arch = $fcm_arch " >> $outfile 
echo "   full_flag=$full_flag, full_xios=$full_xios, full_lmdz=$full_lmdz, full_orch=$full_orch, full_dyna=$full_dyna" >> $outfile 
echo >> $outfile

### 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
            if [ "${parallel}" == "mpi_omp" ] ; then 
		echo "Warning!! Currently at Obelix hybrid mode is not possible."
		echo "          Option -parallel mpi_omp is now changed to -parallel mpi"
		echo ""
		parallel=mpi
            else
		echo "You used -parallel" $parallel
            fi ;;
	ciclad*|climserv*|loholt*|camelot*)
	    fcm_arch=ifort_CICLAD;;
	*)
	    echo Current host is not known. You must use option -arch to specify which architecuture files to use.
	    echo Exit now.
	    exit
    esac
fi

# 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."
    echo " Personal module list before sourcing of ARCH/arch.env file:"    >> $outfile 
    module list   >> $outfile 2>&1

    # 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
fi

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

echo ./makeioipsl_fcm -$optmode -parallel -arch ${fcm_arch} -arch_path $arch_path -j 8 $full_flag   >> $outfile 
     ./makeioipsl_fcm -$optmode -parallel -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"
    echo "ALL INFORMATION IN FILE $outfile"
    exit
fi

## 2.2 Compile xios
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"
    echo "ALL INFORMATION IN FILE $outfile"
    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"
    echo "ALL INFORMATION IN FILE $outfile"
    exit
fi


## 2.3 Compile orchidee
cd $modipsl/modeles/ORCHIDEE

cp ${sources_RH8_path}/ORCHIDEE/mod_orchidee_para.F90 src_parallel/.

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

# If orchide driver executables have been compiled (option -driver), then 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


## ajout compile inca Anne 
    cd $modipsl/modeles/INCA


    echo; echo "NOW COMPILE INCA ${optchimie}"
    echo ./makeinca_fcm -chimie ${optchimie} -$optmode -xios -parallel mpi_omp -j 8 -arch ${fcm_arch}  -arch_path $arch_path $full_flag  >> $outfile
    ./makeinca_fcm -chimie ${optchimie} -$optmode -xios -parallel mpi_omp  -j 8 -arch ${fcm_arch} -arch_path $arch_path  $full_flag >> $outfile 2>&1

    # Test if compiling finished
    if [[ $? != 0 ]] ; then
	echo "THERE IS A PROBLEM IN INCA COMPILATION - STOP"
	echo "ALL INFORMATION IN FILE $outfile"
	exit
    fi



    echo "Move inca.dat modipsl/bin"
    if [[ -f $modipsl/modeles/INCA/SIMULATIONS/${optchimie}/inca.dat ]] ;  then  
	mv $modipsl/modeles/INCA/SIMULATIONS/${optchimie}/inca.dat $modipsl/bin/inca_${optmode}_${optchimie}.dat ;
    else
	echo "THERE IS A PROBLEM IN INCA COMPILATION - STOP"
	echo "ALL INFORMATION IN FILE $outfile"
	exit	
    fi

    echo "Move tracer.def to  modipsl/bin"
    if [[ -f $modipsl/modeles/INCA/tracer.def ]] ;  then
        mv $modipsl/modeles/INCA/tracer.def $modipsl/bin/tracer_${optmode}_${optchimie}.def ;
    fi


## 2.4 Compile lmdz
cd $modipsl/modeles/LMDZ


if [ $reg_only = no ] ; then 

    # Compile LMDZ as library to couple to DYNAMICO
    echo; echo "NOW COMPILE LMDZ FOR COUPLING TO DYNAMICO"
    echo >> $outfile ; echo " NOW COMPILE LMDZ FOR COUPLING TO DYNAMICO"   >> $outfile 

    # Check if the compilation of LMDZ was previsouly interupted prematured. 
    # In that case, the files dimension.h and .lock exist. If the files exist, they are here removed to avoid makelmdz_fcm 
    # to ask question and wait for interactivly answer from the user.
    if [ -f libf/grid/dimensions.h ] || [ -f .lock ] ; then
	echo >> $outfile
	echo " WARNING!! The file LMDZ/libf/grid/dimension.h and/or the LMDZ/.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 files dimension.h and .lock will now be removed. "   >> $outfile
	echo >> $outfile
	rm -f libf/grid/dimensions.h
	rm -f .lock
    fi

    echo ./makelmdz_fcm -p lmd -rrtm true -cosp true -$optmode -mem -parallel $parallel -libphy -v orchidee2.1 -chimie INCA -io xios -arch $fcm_arch -j 8 $full_lmdz    >> $outfile 
    ./makelmdz_fcm -p lmd -rrtm true -cosp true -$optmode -mem -parallel $parallel -libphy -v orchidee2.1 -chimie INCA -io xios -arch $fcm_arch -j 8 $full_lmdz    >> $outfile 2>&1
    # Test if compiling finished
    if [[ $? != 0 ]] ; then
	echo "THERE IS A PROBLEM IN LMDZ PHYSICS COMPILATION - STOP"
	echo "ALL INFORMATION IN FILE $outfile"
	exit
    fi


## 2.5 Compile DYNAMICO
    cd $modipsl/modeles/DYNAMICO
    echo; echo "NOW COMPILE DYNAMICO "
    echo >> $outfile ; echo " NOW COMPILE DYNAMICO"   >> $outfile 

    echo ./make_icosa -$optmode -parallel $parallel -external_ioipsl -with_xios -arch $fcm_arch -arch_path $arch_path -job 8 $full_dyna    >> $outfile 
    ./make_icosa -$optmode -parallel $parallel -external_ioipsl -with_xios -arch $fcm_arch -arch_path $arch_path -job 8 $full_dyna    >> $outfile 2>&1
    # Test if compiling finished
    if [[ $? != 0 ]] ; then
	echo "THERE IS A PROBLEM IN DYNAMICO COMPILATION - STOP"
	echo "ALL INFORMATION IN FILE $outfile"
	exit
    fi

    ## 2.6 Compile interface ICOSA_LMDZ
    cd $modipsl/modeles/ICOSA_LMDZ
    echo; echo "NOW COMPILE ICOSA_LMDZ "
    echo >> $outfile ; echo " NOW COMPILE ICOSA_LMDZ"   >> $outfile 

    echo ./make_icosa_lmdz -nodeps -p lmd -$optmode -parallel $parallel -with_orchidee -with_inca -arch ${fcm_arch} -arch_path ${arch_path} -job 8 $full_dyna    >> $outfile 
    ./make_icosa_lmdz -nodeps -p lmd -$optmode -parallel $parallel -with_orchidee -with_inca -arch ${fcm_arch} -arch_path ${arch_path} -job 8 $full_dyna    >> $outfile 2>&1
    # Test if compiling finished
    if [[ $? != 0 ]] ; then
	echo "THERE IS A PROBLEM IN ICOSA_LMDZ COMPILATION - STOP"
	echo "ALL INFORMATION IN FILE $outfile"
	exit
    fi
    # Move executables to modipsl/bin
    if [ $modipsl/modeles/ICOSA_LMDZ/bin/icosa_lmdz.exe ] ; then 
	mv $modipsl/modeles/ICOSA_LMDZ/bin/icosa_lmdz.exe $modipsl/bin/icosa_lmdz_${optmode}_${optchimie}.exe
    else
	echo "THERE IS A PROBLEM IN ICOSA_LMDZ COMPILATION EXECUTABLE MISSING - STOP"
	echo "ALL INFORMATION IN FILE $outfile"
	exit
    fi
fi 


## 2.7 Compile LMDZ for regular latlon configuration

if [ $ico_only = no ] ; then 
    cd $modipsl/modeles/LMDZ
    # Compile LMDZ regular lat-lon exectuable
    echo; echo "NOW COMPILE LMDZ REGULAR LAT-LON MODE. Resolution = ${resol_atm}"
    echo >> $outfile ; echo " NOW COMPILE LMDZ REGULAR LAT-LON MODE. Resolution = ${resol_atm}"   >> $outfile 
    
    # Check if the compilation of LMDZ was previsouly interupted prematured. 
    # In that case, the files dimension.h and .lock exist. If the files exist, they are here removed to avoid makelmdz_fcm 
    # to ask question and wait for interactivly answer from the user.
    if [ -f libf/grid/dimensions.h ] || [ -f .lock ] ; then
	echo >> $outfile
	echo " WARNING!! The file LMDZ/libf/grid/dimension.h and/or the LMDZ/.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 files dimension.h and .lock will now be removed. "   >> $outfile
	echo >> $outfile
	rm -f libf/grid/dimensions.h
	rm -f .lock
    fi
    echo ./makelmdz_fcm -d ${resol_atm} -p lmd -rrtm true -$optmode -mem -parallel $parallel -io xios -v orchidee2.1 -chimie INCA -arch $fcm_arch -j 8 $full_lmdz gcm    >> $outfile 
    ./makelmdz_fcm -d ${resol_atm} -p lmd -rrtm true -$optmode -mem -parallel $parallel -io xios -v orchidee2.1 -chimie INCA -arch $fcm_arch -j 8 $full_lmdz gcm    >> $outfile 2>&1
    # Test if compiling finished
    if [[ $? != 0 ]] ; then
	echo "THERE IS A PROBLEM IN LMDZ REGULAR LATLON COMPILATION - STOP"
	echo "ALL INFORMATION IN FILE $outfile"
	exit
    fi
fi 


if [ $ce0l = yes ] ; then 
    # Compile ce0l initialization program for LMDZ regular lat-lon exectuable
    echo; echo "NOW COMPILE CE0L OF LMDZ. Resolution = ${resol_atm}"
    echo >> $outfile ; echo " NOW COMPILE CE0L OF LMDZ. Resolution = ${resol_atm}"   >> $outfile 
    
    echo ./makelmdz_fcm -d ${resol_atm} -p lmd -rrtm true -$optmode -mem -parallel $parallel -io xios -v orchidee2.1 -chimie INCA -arch $fcm_arch -j 8 $full_lmdz ce0l    >> $outfile 
    ./makelmdz_fcm -d ${resol_atm} -p lmd -rrtm true -$optmode -mem -parallel $parallel -io xios -v orchidee2.1 -chimie INCA -arch $fcm_arch -j 8 $full_lmdz ce0l    >> $outfile 2>&1
    # Test if compiling finished
    if [[ $? != 0 ]] ; then
	echo "THERE IS A PROBLEM IN CE0L (LMDZ) REGULAR LATLON COMPILATION - STOP"
	echo "ALL INFORMATION IN FILE $outfile"
	exit
    fi
fi 

# Find executable suffix
if [ $parallel == seq ] || [ $parallel == none ] ; then
    suffix=_${resol_atm}_phylmd_seq_orch_inca.e
else
    suffix=_${resol_atm}_phylmd_para_mem_orch_inca.e
fi
echo gcm suffix = $suffix

if [ $ico_only = no ] ; then 
    # Move executables to modipsl/bin folder
    echo "Move gcm.e and ce0l executable to modipsl/bin"
    if [ -f $modipsl/modeles/LMDZ/bin/gcm${suffix} ] ;  then
	mv $modipsl/modeles/LMDZ/bin/gcm${suffix} $modipsl/bin/gcm_${resol_atm}_${optmode}_${optchimie}.e
    else
	echo "ERROR gcm${suffix} executable does not exist." 
	echo "THERE IS A PROBLEM TO MOVE LMDZ REGULAR LATLON EXECUTABLE - STOP"
	echo "ALL INFORMATION IN FILE $outfile"
	exit
    fi
fi 

if [ $ce0l = yes ] ; then 

    if [ -f $modipsl/modeles/LMDZ/bin/ce0l${suffix} ] ;  then
	mv $modipsl/modeles/LMDZ/bin/ce0l${suffix} $modipsl/bin/ce0l_${resol_atm}_${optmode}_${optchimie}.e
    else
	echo "ERROR ce0l${suffix} executable does not exist." 
	echo "THERE IS A PROBLEM IN LMDZ REGULAR LATLON COMPILATION - STOP"
	echo "ALL INFORMATION IN FILE $outfile"
	exit
    fi
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 "Check that executable names correspond with the name set in config.card before launching the job"
echo ls -lrt modipsl/bin
ls -lrt $modipsl/bin

date
exit


