#! /bin/sh
#
#+
#
# .. program:: correct_terminology.sh
#
# .. _correct_terminology.sh:
#
# =======================
# correct_terminology.sh
# =======================
#
# SYNOPSIS
# ========
#
# ::
#
#  $ correct_terminology.sh [--debug] -good goodend -bad badend -d directory
#
# DESCRIPTION
# ===========
#
# .. option:: --debug
#
#    If this option is set, :samp:`ncdump -v tt` will be added to log file
#
# .. option:: -good goodend
#
#    good end part of the name
#
# .. option:: -bad badend
#
#    bad end part of the name
#
# .. option:: -d <dirin>
#
#    base location of the dataset to be changed
#
# Modify filenames in the dataset based under dirin parameter
#
# Log file is written on :file:`${PROJECT_LOG}/correct_terminology.sh.log.{YYYYMMDDTHHMMSSZ}`
#
#     .. graphviz::
#
#        digraph correct_terminology {
#
#           correct_terminology [shape=box,
#           fontname=Courier,
#           color=blue,
#           URL="http://forge.ipsl.jussieu.fr/tropflux/browser/trunk/src/correct_terminology.sh",
#           label="${PROJECT}/src/correct_terminology.sh"];
#
#           {filein} -> {correct_terminology} -> {fileout}
#
#       }
#
# EXAMPLES
# ========
#
# To modify files under /usr/lodyc/incas/fplod/tropflux_d/to_be_published/ because files are not named
# :file:`var_tropflux_1d_${yyyy}_{yyyy}.nc` but :file:`var_tropflux_1d_{yyyy}_{month}{yyyy}.nc`::
#
#  $ correct_terminology.sh -d /usr/lodyc/incas/fplod/tropflux_d/to_be_published/ -good "1979_2013" -bad "1979_march2013"
#
# And look at log file with ::
#
#  $ tlogd.sh correct_terminology
#
# SEE ALSO
# ========
#
# :ref:`updatedata`
#
# :ref:`project_profile.sh`
#
# TODO
# ====
#
# coding rules
#
# overwrite : may be an other option to avoid scratch
#
# debug not used
#
# build file name according to time in the file instead of parameter
#
# EVOLUTIONS
# ==========
#
# $Id$
#
# $URL$
#
# - fplod 20130726T112836Z cratos.locean-ipsl.upmc.fr (Linux)
#
#   * creation
#
#-
system=$(uname)
case "${system}" in
   AIX|IRIX64)
      echo "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
#
LANG=POSIX
#
set -u
#
command=$(basename ${0})
log_date=$(date -u +"%Y%m%dT%H%M%SZ")
#
usage=" Usage : ${command} [--debug] -d dirin -good yyyymmdd_yyyymmdd -bad anything"
#
hostname=$(hostname)
#
# default
debug=0
#
minargcount=0
if [ ${#} -lt ${minargcount} ]
then
   echo "${command} : eee : not enought arguments"
   echo "${usage}"
   exit 1
fi
#
while [ ${#} -gt 0 ]
do
   case ${1} in
      -good)
         goodend=${2}
         shift
      ;;
      -bad)
         badend=${2}
         shift
      ;;
      -d)
         dirin=${2}
         shift
      ;;
      --debug)
         debug=1
      ;;
      *)
        # anything else
        echo "${command} : eee : unknown option ${1}"
        echo "${command} : eee : ${usage}"
        exit 1
      ;;
   esac
   # next flag
   shift
done
#
# check parameters
if [ ! -d ${dirin} ]
then
   echo " eee : ${dirin} not found"
   exit 1
fi
#
# check for permission on dirin
if [ ! -x ${dirin} ]
then
   echo " eee : ${dirin} not reachable"
   exit 1
fi
#++ check good validity
#
# check for ${PROJECT_LOG} definition
if [ "${PROJECT_LOG}" = "" ]
then
   echo "${command} : ${LINENO} : eee : \${PROJECT_LOG} not defined"
   exit 1
fi
#
# check for ${PROJECT_LOG} existence
if [ ! -d ${PROJECT_LOG} ]
then
   echo "${command} : eee : ${PROJECT_LOG} not found"
   exit 1
fi
#
# check for permission access on PROJECT_LOG
if [ ! -x ${PROJECT_LOG} ]
then
   echo "${command} : eee : ${PROJECT_LOG} not reachable"
   exit 1
fi
#
# check for write permission on PROJECT_LOG
if [ ! -w ${PROJECT_LOG} ]
then
   echo "${command} : eee : ${PROJECT_LOG} not writable"
   exit 1
fi
#
log=${PROJECT_LOG}/$(basename ${0} .sh).log.${log_date}
echo "[Context]" 1>> ${log}
echo "command=$(basename ${0})" 1>>${log}
echo "hostname=${hostname}" 1>> ${log}
echo "runtime=${log_date}" 1>> ${log}
echo "log=${log}" 1>> ${log}
unset log_date
#
echo "" 1>> ${log}
echo "[Parameters]" 1>> ${log}
echo "dirin=${dirin}" 1>> ${log}
echo "good=${goodend}" 1>> ${log}
echo "bad=${badend}" 1>> ${log}
echo "" 1>> ${log}
#
# build list files
varlist="lhf lwr netflux q2m shf sst swr t2m tau taux tauy ws"
period_list="daily monthly"
list_filein=""
for var in $varlist
do
     for period in ${period_list}
     do
         if [ "${period}" == "daily" ]
         then
            suffix=1d
         else
            suffix=1m
         fi
         filein=${dirin}/${period}/${var}_tropflux_${suffix}_${badend}.nc
         if [ ! -f ${filein} ]
         then
             echo " eee : ${filein} not found" 1>> ${log}
             exit 1
         else
             list_filein=${list_filein}" ${filein}"
         fi
     done
     unset period
done
unset period_list
unset var
unset varlist
#
# rename all files
for filein in ${list_filein}
do
    echo "iii : rename ${filein}" >> ${log} 2>&1
    bfileout=$(basename ${filein} ${badend}.nc)"${goodend}.nc"
    fileout=$(dirname ${filein})/${bfileout}
    unset bfileout
    mv ${filein} ${fileout} >> ${log} 2>&1
    status=${?}
    if [ ${status} -ne 0 ]
    then
        echo "eee : pb with mv" >> ${log} 2>&1
        ncdump -h ${filein} >> ${log} 2>&1
        exit 1
    fi
    unset status
    unset fileout
done
unset filein
#
# cleanning
#++
#++ set
# end
exit 0
