#!/bin/bash
#
# define_config.sh is called from the main compilation script compile_ipslcm7.sh
# The script will define which components to compile.
# By default all availble components are compiled.
#
# Step 1: The avaible components are defined. 
# Step 2: Define which components to compile. If argument [-subconfig subconfig] is set 
#         to the main compilation script, a control if the components needed are avaiable. 
#         If argument is subconfig is not set, the components defined in step 1 will be defined.


### Step 1: Check available components possible to compile
# LMDZ, ORCHIDEE and XIOS are considered mandatory and will not be checked.

if [ -d ../../modeles/NEMO ] ; then
    compnemo=y
else
    compnemo=n
fi

if [ -d ../../modeles/INCA ] ; then
    compinca=y
else
    compinca=n
fi

if [ -d ../../modeles/DYNAMICO ] ; then
    compdyna=y
else
    compdyna=n
    regular_latlon=y
fi

if [ -d ../../oasis3-mct ] ; then
    compoasis=y
else
    compoasis=n
fi

if [ -d ../../modeles/LMDZ ] ; then
    complmdz=y
else
    complmdz=n
fi



### Step 2: Define which components to compile. Check if the configuration set as argument is possible
if [ $subconfig == default ] ; then
    echo ; echo Compilation of all availble components will be done:

elif [ $subconfig == IPSLCM ] || [ $subconfig == IPSLCM-ico ] ; then
    # Full coupled model currently without INCA
    if [ $compdyna == n ] || [ $compoasis == n ] || [ $compnemo == n ] || [ $complmdz == n ] ; then
        echo ; echo It is not possible to compile IPSLCM.
        echo Source code is missing
        exit
    else
        echo "Compilation of IPSLCM configuration (without INCA) will be done"
        compnemo=y compoasis=y compdyna=y compinca=n complmdz=y comporch=y compxios=y
    fi
elif [ $subconfig == IPSLCM-reg ] ; then
    # Coupled model without DYNAMICO and INCA
    if [ $compoasis == n ] || [ $compnemo == n ] || [ $complmdz == n ] ; then
        echo ; echo It is not possible to compile IPSLCM-reg.
        echo Source code is missing
        exit
    else
	echo "Compilation of IPSLCM-reg configuration (without INCA and DYNAMICO) will be done"
        compnemo=y compoasis=y compdyna=n compinca=n complmdz=y comporch=y compxios=y
        regular_latlon=y
    fi
elif [ $subconfig == ICOLMDZORINCA ] ; then
    if [ $compinca == n ] || [ $compdyna == n ] || [ $complmdz == n ] ; then
	echo ; echo It is not possible to compile ICOLMDZORINCA.
	echo INCA or DYNAMICO source code is missing
        exit
    else
	echo Compilation of ICOLMDZORINCA configuration will be done
        compnemo=n compoasis=n compdyna=y compinca=y complmdz=y comporch=y compxios=y
    fi	
elif [ $subconfig == ICOLMDZOR ] ; then
    if [ $compdyna == n ] || [ $complmdz == n ] ; then
	echo ; echo It is not possible to compile ICOLMDZOR
	echo DYNAMICO source code is missing
        exit
    else
	echo Compilation of ICOLMDZOR configuration will be done
        compnemo=n compoasis=n compdyna=y compinca=n complmdz=y comporch=y compxios=y
    fi
elif [ $subconfig == LMDZORINCA ] ; then
    if [ $compinca == n ] || [ $complmdz == n ] ; then
	echo ; 	echo It is not possible to compile LMDZORINCA.
	echo INCA or LMDZ source code is missing
        exit
    else
	regular_latlon=y
	echo Compilation of regular LMDZORINCA configuration for grid $resol_atm will be done 
        compnemo=n compoasis=n compdyna=n compinca=y complmdz=y comporch=y compxios=y
    fi	
elif [ $subconfig == LMDZOR ] ; then
   if [ $complmdz == n ] ; then
        echo ;  echo It is not possible to compile LMDZOR.
        echo LMDZ source code is missing
        exit
    else
      regular_latlon=y
      echo Compilation of regular LMDZOR configuration for grid $resol_atm will be done
      compnemo=n compoasis=n compdyna=n compinca=n complmdz=y comporch=y compxios=y
   fi
elif [ $subconfig == ORCHIDEE_OL ] ; then
    # We always suppose ORCHIDEE is extracted. No check needs to be done.
    regular_latlon=n
    echo Compilation of ORCHIDEE offline configuration will be done
    compnemo=n compoasis=n compdyna=n compinca=n complmdz=n comporch=y compxios=y
else
    echo ; echo "The configuration $subconfig is not available"
    echo "Choose between ICOLMDZORINCA, ICOLMDZOR, LMDZORINCA, LMDZOR"
    echo "               IPSLCM or IPSLCM-reg" 
    echo "Exit now"
    exit
fi
echo compnemo=$compnemo compoasis=$compoasis compdyna=$compdyna compinca=$compinca complmdz=$complmdz comporch=y compxios=y
echo ;


# Coherence test for variable regular_latlon which can be set as input argument
if [ $regular_latlon == yes ] || [ $regular_latlon == y ] ; then
	regular_laton=y
elif [ $regular_latlon == no ] || [ $regular_latlon == n ] ; then
        regular_laton=n
else
	echo "regular_latlon=$regular_latlon : This option is not possible."
	echo "Compile using argument '-regular_latlon' no or '-regular_latlon yes'" 
	echo "Exit now"
	exit
fi

