source: trunk/insertphp.sh @ 164

Last change on this file since 164 was 146, checked in by pinsard, 12 years ago

xmlstarlet vs xml

  • Property svn:executable set to *
  • Property svn:keywords set to Id
File size: 3.3 KB
RevLine 
[76]1#! /bin/sh
2#+
[80]3#
[109]4# .. program:: insertphp.sh
[103]5#
[95]6# ============
7# insertphp.sh
8# ============
[76]9#
[95]10# -----------------------------------------------
11# ajoute des commandes php dans des fichiers html
12# -----------------------------------------------
[76]13#
[93]14# SYNOPSIS
[76]15# ========
16#
17# ::
18#
19#  $ insertphp.sh -i filein -o fileout -s sedfile -d depth
20#
21# DESCRIPTION
22# ===========
23#
24# ajoute des commandes php dans des fichiers html
25# en fonction d'info dans main.xml
26#
[108]27# .. option:: -i  <input file>
28# .. option:: -o  <output file>
29# .. option:: -s  <sedfile>
30# .. option:: -d  <depth>
[76]31#
32# EXAMPLES
33# ========
34#
[100]35# To do the job on "one" branch::
[76]36#
37#  $ ./insertphp.sh -i template_beforesed.php -o ./template_one.php -d 2 -s insertphp_one.sed
38#
[100]39# To check::
[76]40#
[100]41#  $ diff template_beforesed.php template.php
[76]42#
43#
[100]44# To do the job on "many" branch::
[76]45#
46#  $ ./insertphp.sh -i template_beforesed.php -o ./template_many.php -d 3 -s insertphp_many.sed
47#
48# see makefile
49#
50# EVOLUTIONS
51# ==========
[101]52#
[76]53# $Id$
[101]54#
[146]55# - fplod 20120227
56#
57#   * xmlstarlet vs xml
58#
[76]59# - fplod 2008-09-18T08:55:50Z aedon.locean-ipsl.upmc.fr (Darwin)
60#
[101]61#   * add -d and -d option to perform one and many transformation
[76]62#     with the same shell script
[101]63#
[76]64# - fplod 2008-09-17T15:12:32Z aedon.locean-ipsl.upmc.fr (Darwin)
65#
[101]66#   * creation to encapsulate insertphp_one.sed usage and use
[76]67#     main.xml title
[80]68#
[76]69#-
70system=$(uname)
71case "${system}" in
[100]72   AIX|IRIX64)
73      echo " www : no specific posix checking"
[146]74      xmlcmd=xml
[100]75   ;;
[146]76   Darwin)
77      xmlcmd=xmlstarlet
78   ;;
[100]79   *)
80     set -o posix
[146]81     xmlcmd=xml
[100]82   ;;
[76]83esac
[100]84unset system
85#
[116]86set -u
87#
[76]88command=$(basename ${0})
[93]89log_date=$(date -u +"%Y%m%dT%H%M%SZ")
90log=/tmp/$(basename ${command} .sh).log.${log_date}
[76]91#
92# test if xml is available
[146]93tool=${xmlcmd}
[76]94type ${tool} 1> /dev/null 2>&1
95status=${?}
96if [ ${status} -ne 0 ]
97then
[100]98   echo " eee : ${tool} not found"
99   exit 1
[76]100fi
101unset status
102unset tool
103#
104usage=" Usage : ${command} -i filein -o fileout -s sedfile -d depth"
105#
106minargcount=8
107#echo " narg ${#}"
108if [ ${#} -lt ${minargcount} ]
109then
110   echo "eee : not enought arguments"
111   echo "${usage}"
112   exit 1
113fi
114unset minargcount
115#
[116]116while [ ${#} -gt 0 ]
[76]117do
[100]118   case ${1} in
119      -i)
120         filein=${2}
121         shift
122       ;;
123      -o)
124         fileout=${2}
125         shift
126      ;;
127      -s)
128         sedfile=${2}
129         shift
130      ;;
131      -d)
132         depth=${2}
133         shift
134      ;;
[101]135      *)
136         # other choice
[100]137         echo "eee : unknown option ${1}"
138         echo "${usage}"
139         exit 1
140      ;;
141   esac
[101]142   # next flag
143   shift
[76]144done
145unset usage
146#
147# check for filein
148if [ ! -f ${filein} ]
149then
150   echo "eee : ${filein} not found"
151   exit 1
152fi
153#
154# check for sedfile
155if [ ! -f ${sedfile} ]
156then
157   echo "eee : ${sedfile} not found"
158   exit 1
159fi
160#
161# check for depth
162if [ ${depth} -lt 0 ]
[101]163then
[76]164   echo "eee : ${depth} must be greater than 0"
165   exit 1
166fi
167# ++ check for fileout ++ overwrite
168#
169# check for main.xml  ++ path
170if [ ! -f ./main.xml ]
171then
172   echo "eee : ./main.xml not found"
173   exit 1
174fi
175#
176# get title from main.xml
[146]177title=$(${xmlcmd} sel -N d="http://docbook.org/ns/docbook" -t -m "/d:article/d:title/node()" -c . main.xml)
[76]178#
179dirphp="./"
180idepth=1
181while [ ${idepth} -le ${depth} ]
182do
[100]183   dirphp=${dirphp}"../"
184   idepth=$((${idepth} + 1))
[76]185done
186sed -f ${sedfile} \
187   -e "s@${title}@${title} <?php include(\"${dirphp}php/timestamp.php\"); ?>@" \
188   ${filein} > ${fileout}
189# end
190exit 0
Note: See TracBrowser for help on using the repository browser.