#!/bin/ksh
#MNH_LIC Copyright 1994-2014 CNRS, Meteo-France and Universite Paul Sabatier
#MNH_LIC This is part of the Meso-NH software governed by the CeCILL-C licence
#MNH_LIC version 1. See LICENSE, CeCILL-C_V1-en.txt and CeCILL-C_V1-fr.txt  
#MNH_LIC for details. version 1.
#set -x
set -e

export CONFIG=${CONFIG-"confdollar"}
export CONFEXP=${CONFEXP:-"exp"}
export STEPDIR=${*:-"."}

#
# Scan all directory an eval_dollar all '*.ihm' files
#
for dir in ${STEPDIR} ; do
if [ ! -d ${dir} ]  ; then 
echo "scandollar ERROR :: "
echo "scandollar ERROR :: this parameter is not a directory = ${dir} " 
echo "scandollar ERROR :: "
exit 1
fi
done

clear
echo "#"
echo "#  read default config file :: --->  CONF_DOLLAR=${CONF_DOLLAR} "

export LOCALPWD=${PWD}

for dir in ${STEPDIR} 
 do
(
  cd ${LOCALPWD}/${dir}
#
# Init user environnement
#
  if [ ! -f ${CONFIG} ]  ; then 
      echo
      echo "scandollar ERROR :: "
      echo "scandollar ERROR :: configuration file 'confdollar' not found in dir=${dir} "
      echo "scandollar ERROR :: USAGE :: prep_dollar [dir_dest*] " 
      echo "scandollar ERROR :: "
      echo
      exit 1 
  fi
  [ ! -x ${CONFIG} ] && chmod +x ${CONFIG}
  echo "#" 
  echo "#  read    user config file :: --->  CONFIG=${CONFIG} "
## init default before user
#  [ -x ${CONF_DOLLAR}  ] && source ${CONF_DOLLAR}       
## init user
  [ -x ${CONFIG}  ] && . ${CONFIG}       
#
# Post conf default environnement
#
  [ -x ${CONF_DOLLAR}  ] && . ${CONF_DOLLAR}       
#
# create experience directory
#
#exit
  mkdir -p ${CONFEXP}
#
# process *.ihm file and put them in ${CONFEXP}
#
  echo "#" 
  echo "#  processes file *.ihm and put them in ${dir}/${CONFEXP}/ :: "
  for file in $( ls -1 *.ihm 2>/dev/null )
    do
    destfile=$(basename ${file} .ihm)
    echo "#          ---> ${file} =>  ${dir}/${CONFEXP}/${destfile} "
    eval_dollar ${file} > ${CONFEXP}/${destfile}
    [ -x ${file} ] && chmod +x ${CONFEXP}/${destfile} || true
  done
)

done 
