[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 | #- |
---|
| 70 | system=$(uname) |
---|
| 71 | case "${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] | 83 | esac |
---|
[100] | 84 | unset system |
---|
| 85 | # |
---|
[116] | 86 | set -u |
---|
| 87 | # |
---|
[76] | 88 | command=$(basename ${0}) |
---|
[93] | 89 | log_date=$(date -u +"%Y%m%dT%H%M%SZ") |
---|
| 90 | log=/tmp/$(basename ${command} .sh).log.${log_date} |
---|
[76] | 91 | # |
---|
| 92 | # test if xml is available |
---|
[146] | 93 | tool=${xmlcmd} |
---|
[76] | 94 | type ${tool} 1> /dev/null 2>&1 |
---|
| 95 | status=${?} |
---|
| 96 | if [ ${status} -ne 0 ] |
---|
| 97 | then |
---|
[100] | 98 | echo " eee : ${tool} not found" |
---|
| 99 | exit 1 |
---|
[76] | 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] | 116 | while [ ${#} -gt 0 ] |
---|
[76] | 117 | do |
---|
[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] | 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 ] |
---|
[101] | 163 | then |
---|
[76] | 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 |
---|
[146] | 177 | title=$(${xmlcmd} sel -N d="http://docbook.org/ns/docbook" -t -m "/d:article/d:title/node()" -c . main.xml) |
---|
[76] | 178 | # |
---|
| 179 | dirphp="./" |
---|
| 180 | idepth=1 |
---|
| 181 | while [ ${idepth} -le ${depth} ] |
---|
| 182 | do |
---|
[100] | 183 | dirphp=${dirphp}"../" |
---|
| 184 | idepth=$((${idepth} + 1)) |
---|
[76] | 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 |
---|