#!/bin/bash
# script pour lancer un cycle antarctique 

# name of the run:
namerun="Ant40-02"

tmodelend="10000."
tmodelbegin="-126000."
tmodellength="136000."

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


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:
#--------------------------------------------------
#RUNDIR="/home/climold/aquiquet/GRISLI/GRISLI_trunk/RUNS/"$namerun
#MODELMASTER="/home/climold/aquiquet/GRISLI/GRISLI_trunk/SOURCES"
#MODEL=$RUNDIR"/SOURCES"
LAUCHING_DIR=`pwd`
GRISLI_DIR=${LAUCHING_DIR%%/lauching_area}
RUNDIR=${GRISLI_DIR}"/RUNS/"${namerun}
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/Makefile" $MODEL"/Makefile"
#cp "../Master-files/anteis1_param_list.dat" $RUNDIR"/bin/anteis1_param_list.dat"
cp "Master-files/anteis1_TEMPS-NETCDF-cycle.dat" $MODEL"/Fichiers-parametres/anteis1_TEMPS-NETCDF.dat"
cp "Master-files/module_choix-antar40-0.4-cycles.f90" $MODEL"/Ant40_files/module_choix-antar40-0.4.f90"
# Schoof :
#cp "../Param/anteis1_param_list_Cyc-Scho.dat" $RUNDIR"/bin/anteis1_param_list.dat"
# Tsai
cp "../Param/anteis1_param_list_Cyc-Tsai.dat" $RUNDIR"/bin/anteis1_param_list.dat"

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

cd $RUNDIR"/INPUT"
ln -sf ${GRISLI_DIR}"/INPUT/ANTEIS1" .
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/anteis1_param_list.dat"
sed -i "s/RUNTBEGIN/$tmodelbegin/" $RUNDIR"/bin/anteis1_param_list.dat"
sed -i "s/RUNTEND/$tmodelend/" $RUNDIR"/bin/anteis1_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"
if [ $parallel = "yes" ]; then
    sed -i '/EXEDIR/s/$/\nmodule load netcdf\/4p\n/' $RUNDIR"/bin/grisli.run"
else
    sed -i '/EXEDIR/s/$/\nmodule load netcdf\/4\n/' $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

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

echo "submitting the job"
qsub $RUNDIR"/bin/grisli.run"
echo "-------------------------------------"








