#!/bin/ksh
# $Id$
######################################################
# Author : Rachid benshila for NEMO team
# Contact : nemo_st@lodyc.jussieu.fr
#
# This script aims to create the OPA/WORK directory
# and to link in the WORK the BB_make and BB_make.ldef 
# associated  to a chosen configuration
#
######################################################
d_n=$(dirname $0); b_n=$(basename $0);

#set -xv

#- Saisie et validation des options
x_d="";
x_n="";
x_r="";
while getopts :hld:n:r: V
  do
    case $V in
      (h)  echo "Usage    : "${b_n} \
                " [-h] [-l] [-d] [-p r] [-t h] ";
           echo " -h      : help";
           echo " -l      : list existing configurations";
           echo " -n name : config name";
           echo " -d dir  : choose directories";
           echo "";
           echo "Example to install a new configuration MY_CONFIG";
           echo "with OPA_SRC and ICE_SRC ";
           echo "use_cfg -n MY_CONFIG -d \"OPA_SRC ICE_SRC\"";
           echo "";
#           echo "Defaults     : ";
           echo "";
           exit 0;;
      (d)  x_d=${OPTARG};;
      (n)  x_n=${OPTARG};;
      (r)  x_r=${OPTARG};;
      (l)  cat ${d_n}/cfg.txt; exit;;
      (:)  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));

#- Some tests ---
m_n=${x_n}

if [ ${#x_n} -eq 0 ]; then
echo "Warning"
echo "No configuration specified"
echo "use_cfg -h for help"
exit
fi

cat ${d_n}/cfg.txt | grep ${m_n} > ${d_n}/cfg.tmp
read CONFIG TAB[0] TAB[1] TAB[2] TAB[3] TAB[4] TAB[5] < ${d_n}/cfg.tmp
\rm ${d_n}/cfg.tmp

if [ ${#CONFIG} -eq 0 ] ; then
if [ ${#x_d} -eq 0 ] ; then

echo "You are  installing a new configuration"
echo -n " OPA_SRC [Y/n] "
    read answer
    answer=`echo $answer | sed 's/^[yY].*$/y/'`
    if [  -z "$answer" -o "x$answer" = "xy" ]; then
    TAB[1]=OPA_SRC   
echo " OPA_SRC selected "
echo "  "
    else
echo " OPA_SRC Not selected "
echo "  "
    fi
unset answer
echo -n " LIM_SRC_2 [Y/n] "
    read answer
    answer=`echo $answer | sed 's/^[yY].*$/y/'`
    if [  -z "$answer" -o "x$answer" = "xy" ]; then
    TAB[2]=LIM_SRC_2 
echo " LIM_SRC_2 selected "
echo "  "
    else
echo " LIM_SRC_2 Not selected "
echo "  "
    fi
unset answer
echo -n " LIM_SRC_3 [Y/n] "
    read answer
    answer=`echo $answer | sed 's/^[yY].*$/y/'`
    if [  -z "$answer" -o "x$answer" = "xy" ]; then
    TAB[2]=LIM_SRC_3
echo " LIM_SRC_3 selected "
echo "  "
    else
echo " LIM_SRC_3 Not selected "
echo "  "
    fi
unset answer
echo -n " TOP_SRC [Y/n] "
    read answer
    answer=`echo $answer | sed 's/^[yY].*$/y/'`
    if [  -z "$answer" -o "x$answer" = "xy" ]; then
    TAB[3]=TOP_SRC   
echo " TOP_SRC selected "
echo "  "
    else
echo " TOP_SRC Not selected "
echo "  "
    fi
unset answer   
echo -n " C1D_SRC [Y/n] "
    read answer
    answer=`echo $answer | sed 's/^[yY].*$/y/'`
    if [  -z "$answer" -o "x$answer" = "xy" ]; then
    TAB[4]=C1D_SRC   
echo " C1D_SRC selected "
echo "  "
    else
echo " C1D_SRC Not selected "
echo "  "
    fi
unset answer
echo -n " NST_SRC [Y/n] "
    read answer
    answer=`echo $answer | sed 's/^[yY].*$/y/'`
    if [  -z "$answer" -o "x$answer" = "xy" ]; then
    TAB[5]=NST_SRC   
echo " NST_SRC selected "
echo "  "
    else
echo " NST_SRC Not selected "
echo "  "
    fi
unset answer

else
set -A TAB ${x_d} 
fi
print ${m_n} ${TAB[*]} >> ${d_n}/cfg.txt
fi


#- Create the config  --
r_cfg='ORCA2_LIM'
if [ ${#x_r} -ne 0 ]; then
r_cfg=${x_r}
fi

if  [ ! -d ${d_n}/../../config/${m_n} ]; then
echo " The new configuration ${x_n} will be based on ${r_cfg} following files:"
echo "`ls ../${r_cfg}/AA_make*` "
echo "`ls ../${r_cfg}/scripts/BB_make*` "
echo "`ls ../${r_cfg}/EXP00/*` "
echo "`ls ../${r_cfg}/MY_SRC/*` "
echo "  "
\mkdir  ${d_n}/../../config/${m_n} 
\mkdir  ${d_n}/../../config/${m_n}/scripts
\mkdir  ${d_n}/../../config/${m_n}/EXP00
\mkdir  ${d_n}/../../config/${m_n}/MY_SRC
\cp -R ${d_n}/../../config/${r_cfg}/AA_make ${d_n}/../../config/${m_n}/. 
\cp -R ${d_n}/../../config/${r_cfg}/AA_make.ldef ${d_n}/../../config/${m_n}/. 
\cp -R ${d_n}/../../config/${r_cfg}/scripts/BB_make ${d_n}/../../config/${m_n}/scripts/. 
\cp -R ${d_n}/../../config/${r_cfg}/scripts/BB_make.ldef ${d_n}/../../config/${m_n}/scripts/. 
\cp -R ${d_n}/../../config/${r_cfg}/EXP00/* ${d_n}/../../config/${m_n}/EXP00/. 
\cp -R ${d_n}/../../config/${r_cfg}/MY_SRC/* ${d_n}/../../config/${m_n}/MY_SRC/. 
fi
#- Create the WORK --
d_m=`find ${d_n}/../.. -name ${m_n}`  ;
[ -d ${d_m}/WORK ] || mkdir ${d_m}/WORK;
cd ${d_m}/WORK

#- Clean links and librairies
[ -n "`\ls`" ] && \rm * 

#- Find the number of directories ---
NDIR=${#TAB[*]}

#- Creating the good links, at first on OPA_SRC ---
    echo " Creating ${m_n}/WORK = ${TAB[*]} for ${m_n}"

    i=0
    while [ i -lt $NDIR ]
    do
	[ ${#TAB[i]} -ne 0 ] && [ "${TAB[i]}" = "OPA_SRC" ] && ln -sf ../../../modeles/NEMO/OPA_SRC/*.[Ffh]90 .
	[ ${#TAB[i]} -ne 0 ] && [ "${TAB[i]}" = "OPA_SRC" ] && ln -sf ../../../modeles/NEMO/OPA_SRC/*/*.[Ffh]90 . && break
	let i=$i+1
    done

    i=0
    while [ i -lt $NDIR ]
    do
      if [ "${TAB[i]}" = "LIM_SRC_2" ]; then
	  ln -sf ../../../modeles/NEMO/LIM_SRC_2/*.[Ffh]90 .  
      
      elif [ "${TAB[i]}" = "LIM_SRC_3" ]; then
	  ln -sf ../../../modeles/NEMO/LIM_SRC_3/*.[Ffh]90 .  

      elif [ "${TAB[i]}" = "TOP_SRC" ]; then
	   ln -sf ../../../modeles/NEMO/TOP_SRC/*.[Ffh]90 . 
	   ln -sf ../../../modeles/NEMO/TOP_SRC/*/*.[Ffh]90 . 
      
      elif [ "${TAB[i]}" = "OFF_SRC" ]; then  
	  ln -sf ../../../modeles/NEMO/OFF_SRC/*.[Ffh]90 .
	  ln -sf ../../../modeles/NEMO/OFF_SRC/*/*.[Ffh]90 .

      elif [ "${TAB[i]}" = "C1D_SRC" ]; then  
	  ln -sf ../../../modeles/NEMO/C1D_SRC/*.[Ffh]90 .

      elif [ "${TAB[i]}" = "NST_SRC" ]; then  
	 \rm agrif_opa_interp.F90
	 \rm agrif_opa_sponge.F90
	 \rm agrif_opa_update.F90
	 \rm agrif_top_interp.F90
	 \rm agrif_top_sponge.F90
	 \rm agrif_top_update.F90
	  ln -sf ../../../modeles/NEMO/NST_SRC/*.[Ffh]90 .
      fi
      let i=$i+1
    done

    ln -sf ../../../*/${m_n}/scripts/BB_make AA_make ; 
    ln -sf ../../../*/${m_n}/scripts/BB_make.ldef AA_make.ldef ;


    if [ -d ../../../config/${m_n}/MY_SRC ]; then
        f_list=`\ls ../../../config/${m_n}/MY_SRC/*.[Ffh]90`  ;
        if [ ${#f_list} -ne 0 ] ; then
            echo " "
            echo " Files in the ../config/${m_n}/MY_SRC directory will be taken into account"
            echo " "
	    for i in `ls ../../../config/${m_n}/MY_SRC/*.[Ffh]90`
	       do
	       [ -f $i ] && \rm $i  
	       [ -f ../../../config/${m_n}/MY_SRC/$i ] &&  ln -sf ../../../config/${m_n}/MY_SRC/${i} .
	     done
        fi
    fi

#- Building the standard list of source files ---
cat >.patron <<"EOF" 
CVS
SRC_PARAM
SRC_FILE_LIST
tmplist
AA_make
KEY_CPP
Makefile
*?.o
 i.?*
*?.L
.patron
bloc.com
para.com
defcst.f
fontbc.f
icdyna.f
thersf.f
OPAFILES
EOF

[ ! -d ../../../config/${m_n}/scripts/Attic ] && \mkdir ../../../config/${m_n}/scripts/Attic

#- Writing KEY_CPP file ---
sed -e "s/#-Q- sxnec *//" -e /^P_P/\!d -e "s/P_P = //" AA_make.ldef > ../../../config/${m_n}/scripts/Attic/KEY_CPP  
ln -sf ../../../config/${m_n}/scripts/Attic/KEY_CPP .  

#- Creating SRC list ---
ls -1 | fgrep -v -f .patron  > ../../../config/${m_n}/scripts/Attic/SRC_FILE_LIST
ln -sf ../../../config/${m_n}/scripts/Attic/SRC_FILE_LIST .
cp SRC_FILE_LIST SRC_FILE_LIST.temp   

#- Save new configuration and directories names ---
print ${m_n} ${TAB[*]} >.config 

#- Create repository to store modified files
[ ! -d ../../../config/${m_n}/MY_SRC ] && \mkdir ../../../config/${m_n}/MY_SRC
cp ../../../modeles/NEMO/OPA_SRC/par_oce.F90 ../../../config/${m_n}/MY_SRC/par_oce.F90_keep
if [ -d ../../../config/NVTK/2TEST ]; then
    f_2test=`\ls ../../../config/NVTK/2TEST/*.[Ffh]90`  ;
    if [ ${#f_2test} -ne 0 ] ; then
        echo " "
        echo " Files in the ../config/NVTK/2TEST directory will be taken into account"
        echo " "
        for i in `ls ../../../config/NVTK/2TEST/*.[Ffh]90`
          do
             ln -sf ../../../config/NVTK/2TEST/${i} ../../../config/${m_n}/MY_SRC/.
          done
    fi
fi

unset m_n
unset TAB
unset CONFIG
exit 0;
