source: trunk/insertphp.sh @ 102

Last change on this file since 102 was 102, checked in by pinsard, 14 years ago

improve shell scripts robustness

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