#!/bin/sh
#+
#
# NAME
# ====
#
# ``script_correc.sh`` - run ``script_correc.m`` in batch mode either with ``octave`` or ``matlab``
#
# DESCRIPTION
# ===========
#
# script_correc.sh run script_correc.m in batch mode either with ``octave`` or ``matlab`` using ``-a`` option.
#
# At the end of ``script_correc.m`` execution, output files are renamed from 
# \*.eps to *application*\ _*hostname*\ _*YYYYMMDDTHHMMSSZ*.eps and moved to ${TP_MLP_AEROSOLS_LOG}.
#
# This might be usefull to compare results on the same or different machine 
# with different applications or releases of application.
#
# Log file is written on 
# ${TP_MLP_AEROSOLS_LOG}/script_correc.log.\ *YYYYMMDDTHHMMSSZ*
#
# In this log, you can see stdout and stderr of ``script_correc.m`` and 
# timing statistics of the run.
#
# EXAMPLES
# ========
#
# To run ``script_correc.m`` with matlab :
#
# ::
#
# $ ./script_correc.sh -a matlab
#
#
# To run  script_correc.m with octave
#
# ::
#
# $ ./script_correc.sh -a octave
#
#
# SEE ALSO
# ========
#
# tp_mlp_aerosols_profile.sh_
#
# .. _tp_mlp_aerosols_profile.sh: tp_mlp_aerosols_profile.sh.html
#
# script_correc.m_
#
# .. _script_correc.m: script_correc.m.html
#
#
# TODO
# ====
#
# suppress figure on screen using octave
#
# use init_file option with tp_mlp_aerosols_startup.m (or check link
# on .octaverc or .matlabrc)
#
# move this tool in an administration part
#
# write tp_mlp_aerosols_profile.sh
#
# ok on windows ?
#
# EVOLUTIONS
# ==========
#
# $Id$
#
# - fplod 2009-08-19T14:58:54Z aedon.locean-ipsl.upmc.fr (Darwin)
#
#   * creation
#
#-
system=$(uname)
case "${system}" in
AIX|IRIX64)
  echo " www : no specific posix checking"
;;
*)
  set -o posix
;;
esac
#
command=$(basename ${0})
log_date=$(date -u +"%Y%m%dT%H%M%SZ")
#
#
TP_MLP_AEROSOLS_LOG=/usr/temp/${LOGNAME}/
mkdir -p ${TP_MLP_AEROSOLS_LOG}
TP_MLP_AEROSOLS=/usr/temp/fplod/tp_mlp_aerosols_ws/
#
usage=" Usage : ${command} -a application"
#
# default
application=octave
minargcount=2
if [ ${#} -lt ${minargcount} ]
then
  echo "eee : not enought arguments"
  echo "${usage}"
  exit 1
fi
#
while [ ! -z "${1}" ]
do
 case ${1} in
 -a)
  application=${2}
  shift
 ;;
 *) # anything else
  echo "eee : ${usage}"
  exit 1
 ;;
 esac
 shift # next flag
done
#
set -u
#
log=${TP_MLP_AEROSOLS_LOG}/$(basename ${command} .sh).log.${log_date}
echo "command = ${command}" 1>> ${log}
echo "runtime = ${log_date}" 1>> ${log}
echo "hostname = $(hostname)" 1>> ${log}
echo "application = ${application}" 1>> ${log}
# test if application available
tool=${application}
type ${tool} 1> /dev/null 2>&1
status=${?}
if [ ${status} -ne 0 ]
then
 echo " eee : ${tool} not found"
 exit 1
fi
unset status
#
script_base=${TP_MLP_AEROSOLS}/private/script_correc.m
script=${TP_MLP_AEROSOLS_LOG}/script.m
#
rm ${script} 2> /dev/null
#
{
echo "addpath('../toolbox')"
echo "tp_mlp_aerosols_startup"
echo "more off"
echo "script_correc"
echo "quit"
} > ${script}
#
cd $(dirname ${script_base})
#
case ${application} in
matlab)
 time matlab -nojvm -nosplash -nodisplay <  ${script}  1>> ${log} 2>&1
;;
octave)
 time octave --silent ${script} 1>> ${log} 2>&1
;;
esac
#
for file in ./*.eps
do
 mv ${file} ${TP_MLP_AEROSOLS_LOG}/${file}.${application}_$(hostname)_${log_date}.eps
done
#
rm ${script} 2> /dev/null
exit
