#!/bin/ksh
# $Id$
#---------------------------------------------------------------------
#- Installation of jobs according to an environment
#---------------------------------------------------------------------
function ins_job_Usage
{
print - "
ins_job installs the jobs in the directories
which contain a file config.card

ins_job must be launched on the host
on which the job will be submitted

Usage :
  ${b_n} [-h] [-v]

Options :
  -h   : help
  -v   : verbose mode
"
}
function ins_job_Warning
{
   print - "\n############### WARNING ###############";
   print - "File ${n_f} already exists\nin directory ${j}";
   print - "You must delete this file to update !";
}
#-
#     dirname     and      basename
#-
d_n=$(dirname ${0}); b_n=$(basename ${0});
#-
# Retrieving and validation of the options
#-
x_v='silencious';
while getopts :hv V
  do
   case $V in
    (h)  ins_job_Usage; exit 0;;
    (v)  x_v='verbose';;
    (:)  echo ${b_n}" : -"${OPTARG}" option : missing value" 1>&2;
         exit 2;;
    (\?) echo ${b_n}" : -"${OPTARG}" option : not supported" 1>&2;
         exit 2;;
    esac
  done
shift $(($OPTIND-1));
#-
# Define working files
#-
F_MOD=$(cd ${d_n}'/..';/bin/pwd;)
# [[ ${F_MOD##*/} != 'modipsl' ]] && \
#  { print - "directory 'modipsl' unreachable"; exit 3; }
W_W=${d_n}'/../libIGCM'
[[ ! -d ${W_W} ]] && { print - "${W_W} unreachable"; exit 3; }
libIGCM=$(cd ${W_W};/bin/pwd;)
F_JOB=${libIGCM}'/AA_job';
[[ ! -f ${F_JOB} ]] && { print - "${F_JOB} unreachable"; exit 3; }
F_RCI=${libIGCM}'/run.card.init';
[[ ! -f ${F_RCI} ]] && { print - "${F_RCI} unreachable"; exit 3; }
#-
# Host Name
#-
x_t=$(${d_n}/w_i_h) 2>/dev/null;
{ [[ ${?} != 0 ]] && \
   { print - "\nw_i_h or uname unreachable\n" 1>&2; exit 1; }; } || \
  [[ ${x_t} = "Unknown" ]] && \
   { print - "\nLocal_Host not supported\n" 1>&2; exit 1; };
#-
W_W=$(sed -n -e "s/^#-Q-  *\([^ ]*\).*$/\1/p" ${F_JOB} | \
       sort -u | sed -e "/${x_t}/!d");
[[ '\?'"${W_W}" != '\?'${x_t} ]] && \
  {
   print - "\nHost  "${x_t}"  not supported" 1>&2;
   print - "'default' will be used" 1>&2;
   x_t='default'
  }
#-
[[ ${x_v} = 'verbose' ]] && \
 {
  print - "";
  print - '--- Host        : '${x_t};
  print - '--- modipsl     : '${F_MOD};
  print - '--- libIGCM     : '${libIGCM};
  print - '--- basic job   : '${F_JOB};
  print - '--- basic card  : '${F_RCI};
 }
#-
print - "\nInstallation of jobs for '${x_t}'";
#-
# Accessing to functions (without stack)
#-
DEBUG_debug=false
. ${libIGCM}/libIGCM_debug/libIGCM_debug.ksh
. ${libIGCM}/libIGCM_card/libIGCM_card.ksh
#-
# Define the pattern string to substitute
#-
W_P='#-Q- '; W_W=${W_P}${x_t};
#-
# Extract list of 'config.card' files
# and create jobs with AA_job
#-
F_CFG='config.card';
for i in $(find ${d_n}/.. -name ${F_CFG} -print)
do
 j=$(cd ${i%/*};/bin/pwd;)
 n_f=${F_RCI##*/};
 [[ -f ${j}'/'${n_f} ]] && { ins_job_Warning; } || \
  {
   print - "\nCopying file ${F_RCI}\nin directory ${j}";
   \cp ${F_RCI} ${j};
  }
 # We keep explicitly JobName because it is not optionnal.
 RESOL_ATM_3D=this_is_a_test_string
 RESOL=this_is_another_test_string
 IGCM_card_DefineVariableFromOption ${j}'/'${F_CFG} UserChoices JobName
 if [ ! X$( echo ${config_UserChoices_JobName} | grep ${RESOL_ATM_3D} ) = X ] ; then
    TRUERESOL=$( find ../ -name ".resol" -exec tail -1 {} \; | awk "-F=" '{print $2}' )
    config_UserChoices_JobName=$( echo ${config_UserChoices_JobName} | sed -e "s/${RESOL_ATM_3D}/${TRUERESOL}/" )
    IGCM_card_WriteOption ${j}'/'${F_CFG} UserChoices JobName ${config_UserChoices_JobName}
 else
    if [ ! X$( echo ${config_UserChoices_JobName} | grep ${RESOL} ) = X ] ; then
	    TRUERESOL=$( find ../ -name ".resol" -exec head -1 {} \; )
	    config_UserChoices_JobName=$( echo ${config_UserChoices_JobName} | sed -e "s/${RESOL}/${TRUERESOL}/" )
	    IGCM_card_WriteOption ${j}'/'${F_CFG} UserChoices JobName ${config_UserChoices_JobName}
    fi
 fi
 
 # Other options are not always needed
 IGCM_card_DefineArrayFromSection ${j}'/'${F_CFG} UserChoices
 # Default values
 config_UserChoices_JobClass=mono
 config_UserChoices_JobNumProcTot=1
 # config.card optionnal values
 typeset option
 for option in ${config_UserChoices[*]} ; do
     IGCM_card_DefineVariableFromOption ${j}'/'${F_CFG} UserChoices ${option}
 done
 # File name for Job
 n_f='Job_'${config_UserChoices_JobName};
 [[ -f ${j}'/'${n_f} ]] && { ins_job_Warning; continue; }
 print - "\nWorking with file ${F_CFG}\nin directory ${j}\nfor ${n_f}";
 sed -e "/^${W_W} */ s///" \
     -e "/^${W_P}/d"       \
     -e "s%::modipsl::%${F_MOD}%" \
     -e "s/::Jobname::/${config_UserChoices_JobName}/" \
     -e "s/::JobClass::/${config_UserChoices_JobClass}/" \
     -e "s/::JobNumProcTot::/${config_UserChoices_JobNumProcTot}/" \
     ${F_JOB} > ${j}'/'${n_f}
done
#-
# Extract list of AA_* files in libIGCM
# and create jobs (for all except AA_job)
#-
for i in $(find ${libIGCM} -name "AA_*" -print | grep -v .svn )
do
 i_f=${i##*/};
 [[ ${i_f} = 'AA_job' ]] && { continue; }
 j=${i%/*}; n_f=${i_f#AA_}'.job';
 [[ -f ${j}'/'${n_f} ]] && { ins_job_Warning; continue; }
 print - "\nIn directory ${j}\n${i_f} -> ${n_f}"
 sed -e "/^${W_W} */ s///" \
     -e "s%::modipsl::%${F_MOD}%" \
     -e "/^${W_P}/d"       \
     ${i} > ${j}'/'${n_f}
 chmod u+x ${j}'/'${n_f}
done
#-
print - "";
#-
# That's all folks
#-
exit 0;
