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