#!/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."
tmodelbegin="-26000."
tmodellength="26000."
runatmvar="0.25"
runpdd="2"

parallel="no" # "yes or no?"
proc_number="4" # number of processor used
ppn_number=$proc_number # number of cpus (ppn)
exename="Ant-40"

queue="long"  # specify queue used : short, medium, long, xlong...

# Files used
grisli_run_file="Master-files/grisli.run.lsce"                        # grisli.run
param_file="Master-files/Param/degla_PMIP_${geoplace}_param_list.dat"                    # param file
TEMPS_NETCDF_file="Master-files/Param/TEMPS-NETCDF-degla.dat"               # TEMPS-NETCDF.dat
#module_choix_file="Master-files/module_choix-antar40-0.4-cycles.f90"  # module choix


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 "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:
#--------------------------------------------------
LAUNCHING_DIR=`pwd`
GRISLI_DIR=${LAUNCHING_DIR%%/launching_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 $TEMPS_NETCDF_file $MODEL"/Fichiers-parametres/TEMPS-NETCDF.dat"
cp $param_file $RUNDIR"/bin/${geoplace}_param_list.dat"

cp $grisli_run_file $RUNDIR"/bin/grisli.run"

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"
sed -i "s|USERNAME|$USER|" $RUNDIR"/bin/grisli.run"

if [ $parallel = "yes" ]; then
    sed -i "s/QUEUE/${queue}p/" $RUNDIR"/bin/grisli.run"
    sed -i "s/PROC_NUMBER/$proc_number/" $RUNDIR"/bin/grisli.run"
    sed -i "s/PPN_NUMBER/$ppn_number/" $RUNDIR"/bin/grisli.run"
else
    sed -i "s/QUEUE/$queue/" $RUNDIR"/bin/grisli.run"
    sed -i "s/PROC_NUMBER/1/" $RUNDIR"/bin/grisli.run"
    sed -i "s/PPN_NUMBER/1/" $RUNDIR"/bin/grisli.run"
    sed -i "/export\ MKL_NUM_THREADS/d" $RUNDIR"/bin/grisli.run"
    sed -i "/export\ OMP_NUM_THREADS/d" $RUNDIR"/bin/grisli.run"
    sed -i "/export\ KMP_STACKSIZE/d" $RUNDIR"/bin/grisli.run"
fi

# 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


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