#! /bin/sh
#+
#
# ============
# insertphp.sh
# ============
#
# -----------------------------------------------
# ajoute des commandes php dans des fichiers html
# -----------------------------------------------
#
# SYNOPSIS
# ========
#
# ::
#
#  $ insertphp.sh -i filein -o fileout -s sedfile -d depth
#
#
# DESCRIPTION
# ===========
#
# ajoute des commandes php dans des fichiers html
# en fonction d'info dans main.xml
#
# -i  input file
# -o  output file
# -s  sedfile
# -d  depth
#
# EXAMPLES
# ========
#
# To do the job on "one" branch::
#
#  $ ./insertphp.sh -i template_beforesed.php -o ./template_one.php -d 2 -s insertphp_one.sed
#
# To check::
#
#  $ diff template_beforesed.php template.php
#
#
# To do the job on "many" branch::
#
#  $ ./insertphp.sh -i template_beforesed.php -o ./template_many.php -d 3 -s insertphp_many.sed
#
# see makefile
#
# EVOLUTIONS
# ==========
# 
# $Id$
# 
# - fplod 2008-09-18T08:55:50Z aedon.locean-ipsl.upmc.fr (Darwin)
#
#   * add -d and -d option to perform one and many transformation 
#     with the same shell script
#     
# - fplod 2008-09-17T15:12:32Z aedon.locean-ipsl.upmc.fr (Darwin)
#
#   * creation to encapsulate insertphp_one.sed usage and use 
#     main.xml title
#
#-
system=$(uname)
case "${system}" in
   AIX|IRIX64)
      echo " www : no specific posix checking"
   ;;
   *)
     set -o posix
   ;;
esac
unset system
#
command=$(basename ${0})
log_date=$(date -u +"%Y%m%dT%H%M%SZ")
log=/tmp/$(basename ${command} .sh).log.${log_date}
#
# test if xml is available
tool=xml
type ${tool} 1> /dev/null 2>&1
status=${?}
if [ ${status} -ne 0 ]
then
   echo " eee : ${tool} not found"
   exit 1
fi
unset status
unset tool
#
usage=" Usage : ${command} -i filein -o fileout -s sedfile -d depth"
#
minargcount=8
#echo " narg ${#}"
if [ ${#} -lt ${minargcount} ]
then
   echo "eee : not enought arguments"
   echo "${usage}"
   exit 1
fi
unset minargcount
#
while [ ! -z "${1}" ]
do
   case ${1} in
      -i)
         filein=${2}
         shift
       ;;
      -o)
         fileout=${2}
         shift
      ;;
      -s)
         sedfile=${2}
         shift
      ;;
      -d)
         depth=${2}
         shift
      ;;
      *) # other choice
         echo "eee : unknown option ${1}"
         echo "${usage}"
         exit 1
      ;;
   esac
   shift # next flag
done
unset usage
#
set -u
#
# check for filein
if [ ! -f ${filein} ]
then
   echo "eee : ${filein} not found"
   exit 1
fi
#
# check for sedfile
if [ ! -f ${sedfile} ]
then
   echo "eee : ${sedfile} not found"
   exit 1
fi
#
# check for depth
if [ ${depth} -lt 0 ]
then 
   echo "eee : ${depth} must be greater than 0"
   exit 1
fi
# ++ check for fileout ++ overwrite
#
# check for main.xml  ++ path
if [ ! -f ./main.xml ]
then
   echo "eee : ./main.xml not found"
   exit 1
fi
#
# get title from main.xml
title=$(xml sel -N d="http://docbook.org/ns/docbook" -t -m "/d:article/d:title/node()" -c . main.xml)
#
dirphp="./"
idepth=1
while [ ${idepth} -le ${depth} ]
do
   dirphp=${dirphp}"../"
   idepth=$((${idepth} + 1))
done
sed -f ${sedfile} \
   -e "s@${title}@${title} <?php include(\"${dirphp}php/timestamp.php\"); ?>@" \
   ${filein} > ${fileout}
# end
exit 0
