#!/bin/ksh
# $Id$
######################################################
# Original : C. Talandier for NEMO team
# Contact  : nemo_st@locean-ipsl.upmc.fr
#
# This script aims to (for each standard configuration) :
#  - I. Take into account specific files changes
#       - ./modipsl/util/ins_make
#       - ./modipsl/config/NAME_CONFIG/AA_make*
#       - ./modipsl/config/NAME_CONFIG/scripts/BB_make.ldef
#  - II. Build configuration's environment & dependencies
#       - ./modipsl/config/NAME_CONFIG/WORK
#       - ./modipsl/config/NAME_CONFIG/MY_SRC
#       - ./modipsl/config/NAME_CONFIG/scripts/BB_make
#  - III. Build results sub-directories in the OUTDIR 
#         directory specified by user just below
#         NEMO_VALID/WNAME_CONFIG/
#                    |-- mon		--> MONO run
#                    |   |-- 1_SHORT
#                    |   |-- 2_SHORT
#                    |   |-- LONG
#                    |   `-- GTIME
#                    |-- mpi		--> MPI run
#                    |   |-- 1_SHORT
#                    |   |-- 2_SHORT
#                    |   |-- LONG
#                    |   `-- GTIME
#                    `-- omp		--> Open-MP run
#                        |-- 1_SHORT
#                        |-- 2_SHORT
#                        |-- LONG
#                        `-- GTIME
#
#  - IV. Modify lance_batch.ksh & assessment.ksh scripts
#  - V. Preliminary compilations
######################################################

#set -xv

##########################################################
##### Begin Users modifications
##########################################################
# OUTDIR : working directory from which jobs will be launched 
#          & results stored
# INPUTD : directory where to get ORCA2_LIM_nemo_v2.tar 
# REFDAT : directory where to get results from reference tags
# MAIL   : give your e-mail
# UAGRIF : one of the standard configurations is based on AGRIF yes/no
##########################################################
OUTDIR=/Users/ctlod/WORK/AZE/modipsl/config
INPUTD=/u/rech/eee/reee831/IO_NEMO_ORCA2_LIM
REFDAT=/u/rech/eee/reee831/NEMO_VALIDATION
MAIL="ctlod@locean-ipsl.upmc.fr"
UAGRIF=yes
##########################################################
##### End   Users modifications
##########################################################


# Standard configurations
NBCFG=7
CONF[0]=ORCA2_LIM
CONF[1]=ORCA2_LIM3
CONF[2]=GYRE
CONF[3]=GYRE_LOBSTER
CONF[4]=ORCA2_LIM_PISCES
CONF[5]=ORCA2_OFF_PISCES
CONF[6]=ZAGRIF
LCLDIR=`pwd`

# Use a modified modipsl script: ins_make
# which allows to build Makefiles of a specific configuration
# using the following syntax: ins_make -w NAME_CONF
cp ./MODIPSL_FILES/ins_make ../../../util/.

# Get the current target name
LOCTARG=`../../../util/w_i_h`

# Set following variable in AA_make.ldef script And master Makefile
# - WWDIR = directory where to store results and jobs
CAR_WORK_tmp=$( grep 'WWDIR=' ./CONFIG_FILES/AA_make.ldef )
CAR_WORK=`echo $CAR_WORK_tmp | cut -f 1 -d " " `
CAR_TARG_tmp=$( grep 'HPCTAR=' ./CONFIG_FILES/AA_make.ldef )
CAR_TARG=`echo $CAR_TARG_tmp`
sed -e "s?${CAR_WORK}?WWDIR=${OUTDIR}?" -e "s?${CAR_TARG}?HPCTAR=${LOCTARG}?" \
       ./CONFIG_FILES/AA_make.ldef > ./CONFIG_FILES/AA_make.ldef.$$ 
mv ./CONFIG_FILES/AA_make.ldef.$$  ./CONFIG_FILES/AA_make.ldef && rm -f ./CONFIG_FILES/AA_make.ldef.$$

sed -e "s?DELFIL?${OUTDIR}?" ../Makefile > ../Makefile.$$ 
mv ../Makefile.$$  ../Makefile && rm -f ../Makefile.$$

# Loop over each standard configuration
# #####################################
ind=0
while [ $ind -lt ${NBCFG} ]
do
       # I. Take into account specific changes
       # #####################################
       # Set following variables in job_NAME_CONF.ksh script
       # - MAINDIR = directory where to get namelist input files
       # - WORK    = directory where to store results
       # - REF_OD  = directory where Are stored results from previous tags
       cd ../../../.. ; NAME_ARCH=$( pwd ) ;
       cd ${LCLDIR}
       echo ${NAME_ARCH}
       CAR_MAIN_tmp=$( grep 'MAINDIR=' ./JOBS/job_${CONF[${ind}]}.ksh )
       CAR_MAIN=`echo $CAR_MAIN_tmp | cut -f 1 -d " " `
       CAR_WORK_tmp=$( grep 'WORK=' ./JOBS/job_${CONF[${ind}]}.ksh )
       CAR_WORK=`echo $CAR_WORK_tmp | cut -f 1 -d " " `
       CAR_REFO_tmp=$( grep 'REF_OD=' ./JOBS/job_${CONF[${ind}]}.ksh  | head -1 )
       CAR_REFO=`echo $CAR_REFO_tmp | cut -f 1 -d " " `
       sed -e "s?${CAR_MAIN}?MAINDIR=${NAME_ARCH}?" -e "s?${CAR_WORK}?WORK=${OUTDIR}?" \
       -e "s?${CAR_REFO}?REF_OD=${REFDAT}?" ./JOBS/job_${CONF[${ind}]}.ksh > ./JOBS/job_tmp.$$
       mv ./JOBS/job_tmp.$$  ./JOBS/job_${CONF[${ind}]}.ksh && rm -f ./JOBS/job_tmp.$$

       # II. Build configuration environment
       # ###################################
       (cd ../ ; ./use_cfg -n ${CONF[${ind}]} ;)
echo '			'
echo '	     >>>>>>>>   Configuration directories have been built:'
echo "			./config/${CONF[${ind}]}/WORK"
echo "			./config/${CONF[${ind}]}/MY_SRC"
echo '			'

       cp ./CONFIG_FILES/AA_make* ../../${CONF[${ind}]}/.
       cp ./CONFIG_FILES/BB_make.ldef_${CONF[${ind}]} ../../${CONF[${ind}]}/scripts/BB_make.ldef

       # Configuration dependencies
       (cd ../../${CONF[${ind}]} ; ../NVTK/fait_AA_make ; )
echo '                  '
echo "             >>>>>>>>   Dependencies file ./config/${CONF[${ind}]}/BB_make have been re-built"

       # III. Build output sub-directories
       # #################################
       # Directories from where Jobs will be launched 
       # and where results will be stored before analysed
       cd ${OUTDIR}
       if [ ! -d 'NEMO_VALID' ] ;  then
           mkdir  NEMO_VALID
       fi
       cd NEMO_VALID
       mkdir -p W${CONF[${ind}]}/infos/inter
       mkdir -p W${CONF[${ind}]}/mon/LONG
       mkdir -p W${CONF[${ind}]}/mon/GTIME
       mkdir -p W${CONF[${ind}]}/mon/1_SHORT
       mkdir -p W${CONF[${ind}]}/mon/2_SHORT
       #
       mkdir -p W${CONF[${ind}]}/mpi/LONG
       mkdir -p W${CONF[${ind}]}/mpi/GTIME
       mkdir -p W${CONF[${ind}]}/mpi/1_SHORT
       mkdir -p W${CONF[${ind}]}/mpi/2_SHORT
       #
       mkdir -p W${CONF[${ind}]}/omp/LONG
       mkdir -p W${CONF[${ind}]}/omp/GTIME
       mkdir -p W${CONF[${ind}]}/omp/1_SHORT
       mkdir -p W${CONF[${ind}]}/omp/2_SHORT
       #
echo '			'
echo '	     >>>>>>>>   The following sub-directories have been built '
echo '                        under main directory '$( pwd )
echo '			They are dedicated to store results'
       find W${CONF[${ind}]} -type d
echo '			'

       cd ${LCLDIR}

       let ind=$ind+1
done


# IV. Modify lance_batch.ksh & assessment.ksh scripts
# ###################################################
# Set following variables in lance_batch.ksh script
# - IODIR = directory where to get ORCA2_LIM_nemo_v2.tar
# - PRC   = total number of processors which will be used

CAR_INPU_tmp=$( grep 'IODIR=' ./JOBS/lance_batch.ksh )
CAR_INPU=`echo $CAR_INPU_tmp | cut -f 1 -d " " `
CAR_TARG_tmp=$( grep 'TARGET=' ./JOBS/lance_batch.ksh | head -1)
CAR_TARG=`echo $CAR_TARG_tmp | cut -f 1 -d " " `
sed -e "s?${CAR_INPU}?IODIR=${INPUTD}?" -e "s/${CAR_TARG}/TARGET=${LOCTARG}/" \
./JOBS/lance_batch.ksh > ./JOBS/lance_batch.$$
mv ./JOBS/lance_batch.$$  ./JOBS/lance_batch.ksh && rm -f ./JOBS/lance_batch.$$
chmod 744 ./JOBS/lance_batch.ksh

# Set following variables in assessment.ksh script
# - TARGET = the current plateform 
# - MAIL   = your e-mail
CAR_TARG_tmp=$( grep 'TARGET=' ./JOBS/assessment.ksh )
CAR_TARG=`echo $CAR_TARG_tmp | cut -f 1 -d " " `
CAR_MAIL_tmp=$( grep 'EMAIL=' ./JOBS/assessment.ksh )
CAR_MAIL=`echo $CAR_MAIL_tmp | cut -f 1 -d " " `
sed -e "s/${CAR_TARG}/TARGET=${LOCTARG}/" -e "s/${CAR_MAIL}/EMAIL='${MAIL}'/" \
./JOBS/assessment.ksh > ./JOBS/assessment.$$
mv ./JOBS/assessment.$$  ./JOBS/assessment.ksh && rm -f ./JOBS/assessment.$$
chmod 744 ./JOBS/assessment.ksh

# V. Preliminary compilations
# ###########################

# IOIPSL compilation
../../../util/ins_make -w IOIPSL
if [ ${LOCTARG} = 'sx8brodie' -o ${LOCTARG} = 'sx8mercure' ] ; then
    (cd ../../../modeles/IOIPSL/src ; sxgmake ;)
else
    (cd ../../../modeles/IOIPSL/src ; gmake ;)
fi
echo '			'
echo '	     >>>>>>>>   IOIPSL Makefile and compilation done'
echo '			'

# AGRIF compilation
if [ ${UAGRIF} = 'yes' ] ;  then
    ../../../util/ins_make -w AGRIF
    (cd ../../../modeles/AGRIF/ ; gmake ;)
    echo '			'
    echo '			AGRIF Makefile and compilation done'
    echo '			'
fi


echo '			'
echo '			#############################################################'
echo '             >>>>>>>>   Following configuration(s) can be used with NVTK:'
ind=0
while [ $ind -lt ${NBCFG} ]
do
echo "                        # ${CONF[${ind}]}"
let ind=$ind+1
done
echo '			###########################################################'
echo '			'
