#!/bin/bash
# script pour lancer une deglaciation

# name of the run:
#IPSL
namerun="deg-HN40"
#geoplace="euras40"
#geoplace="euras20"
#geoplace="euras10"
geoplace="hemin40"
#geoplace="laure16"

tmodelend="0." #"0."
tmodelbegin="-26000."
#tmodelbegin="-50000."
tmodellength="26000."
#tmodellength="50000."
runatmvar="0.25"
runpdd="2"

parallel="no" # "yes or no?"

case ${geoplace} in
  euras40 ) exename="Euras-40" ; INPUTDIR="EURASIE40" ;;
  euras20 ) exename="Euras-20" ; INPUTDIR="EURASIE20" ;;
  euras10 ) exename="Euras-10" ; INPUTDIR="EURASIE10" ;;
  hemin40 ) exename="Hemin-40" ; INPUTDIR="HEMIN40" ;;
  laure16 ) exename="Laure-16" ; INPUTDIR="LAURE16" ;;
esac

#Revision number :
rev=`svn info | grep "Révision : " | awk '{print $2}'`
echo "Code revision svn "$rev

echo "-------------------------------------"
echo "    Now launching run: "   $namerun
echo "      -------------------------      "

#--------------------------------------------------
# Check if times are ok:
#--------------------------------------------------

tcheck=`echo "$tmodelbegin + $tmodellength" | bc -l`
tcheckbis=`echo "$tmodelend + 0" | bc -l`
if [ $tcheck != $tcheckbis ]; then
    echo "The times provided are wrong!"
    echo $tcheck $tcheckbis
    echo "Tbegin:" $tmodelbegin ", Tend:" $tmodelend ", length:" $tmodellength
    exit 0
else
    echo "The times provided are correct."
fi

#--------------------------------------------------
# The different directories:
#--------------------------------------------------
LAUCHING_DIR=`pwd`
GRISLI_DIR=${LAUCHING_DIR%%/lauching_area}
RUNDIR=${GRISLI_DIR}"/RUNS/"${namerun}-r${rev}
MODELMASTER=${GRISLI_DIR}"/SOURCES"
MODEL=${RUNDIR}"/SOURCES"

if [ -d $RUNDIR ]; then
    echo "This run exists, do you really want to overwrite?"
    echo "You have 3 sec. to do a ctr+c..."
    sleep 3
    echo "Ok, your choice."
else
    mkdir $RUNDIR
    echo "      Run will be in:"
    echo "  " $RUNDIR
fi

if [ -d $MODEL ]; then
    echo "model dir already created, cleaning up before copying files"
    rm -rf $MODEL"/"*
else
    mkdir $MODEL
fi
 
cp -R $MODELMASTER"/"* $MODEL"/."
svn status $MODELMASTER > $MODEL"/svn.status.txt"
svn diff $MODELMASTER > $MODEL"/svn.diff.txt"

if [ -d $RUNDIR"/bin" ]; then
    echo "exe in bin/"
else
    mkdir $RUNDIR"/bin"
fi

if [ -d $RUNDIR"/RESULTATS" ]; then
    echo "exe in RESULTATS/"
else
    mkdir $RUNDIR"/RESULTATS"
fi

if [ -d $RUNDIR"/INPUT" ]; then
    echo "inputs are linked to model master"
else
    echo "inputs will be linked to model master"
    mkdir $RUNDIR"/INPUT"
fi


    
echo "Now copying master files to the working directory:"
echo $RUNDIR
echo "And to the model directory:"
echo $MODEL

cp "Master-files/TEMPS-NETCDF-degla.dat" $MODEL"/Fichiers-parametres/TEMPS-NETCDF.dat"
cp "degla_PMIP_${geoplace}_param_list.dat" $RUNDIR"/bin/${geoplace}_param_list.dat"

if [ $parallel = "yes" ]; then
  cp "Master-files/grisli.run.openmp" $RUNDIR"/bin/grisli.run"
else
  cp "Master-files/grisli.run" $RUNDIR"/bin/grisli.run"
fi

cd $RUNDIR"/INPUT"
ln -sf ${GRISLI_DIR}"/INPUT/"${INPUTDIR} .
ln -sf ${GRISLI_DIR}"/INPUT/Forcage" .
ln -sf ${GRISLI_DIR}"/INPUT/tracer.dat" .

#--------------------------------------------------
# Replacing key words:
#--------------------------------------------------

# run spec.:
sed -i "s/RUNNAME/$namerun/" $RUNDIR"/bin/${geoplace}_param_list.dat"
sed -i "s/RUNTBEGIN/$tmodelbegin/" $RUNDIR"/bin/${geoplace}_param_list.dat"
sed -i "s/RUNTEND/$tmodelend/" $RUNDIR"/bin/${geoplace}_param_list.dat"
sed -i "s/RUNRATMVAR/$runatmvar/" $RUNDIR"/bin/${geoplace}_param_list.dat"
sed -i "s/RUNPDDTYPE/$runpdd/" $RUNDIR"/bin/${geoplace}_param_list.dat"

# submission script:
sed -i "s|SOURCESREP|$MODEL|" $RUNDIR"/bin/grisli.run"
sed -i "s|EXEREP|$RUNDIR\/bin|" $RUNDIR"/bin/grisli.run"
sed -i "s/RUNNAME/$namerun/" $RUNDIR"/bin/grisli.run"
sed -i "s/EXE_NAME/$exename/g" $RUNDIR"/bin/grisli.run"

# Makefile:
if [ $parallel = "yes" ]; then
    sed -i "s/Makefile.tof-lsce3130.inc/Makefile.tof-asterix-parallel.inc/" $RUNDIR"/SOURCES/Makefile"
else
    sed -i "s/Makefile.tof-lsce3130.inc/Makefile.tof-asterix-sequential.inc/" $RUNDIR"/SOURCES/Makefile"
fi

# param values:
#sed -i "s/PARAMENHFACT/$enh_fact/" $RUNDIR"/bin/anteis1_param_list.dat"
#sed -i "s/PARAMCF/$cf_fact/" $RUNDIR"/bin/anteis1_param_list.dat"
#sed -i "s/PARAMSEUILNEFF/$seuilneff_fact/" $RUNDIR"/bin/anteis1_param_list.dat"
#sed -i "s/PARAMINFILTR/$infiltr_fact/" $RUNDIR"/bin/anteis1_param_list.dat"
#sed -i "s/PARAMKOND0/$kond0_fact/" $RUNDIR"/bin/anteis1_param_list.dat"
#sed -i "s/BETAMAX/$betamax_fact/" $RUNDIR"/bin/anteis1_param_list.dat"
#sed -i "s/TOSTICK/$tostick_fact/" $RUNDIR"/bin/anteis1_param_list.dat"
#sed -i "s/EXPOSLOPE/$exposlope_fact/" $RUNDIR"/bin/anteis1_param_list.dat"
#sed -i "s/PARAMBSLOPE/$bslope_fact/" $RUNDIR"/bin/anteis1_param_list.dat"
#sed -i "s/PARAMBEXPO/$bexpo_fact/" $RUNDIR"/bin/anteis1_param_list.dat"
#sed -i "s/PARAMBMELTCOEF/$bmelt_fact/" $RUNDIR"/bin/anteis1_param_list.dat"
#sed -i "s/PARAMSF/$sf_fact/" $RUNDIR"/bin/anteis1_param_list.dat"


#--------------------------------------------------
# Submitting the job:
#--------------------------------------------------
cd $RUNDIR"/bin"
echo "submitting the job"
qsub $RUNDIR"/bin/grisli.run"
echo "-------------------------------------"
