#!/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

#### 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_icolmdzor.sh -regular_lonlat 144x142x79"
regular_latlon=no
# fcm_arch
fcm_arch=default
# Default values to be overritten
parallel=mpi_omp
export fcm_arch parallel xios
full_flag=""
# Default netcdf_lib is used for XIOS but can be change by argument
netcdf_lib=""

# Output text file for compilation of each component
outfile=$submitdir/out_compile_icolmdzor
rm -f $outfile; echo > $outfile
echo; echo "Text output from compilation will be stored in file out_compile_icolmdzor"; echo 

#### Read arguments 
# Loop over all arguments to modify default set up
while (($# > 0)) ; do
    case $1 in
	"-h") cat <<fin

########################################################################
# Usage of the script compile_icolmdzor.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_icolmdzor.sh

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

Example 3: Default compilation with full recompilation of all components
./compile_icolmdzor.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_icolmdzor.sh -regular_latlon 144x142x79

fin
	    exit;;

	"-parallel")
	    parallel=$2 ; shift ; shift ;; 
	
	"-arch")
	    fcm_arch="$2" ; shift ; shift ;;
	
	"-xios")
	    xios="$2" ; shift ; shift ;;
	
	"-optmode")
	    optmode=$2 ; shift ; shift ;;

	"-debug")
	    optmode=debug ; shift ;;

	"-dev")
	    optmode=dev ; shift ;;

	"-prod")
	    optmode=prod ; shift ;;

	"-regular_latlon")
	    regular_latlon=yes ;
	    resol_atm_3d=$2 ; shift ; shift ;;
	
	"-full")
	    full_flag="-full"; shift ;;
	
        "-netcdf_lib_seq")
            netcdf_lib="--netcdf_lib netcdf4_seq"; shift ;;

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

echo "Following arguments are set in current compiling:" >> $outfile 
echo "   optmode = $optmode, parallel = $parallel, fcm_arch = $fcm_arch, full_flag=$full_flag regular_latlon=$regular_latlon" >> $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
	ada*)
	    fcm_arch=X64_ADA;;
	irene*)
	    fcm_arch=X64_IRENE;;
	asterix*|obelix*)
	    fcm_arch=ifort_LSCE;;
	ciclad*|climserv*)
	    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 and source the file 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."
    echo "Note that this new environement will be kept after compilation."
    echo "You might want to source again your personal environment after compilation. This must be done manually or by reconnection."
    echo " Personal module list before sourcing of ARCH/arch.env file:"    >> $outfile 
    module list   >> $outfile 2>&1

    rm -f ARCH/arch.env
    ln -s arch-${fcm_arch}.env ARCH/arch.env
    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"
    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_flag   >> $outfile 
     ./make_xios --$optmode --arch $fcm_arch --arch_path $arch_path ${netcdf_lib} --job 4 $full_flag   >> $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 [ $modipsl/modeles/XIOS/bin/xios_server.exe ] ; then 
    mv $modipsl/modeles/XIOS/bin/xios_server.exe $modipsl/bin/.
else
    echo "THERE IS A PROBLEM IN XIOS COMPILATION EXECUTABLE MISSING - STOP"
    exit
fi


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

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

## 2.4 Compile lmdz
cd $modipsl/modeles/LMDZ
# 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 

echo ./makelmdz_fcm -p lmd -rrtm true -cosp true -$optmode -mem -parallel $parallel -libphy -v orchidee2.1 -io xios -arch $fcm_arch -j 8 $full_flag    >> $outfile 
     ./makelmdz_fcm -p lmd -rrtm true -cosp true -$optmode -mem -parallel $parallel -libphy -v orchidee2.1 -io xios -arch $fcm_arch -j 8 $full_flag    >> $outfile 2>&1
# Test if compiling finished
if [[ $? != 0 ]] ; then
    echo "THERE IS A PROBLEM IN LMDZ PHYSICS COMPILATION - STOP"
    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_flag    >> $outfile 
     ./make_icosa -$optmode -parallel $parallel -external_ioipsl -with_xios -arch $fcm_arch -arch_path $arch_path -job 8 $full_flag    >> $outfile 2>&1
# Test if compiling finished
if [[ $? != 0 ]] ; then
    echo "THERE IS A PROBLEM IN DYNAMICO COMPILATION - STOP"
    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 -arch ${fcm_arch} -arch_path ${arch_path} -job 8 $full_flag    >> $outfile 
     ./make_icosa_lmdz -nodeps -p lmd -$optmode -parallel $parallel -with_orchidee -arch ${fcm_arch} -arch_path ${arch_path} -job 8 $full_flag    >> $outfile 2>&1
# Test if compiling finished
if [[ $? != 0 ]] ; then
    echo "THERE IS A PROBLEM IN ICOSA_LMDZ COMPILATION - STOP"
    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/. 
else
    echo "THERE IS A PROBLEM IN ICOSA_LMDZ COMPILATION EXECUTABLE MISSING - STOP"
    exit
fi



## 2.7 Compile LMDZ for regular latlon configuration
if [ $regular_latlon = yes ] ; then

    cd $modipsl/modeles/LMDZ
    # Compile LMDZ regular lat-lon exectuable
    echo; echo "NOW COMPILE LMDZ REGULAR LAT-LON MODE. Resolution = ${resol_atm_3d}"
    echo >> $outfile ; echo " NOW COMPILE LMDZ REGULAR LAT-LON MODE. Resolution = ${resol_atm_3d}"   >> $outfile 

    echo ./makelmdz_fcm -d ${resol_atm_3d} -p lmd -rrtm true -$optmode -mem -parallel $parallel -io xios -v orchidee2.1 -arch $fcm_arch -j 8 $full_flag gcm    >> $outfile 
         ./makelmdz_fcm -d ${resol_atm_3d} -p lmd -rrtm true -$optmode -mem -parallel $parallel -io xios -v orchidee2.1 -arch $fcm_arch -j 8 $full_flag gcm    >> $outfile 2>&1
    # Test if compiling finished
    if [[ $? != 0 ]] ; then
	echo "THERE IS A PROBLEM IN LMDZ REGULAR LATLON COMPILATION - STOP"
	exit
    fi

    # Compile ce0l initialization program for LMDZ regular lat-lon exectuable
    echo; echo "NOW COMPILE CE0L OF LMDZ. Resolution = ${resol_atm_3d}"
    echo >> $outfile ; echo " NOW COMPILE CE0L OF LMDZ. Resolution = ${resol_atm_3d}"   >> $outfile 

    echo ./makelmdz_fcm -d ${resol_atm_3d} -p lmd -rrtm true -$optmode -mem -parallel $parallel -io xios -v orchidee2.1 -arch $fcm_arch -j 8 $full_flag ce0l    >> $outfile 
         ./makelmdz_fcm -d ${resol_atm_3d} -p lmd -rrtm true -$optmode -mem -parallel $parallel -io xios -v orchidee2.1 -arch $fcm_arch -j 8 $full_flag ce0l    >> $outfile 2>&1
    # Test if compiling finished
    if [[ $? != 0 ]] ; then
	echo "THERE IS A PROBLEM IN CE0L (LMDZ) REGULAR LATLON COMPILATION - STOP"
	exit
    fi

    # Find executable suffix
    if [ $parallel == seq ] || [ $parallel == none ] ; then
	suffix=_${resol_atm_3d}_phylmd_seq_orch
    else
	suffix=_${resol_atm_3d}_phylmd_para_mem_orch
    fi
    echo suffix = $suffix
    
    # Move executables to modipsl/bin folder
    echo "Move gcm.e and ce0l executable to modipsl/bin"
    if [ $modipsl/modeles/LMDZ/bin/gcm${suffix}.e ] ;  then mv $modipsl/modeles/LMDZ/bin/gcm${suffix}.e $modipsl/bin/gcm_${resol_atm_3d}.e ; fi
    if [ $modipsl/modeles/LMDZ/bin/ce0l${suffix}.e ] ;  then mv $modipsl/modeles/LMDZ/bin/ce0l${suffix}.e $modipsl/bin/ce0l_${resol_atm_3d}.e ; fi

    # Write .resol file
    cd $submitdir   
    resol2D=$( echo ${resol_atm_3d} | awk '-Fx' '{print $1}' )$( echo ${resol_atm_3d} | awk '-Fx' '{print $2}' )
    echo "noORCAxLMD$resol2D" >.resol
    echo "RESOL_ATM_3D=${resol_atm_3d}" >>.resol
fi


echo >>$outfile ; echo "ALL COMPILING FINISHED" >> $outfile
echo; echo "ALL COMPILING FINISHED"

date
exit


