#! /bin/sh
#+
#
# .. _elogd.sh:
#
# ========
# elogd.sh
# ========
#
# --------------------------------------------------------------------------
# edit the most recent :file:`${IRCAAM_LOG}/{action}.log.{YYYYMMDDTHHMMSSZ}`
# --------------------------------------------------------------------------
#
# SYNOPSIS
# ========
#
# ::
#
#  $ elogd.sh action
#
#
# DESCRIPTION
# ===========
#
# ``elogd.sh`` launch ${EDITOR} on the most recent release of :file:`${IRCAAM_LOG}/{action}.log.{YYYYMMDDTHHMMSSZ}`.
#
#
# SEE ALSO
# ========
#
# :ref:`ircaam_profile.sh`
#
# :ref:`dlogd.sh`
#
# :ref:`plogd.sh`
#
# :ref:`tlogd.sh`
#
# EVOLUTIONS
# ==========
#
# $Id$
#
# - fplod 20100226T151441Z aedon.locean-ipsl.upmc.fr (Darwin)
#
#   * replace ????-??-??T??:??:??Z by ????????T??????Z
#
# - fplod 2006-02-20T16:38:30Z aedon.lodyc.jussieu.fr (Darwin)
#
#   * posix
#
#-
#
set -u
system=$(uname)
case "${system}" in
   AIX|IRIX64)
      echo " www : no specific posix checking"
   ;;
   *)
      set -o posix
   ;;
esac
unset system
#
LANG=POSIX
#
command=$(basename ${0})
#
if [ ! "${EDITOR}" ]
then
   echo " ${command} : eee : EDITOR undefined" >&2
   exit 1
fi
if [ "${1}" = "" ]
then
   echo "${command} : eee : 1st parameter missing" >&2
   exit 1
fi
#
# check for ${IRCAAM_LOG} definition
if [ "${IRCAAM_LOG}" = "" ]
then
   echo " eee : \${IRCAAM_LOG} not defined"
   exit 1
fi
#
# check for ${IRCAAM_LOG} existence
if [ ! -d ${IRCAAM_LOG} ]
then
   echo " eee : ${IRCAAM_LOG} not found"
   exit 1
fi
#
# check for permission access on IRCAAM_LOG
if [ ! -x ${IRCAAM_LOG} ]
then
   echo " eee : ${IRCAAM_LOG} not reachable"
   exit 1
fi
#
log=$(find ${IRCAAM_LOG} -name "${1}.log.????????T??????Z" | sort | \
tail -n 1)
tmp=${?}
if [ ${tmp} -eq 0 ]
then
   if [ "${log}" = "" ]
   then
      echo "${command} : eee : no release of ${IRCAAM_LOG}/${1}.log" >&2
      exit 1
   else
      # edit
      ${EDITOR} ${log}
      exit 0
   fi
else
   exit 1
fi
