#! /bin/sh
#+
#
# .. program:: tst_pltt_zero.sh
#
# .. _tst_pltt_zero.sh:
#
# ================
# tst_pltt_zero.sh
# ================
#
# SYNOPSIS
# ========
#
# .. code-block:: bash
#
#    tst_pltt_zero.sh --save_image saveimage
#
# DESCRIPTION
# ===========
#
# Run :ref:`tst_pltt_zero.pro`
#
# We suppose here that all inputs data are already available locally.
#
# .. option:: --save_image <save_image>
#
#    y or n
#
# Some IDL programs are launched here using ${IDL_CMD} command
#
# Log file is written on
# :file:`${PROJECT_LOG}/tst_pltt_zero.log.{YYYYMMDDTHHMMSSZ}`
#
# .. only:: man
#
#    Figure is visible on PDF and HTML documents only.
#
# .. only:: html or latex
#
#     .. graphviz::
#
#        digraph tst_pltt_zero {
#
#           log [shape=ellipse,
#           fontname=Courier,
#           label="${PROJECT_LOG}/tst_pltt_zero.log{YYYYMMDDTHHMMSSZ}"];
#
#           tst_pltt_zero_sh [shape=box,
#           fontname=Courier,
#           color=blue,
#           URL="http://forge.ipsl.jussieu.fr/saxo/browser/trunk/SRC/Test/tst_pltt_zero.sh",
#           label="${PROJECT}/SRC/Tests/tst_pltt_zero.sh"];
#
#           tst_pltt_zero_pro [shape=box,
#           fontname=Courier,
#           color=blue,
#           URL="http://forge.ipsl.jussieu.fr/saxo/browser/trunk/SRC/Test/tst_pltt_zero.pro",
#           label="${PROJECT}/SRC/Tests/tst_pltt_zero.pro"];
#
#           {data_test} -> {tst_pltt_zero_sh tst_pltt_zero_pro} -> {log png}
#
#         }
#
# EXAMPLES
# ========
#
# To test in foreground:
#
# .. code-block:: bash
#
#    tst_pltt_zero.sh --save_image y
#
# To test in background:
#
# .. code-block:: bash
#
#    tst_pltt_zero.sh --save_image y &
#
# And look at log file with:
#
# .. code-block:: bash
#
#    tlogd.sh tst_pltt_zero
#
# and of course on image files in imagedir define in ${IDL_STARTUP}
#
# SEE ALSO
# ========
#
# |idl|
#
# Use :
# :ref:`tst_pltt_zero.pro`
#
# TODO
# ====
#
# minimize dependencies on FPTOOLS (log, etc)
#
# PATH should contains $SAXO_DIR/SRC/Tests/
#
# improve graph
#
# EVOLUTIONS
# ==========
#
# $Id: tst_pltt_zero.sh 288 2013-05-16 15:12:36Z pinsard $
#
# $URL$
#
# - fplod 20130802T101039Z cratos.locean-ipsl.upmc.fr (Linux)
#
#   * creation to check difference between behaviour of tst_pltt_zero.pro
#     in an IDL session, in a shell session foreground and background
#
#-
system=$(uname)
case "${system}" in
    AIX|IRIX64)
        echo "${command} : ${LINENO} : www : no specific posix checking"
        date_cmd=date
    ;;
    Darwin)
        set -o posix
        date_cmd=gdate
    ;;
    Linux)
        set -o posix
        date_cmd=date
    ;;
   *)
    set -o posix
   ;;
esac
unset system
#
set -u
#
LANG=POSIX
#
command=$(basename ${0})
log_date=$(date -u +"%Y%m%dT%H%M%SZ")
#
usage=" Usage : ${command} [-f] --save_image save_image"
#
hostname=$(hostname)
#
# default
debug=0
force=0
save_image=0
#
minargcount=2
if [ ${#} -lt ${minargcount} ]
then
    echo "${command} : ${LINENO} : eee : not enough arguments"
    echo "${usage}"
    exit 1
fi
unset minargcount
#
while [ ${#} -gt 0 ]
do
    case ${1} in
        --save_image)
            save_image=${2}
            shift
        ;;
        -h)
            echo "${usage}"
            exit 0
        ;;
        *)
            # other choice
            echo "${command} : ${LINENO} : eee : unknown option ${1}"
            echo "${usage}"
            exit 1
        ;;
    esac
    # next flag
    shift
done
unset usage
#
# check parameters
#++
#
tool=${IDL_CMD}
type ${tool} 1> /dev/null 2>&1
status=${?}
if [ ${status} -ne 0 ]
then
    echo "${command} : ${LINENO} : eee : tool ${tool} not found"
    echo "${command} : ${LINENO} : eee : check project_profile.sh sequence"
    exit 1
fi
unset status
unset tool
#
# check for write permission on PROJECT_LOG
if [ ! -w ${PROJECT_LOG} ]
then
    echo "${command} : ${LINENO} : eee : ${PROJECT_LOG} not writable"
    exit 1
fi
#
log=${PROJECT_LOG}/$(basename ${0} .sh).log.${log_date}
echo "[Context]" 1>> ${log}
echo "PID=${$}" 1>>${log}
echo "command=$(basename ${0})" 1>>${log}
echo "hostname=${hostname}" 1>> ${log}
echo "runtime=${log_date}" 1>> ${log}
echo "log=${log}" 1>> ${log}
echo "IDL_CMD=${IDL_CMD}" 1>> ${log}
echo "SAXO_DIR=${SAXO_DIR}" 1>> ${log}
echo "MY_IDL=${MY_IDL}" 1>> ${log}
#++echo "IDL_STARTUP=${IDL_STARTUP}" 1>> ${log}
unset log_date
echo "" 1>> ${log}
echo "[Parameters]" 1>> ${log}
echo "save_image=${save_image}" 1>> ${log}
echo "" 1>> ${log}
#
if [ "${save_image}" == "y" ]
then
    image_arg=", IMAGE=\"${command}\""
else
    image_arg=""
fi
# build IDL script
cat >> ${MY_IDL}/tst_pltt_zero_${$}.pro << EOF
pro tst_pltt_zero_${$}
;
compile_opt idl2, strictarrsubs, logical_predicate
;
@cm_general
IF (key_gdl EQ 0) THEN BEGIN
    s = size(SCOPE_TRACEBACK(/STRUCTURE), /DIMENSION)
    routine = (SCOPE_TRACEBACK(/STRUCTURE))[s-1].Routine
ENDIF ELSE BEGIN
    routine = 'tst_pltt_zero_${$}'
ENDELSE
;
@cm_project
;
save_image = '${save_image}'
@tst_initlev
tst_pltt_zero ${image_arg}
print, routine, ' reach end'
end
EOF
#
# more ${MY_IDL}/tst_pltt_zero_${$}.pro
#
# run IDL or equivalent
${IDL_CMD} << EOF >> ${log} 2>&1
tst_pltt_zero_${$}
EOF
status=${?}
if [ ${status} -ne 0 ]
then
    echo "${command} : ${LINENO} : eee: ${IDL_CMD} failed : ${status}" >> ${log} 2>&1
    cat ${MY_IDL}/tst_pltt_zero_${$}.pro >> ${log} 2>&1
    exit 1
fi
unset status
#++rm ${MY_IDL}/tst_pltt_zero_${$}.pro
#
unset command
unset log
unset hostname
unset usage
#
unset date_cmd
#
# end
#++set
exit
