#!/bin/sh
#============================================================================
#
#     This file is part of the Code_Saturne Kernel, element of the
#     Code_Saturne CFD tool.
#
#     Copyright (C) 1998-2008 EDF S.A., France
#
#     contact: saturne-support@edf.fr
#
#     The Code_Saturne Kernel is free software; you can redistribute it
#     and/or modify it under the terms of the GNU General Public License
#     as published by the Free Software Foundation; either version 2 of
#     the License, or (at your option) any later version.
#
#     The Code_Saturne Kernel is distributed in the hope that it will be
#     useful, but WITHOUT ANY WARRANTY; without even the implied warranty
#     of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#     GNU General Public License for more details.
#
#     You should have received a copy of the GNU General Public License
#     along with the Code_Saturne Kernel; if not, write to the
#     Free Software Foundation, Inc.,
#     51 Franklin St, Fifth Floor,
#     Boston, MA  02110-1301  USA
#
#============================================================================
#
# Handle SYRTHES files when coupled: compile, copy data, or copy results
#
# Command-line arguments are described by the usage message below.
#
########################################################################

# Indicate usage

if [ "$1" = "" -o "$1" = "-h" -o "$1" = "--help" ] ; then
  echo "Usage:"
  echo
  echo "  $0 -compile -cs-bindir=<dir> [-src-syr=<dir>] [-copy-dir=<dir>]"
  echo "              [-log-dir=<dir>] [-exec-dir=<dir>]"
  echo
  echo "   Indicate where to find Code_Saturne main user script:"
  echo "     -cs-bindir: directory where is installed Code_Saturne binaries."
  echo
  echo "   Compile and link SYRTHES, with the following optional arguments:"
  echo "     -src-syr:   source code directory; if none, link only."
  echo "     -src-copy:  directory in which sources are copied."
  echo "     -log:       file to which the compilation/link log is copied."
  echo "     -exec-dir:  SYRTHES execution diretory."
  echo
  echo "or:"
  echo
  echo "  $0 -copy-data -syrthes-env=<dir> [-exec-dir=<dir>]"
  echo
  echo "   Copy data to execution directory based on syrthes.env file:"
  echo "     -syrthes-env: full pathname for syrthes.env file."
  echo "     -exec-dir: SYRTHES execution directory."
  echo
  echo "or:"
  echo
  echo "  $0 -copy-results -result-dir=<dir> [-exec-dir=<dir>]"
  echo
  echo "   Copy results to result directory:"
  echo "     -result-dir:  directory in which results are saved."
  echo "     -exec-dir: SYRTHES execution directory."
  echo
  exit 0
fi

# Parse secondary arguments

unset CS_BINDIR
unset SRC_SYR
unset EXEC_DIR
unset RESULT_DIR
unset SYRTHES_ENV

for arg in $*
do

  is_cs_bindir=`echo $arg | grep -e "-cs-bindir="`
  if [ "$is_cs_bindir" != "" ] ; then
    CS_BINDIR=`echo $arg | sed -e "s/-cs-bindir=//"`
  fi

  is_exec_dir=`echo $arg | grep -e "-exec-dir="`
  if [ "$is_exec_dir" != "" ] ; then
    EXEC_DIR=`echo $arg | sed -e "s/-exec-dir=//"`
  fi

  is_src_syr=`echo $arg | grep -e "-src-syr="`
  if [ "$is_src_syr" != "" ] ; then
    SRC_SYR=`echo $arg | sed -e "s/-src-syr=//"`
  fi

  is_log=`echo $arg | grep -e "-log="`
  if [ "$is_log" != "" ] ; then
    LOG_COPY=`echo $arg | sed -e "s/-log=//"`
  fi

  is_src_copy=`echo $arg | grep -e "-src-copy="`
  if [ "$is_src_copy" != "" ] ; then
    SRC_COPY=`echo $arg | sed -e "s/-src-copy=//"`
  fi

  is_syrthes_env=`echo $arg | grep -e "-syrthes-env="`
  if [ "$is_syrthes_env" != "" ] ; then
    SYRTHES_ENV=`echo $arg | sed -e "s/-syrthes-env=//"`
  fi

  is_result_dir=`echo $arg | grep -e "-result-dir="`
  if [ "$is_result_dir" != "" ] ; then
    RESULT_DIR=`echo $arg | sed -e "s/-result-dir=//"`
  fi

done

if [ "${EXEC_DIR}" = "" ]
then
  EXEC_DIR="."
fi

########################################################################

if [ "$1" = "-compile" ] ; then

  echo
  echo  "  **********************************************************"
  echo  "   Compilation of the user subroutines and link of SYRTHES"
  echo  "  **********************************************************"

  cur_dir=`pwd`
  user_files=no
  mkdir $EXEC_DIR/src_syrthes
  cd $EXEC_DIR/src_syrthes
  if [ "${SRC_SYR}" != "" ]
  then
    for f in $SRC_SYR/*.[F,c,h] ; do
      if [ -f $f ] ; then
        cp ${f} .
        user_files=yes
      fi
    done
  fi

  ${CS_BINDIR}/code_saturne compile --syrthes > ./compil.log 2>&1
  make_ret=$?

  if [ "${LOG_COPY}" != "" ] ; then
    cp ./compil.log ${LOG_COPY}
  fi

  if [ $user_files = yes -a "${SRC_COPY}" != "" ] ; then
    if [ ! -d ${SRC_COPY} ] ; then
      mkdir ${SRC_COPY}
    fi
    if [ -d ${SRC_COPY} ] ; then
      for f in *.[f,F,c,h] ; do
        if [ -f ${f} ] ; then
          cp -R ${f} ${SRC_COPY}/
          fbase=`basename ${f}`
          # chmod a-w $RESULT_DIR/${fbase}
        fi
      done
    fi
  fi

  if [ $make_ret -ne 0 ] ; then
    echo "SYRTHES compile or link error."
    exit 1
  fi

  mv syrthes $cur_dir/
  cd $cur_dir

fi

########################################################################

# Extract information from the syrthes.env file

if [ "$1" = "-copy-data" -o "$1" = "-copy-results" ] ; then

  if [ "$1" = "-copy-results" ] ; then
    SYRTHES_ENV="${EXEC_DIR}/syrthes.env.ref"
    if [ ! -f $SYRTHES_ENV ] ; then
      SYRTHES_ENV="${EXEC_DIR}/syrthes.env"
    fi
    if [ ! -f $SYRTHES_ENV ] ; then
      echo "No syrthes.env in ${EXEC_DIR}; no results copied."
      exit 1
    fi
  elif [ ! -f $SYRTHES_ENV ] ; then
    echo "No syrthes.env defined; no data copied."
    exit 1
  fi

  SYR_IN=`grep "AMONT :" $SYRTHES_ENV | awk -F: '{print $NF}' | sed -e "s/ //g"`
  SYR_OUT=` grep "AVAL :"  $SYRTHES_ENV | awk -F: '{print $NF}' | sed -e "s/ //g"`
  SYR_RESTART=`grep "SUITE :" $SYRTHES_ENV | awk -F: '{print $NF}' | sed -e "s/ //g"`
  SYRTHES_DATA=`grep "DONNEES DU CALCUL :" $SYRTHES_ENV | awk -F: '{print $NF}'`
  SYRTHES_GEOM=`grep "GEOMETRIE SOLIDE :" $SYRTHES_ENV | awk -F: '{print $NF}'`
  SYRTHES_SUIT=`grep "SUITE SOLIDE RESU :" $SYRTHES_ENV | awk -F: '{print $NF}'`
  SYRTHES_DATA_RAY=`grep "DONNEES POUR LE RAYONNEMENT :" $SYRTHES_ENV | awk -F: '{print $NF}'`
  SYRTHES_MAIL_RAY=`grep "MAILLAGE RAYONNEMENT :" $SYRTHES_ENV | awk -F: '{print $NF}'`
  SYRTHES_CORR=`grep "STOCKAGE DES CORRESPONDANTS :" $SYRTHES_ENV | awk -F: '{print $NF}'`
  SYRTHES_FFOR_RAY=`grep "FACTEURS DE FORME RAYONNEMENT :" $SYRTHES_ENV | awk -F: '{print $NF}'`
  SYRTHES_CORR_RAY=`grep "STOCKAGE DES CORRESPONDANTS RAYONNEMENT :" $SYRTHES_ENV | awk -F: '{print $NF}'`
  SYRTHES_OUT_RES1=`grep "RESU SYRTHES 1 :" $SYRTHES_ENV | awk -F: '{print $NF}'`
  SYRTHES_OUT_RES2=`grep "RESU SYRTHES 2 :" $SYRTHES_ENV | awk -F: '{print $NF}'`
  SYRTHES_OUT_CHR2=`grep "CHRONO SYRTHES 2 :" $SYRTHES_ENV | awk -F: '{print $NF}'`
  SYRTHES_OUT_HIST=`grep "HISTORIQUE SOLIDE RESULTAT :" $SYRTHES_ENV | awk -F: '{print $NF}'`
  SYRTHES_OUT_MAIL_PEAU_FLUIDE=`grep "MAILLAGE PEAU FLUIDE :" $SYRTHES_ENV | awk -F: '{print $NF}'`
  SYRTHES_OUT_RESU_PEAU_FLUIDE=`grep "RESULTATS PEAU FLUIDE :" $SYRTHES_ENV | awk -F: '{print $NF}'`
  SYRTHES_OUT_CHR_PEAU_FLUIDE=`grep "CHRONO PEAU FLUIDE :" $SYRTHES_ENV | awk -F: '{print $NF}'`

  # if empty, we use default names for results:
  if [ -z "$SYRTHES_CORR" ] ; then
    SYRTHES_CORR="corresp"
  fi
  if [ -z "$SYRTHES_CORR_RAY" ] ; then
    SYRTHES_CORR_RAY="corresp_ray"
  fi
  if [ -z "$SYRTHES_FFOR_RAY" ] ; then
    SYRTHES_FFOR_RAY="fdf_ray"
  fi
  if [ -z "$SYRTHES_OUT_RES1" ] ; then
    SYRTHES_OUT_RES1="geoms"
  fi
  if [ -z "$SYRTHES_OUT_RES2" ] ; then
    SYRTHES_OUT_RES2="resus"
  fi
  if [ -z "$SYRTHES_OUT_CHR2" ] ; then
    SYRTHES_OUT_CHR2="chronos"
  fi
  if [ -z "$SYRTHES_OUT_HIST" ] ; then
    SYRTHES_OUT_HIST="histos"
  fi
  if [ -z "$SYRTHES_OUT_MAIL_PEAU_FLUIDE" ] ; then
    SYRTHES_OUT_MAIL_PEAU_FLUIDE="geomf_ef"
  fi
  if [ -z "$SYRTHES_OUT_RESU_PEAU_FLUIDE" ] ; then
    SYRTHES_OUT_RESU_PEAU_FLUIDE="resuf_ef"
  fi
  if [ -z "$SYRTHES_OUT_CHR_PEAU_FLUIDE" ] ; then
    SYRTHES_OUT_CHR_PEAU_FLUIDE="chronof_ef"
  fi
  if [ -z "$SYRTHES_OUT_MAIL_RAY" ] ; then
    SYRTHES_OUT_MAIL_RAY="resu_ray.geom"
  fi
  if [ -z "$SYRTHES_OUT_RESU_RAY" ] ; then
    SYRTHES_OUT_RESU_RAY="resu_ray"
  fi
  if [ -z "$SYRTHES_OUT_CHR_RAY" ] ; then
    SYRTHES_OUT_CHR_RAY="chrono_ray"
  fi
  if [ -z "$SYRTHES_OUT_HIST_RAY" ] ; then
    SYRTHES_OUT_HIST_RAY="histor"
  fi

fi

########################################################################

# Build a local copy of the syrthes.env file in which all files are
# local. The initial file is backed-up so that it may be copied with
# results afterwards.

if [ "$1" = "-copy-data" ] ; then

  cp $SYRTHES_ENV  ${EXEC_DIR}/syrthes.env.ref

  sed -e "s/:.*\//: /" \
      -e "s/AMONT :.*/AMONT : .\//" \
      -e "s/AVAL :.*/AVAL : .\//" \
      -e "s/SUITE :.*/SUITE : .\//" \
    ${SYRTHES_ENV} > ${EXEC_DIR}/syrthes.env.tmp

  mv ${EXEC_DIR}/syrthes.env.tmp ${EXEC_DIR}/syrthes.env

  # Transfer SYRTHES files

  for var in $SYRTHES_DATA $SYRTHES_GEOM $SYRTHES_SUIT $SYRTHES_DATA_RAY $SYRTHES_MAIL_RAY ; do
    cp $SYR_IN/$var $EXEC_DIR/`echo $var | sed -e "s/.*\///"`
  done
  for var in $SYRTHES_CORR $SYRTHES_FFOR_RAY $SYRTHES_CORR_RAY ; do
    cp $SYR_RESTART/$var $EXEC_DIR/`echo $var | sed -e "s/.*\///"`
  done

fi

########################################################################

if [ "$1" = "-copy-results" ] ; then

  if [ "${RESULT_DIR}" = "" ] ; then
    echo "No result directory  defined; no results copied."
    exit 1
  elif [ "${RESULT_DIR}" = "${EXEC_DIR}" ] ; then
    exit 0
  fi

  if [ ! -d "${RESULT_DIR}" ] ; then
    mkdir $RESULT_DIR
    if [ $? -ne 0 ] ; then
      echo Failure creating ${RESULT_DIR}
      exit 1
    fi
  fi
  for var in $SYRTHES_ENV $SYRTHES_DATA $SYRTHES_DATA_RAY \
             $SYRTHES_CORR $SYRTHES_CORR_RAY $SYRTHES_FFOR_RAY \
             $SYRTHES_OUT_RES1 $SYRTHES_OUT_RES2 $SYRTHES_OUT_CHR2 \
             $SYRTHES_OUT_HIST \
             $SYRTHES_OUT_MAIL_PEAU_FLUIDE $SYRTHES_OUT_RESU_PEAU_FLUIDE \
             $SYRTHES_OUT_CHR_PEAU_FLUIDE \
             $SYRTHES_OUT_MAIL_RAY $SYRTHES_OUT_RESU_RAY \
             $SYRTHES_OUT_CHR_RAY \
             $SYRTHES_OUT_HIST_RAY ; do
    f=$EXEC_DIR/`basename $var`
    if [ -f $f ] ; then
      cp $f ${RESULT_DIR}/
    fi
  done

fi
