#!/bin/ksh
######################################################
# Author : Rachid benshila for ESOPA
# Contact : opatlod@lodyc.jussieu.fr
#
# This script aims to create the NEMOTAM/WORK directory 
# with links to target directories for the sources
# and to link in the WORK the BB_make and BB_make.ldef 
# associated  to a chosen configuration
#
######################################################

###################################
#                                 #
# Beginning of user modifications #
#                                 #
###################################
#- Config name ---
# To add a new configuration called NAME you have to change this script
# 1. Add your configuration in LIST
# 2. Add the directories needed for this config : set -A DIR_NAME DIR1 DIR2 ...
# 3. Run fait_config NAME
# If there is no directory config/NAME/scripts, and in this no BB_make.ldef, 
# the config/ORCA2_LIM_TAM directory is copied under NAME
#
LIST="ORCA2_LIM_TAM"
set -A DIR_ORCA2_LIM_TAM OPATAM_SRC OPA_SRC C1D_SRC
###################################
#                                 #
#    End of user modifications    #
#                                 #
###################################


#- Some tests ---
d_n=$(dirname $0); b_n=$(basename $0);

if   [ ${#} -gt 1 ]; then
    echo 'Only one configuration can be specified in:' 1>&2;
    echo $LIST 1>&2;
    exit 3;
elif [ ${#} -eq 1 ]; then
     m_n="${1}"
elif [ ${#} -eq 0 ]; then
    echo 'You must specify one configuration in :' 1>&2;
    echo $LIST 1>&2;
    echo 'You can also complete fait_config to add a new one'
    exit 3;
fi

#- Create the WORK --
d_m=`find ${d_n}/.. -name NEMOTAM`  ;
[ -d ${d_m}/WORK ] || mkdir ${d_m}/WORK;
cd ${d_m}/WORK

#- Clean links and librairies
[ -f Makefile ] && gmake clean 
[ -n "`\ls`" ] && \rm -rf * 

#- Find the number of directories ---
eval NDIR=\${#DIR_${m_n}[*]}

#- Build a working array TAB containing the directories ---
i=0
while [ i -lt $NDIR ]
do
    eval  TAB[i]=\${DIR_${m_n}[i]}
    let i=i+1
done

#- Creating the good links, at first on OPATAM_SRC ---
if [ ${#TAB[*]} -ne 0 ] ; then
    echo " Creating NEMOTAM/WORK = ${TAB[*]} for ${m_n}"

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

      fi
      let i=$i+1
    done	 		

    [ ! -d ../../../config/${m_n} ] && \cp -R ../../../config/ORCA2_LIM_TAM ../../../config/${m_n} 
    ln -sf ../../../*/${m_n}/scripts/BB_make AA_make ; 
    ln -sf ../../../*/${m_n}/scripts/BB_make.ldef AA_make.ldef ;

else

    echo "Configuration "${m_n}" not supported" 1>&2
    echo 'Make your choice in :' 1>&2
    echo $LIST 1>&2
    echo 'You can also complete fait_config to add a new one'
    exit 1

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
ls -1 | fgrep -v -f .patron  >SRC_FILE_LIST ; cp SRC_FILE_LIST SRC_FILE_LIST.temp ;

#- Writing KEY_CPP file ---
sed -e /^P_P/\!d -e "s/P_P = //" AA_make.ldef > KEY_CPP;    

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

exit 0;
