| 1 | #!/bin/sh |
| 2 | # Convert the output of 'exifprobe' to a simple HTML file. |
| 3 | # Usage: |
| 4 | # probe2html [exifprobe options] pathname |
| 5 | # (runs exifprobe with options on 'pathname' and writes a file named by |
| 6 | # the last component of 'pathname' + the options (with spaces removed) |
| 7 | # and a ".html" extension. |
| 8 | # OR |
| 9 | # [command] | probetohtml - [command | almost anything] |
| 10 | # [command] should generate exifprobe output (e.g. 'exifgrep'). The '-' |
| 11 | # argument to probe2html forces reading from stdin; the output filename |
| 12 | # is a unique filename generated from the first word following the '-' |
| 13 | # the prefix PROBE, a number making the filename unique, and the extension |
| 14 | # ".html". The page will be titled with *all* of the words following '-' |
| 15 | |
| 16 | # This allows something like |
| 17 | # exifgrep Maker some/file/name | probe2html - exifgrep some/file/name |
| 18 | # which will produce a file PROBEexifgrep.0.html with the output of that |
| 19 | # command and a title which shows the command. |
| 20 | |
| 21 | unique_name() |
| 22 | { |
| 23 | prefix= |
| 24 | case $# in |
| 25 | 0) name="unique.$$" ;; |
| 26 | *) name=$1; prefix=$2; suffix=$3 ;; |
| 27 | esac |
| 28 | |
| 29 | case ${prefix} in |
| 30 | X) prefix= ;; |
| 31 | esac |
| 32 | |
| 33 | case "${suffix}" in |
| 34 | .*|"") ;; |
| 35 | *) suffix=.${suffix} ;; |
| 36 | esac |
| 37 | |
| 38 | if test -d ${prefix}${name}${suffix} -o -d ${prefix}${name}.0${suffix} |
| 39 | then |
| 40 | vers=1 |
| 41 | while test -d ${prefix}${name}.${vers}${suffix} |
| 42 | do |
| 43 | vers=`expr $vers + 1` |
| 44 | done |
| 45 | fi |
| 46 | |
| 47 | if test -f ${prefix}${name}${suffix} -o -f ${prefix}${name}.0${suffix} |
| 48 | then |
| 49 | vers=${vers:=1} |
| 50 | while test -f ${prefix}${name}.${vers}${suffix} -o -f ${prefix}${name}.${vers}${suffix}.gz -o -d ${prefix}${name}.${vers}${suffix} |
| 51 | do |
| 52 | vers=`expr $vers + 1` |
| 53 | done |
| 54 | fi |
| 55 | |
| 56 | case "${vers}" in |
| 57 | "") echo ${prefix}${name}.0${suffix} ;; |
| 58 | *) echo ${prefix}${name}.${vers}${suffix} ;; |
| 59 | esac |
| 60 | } |
| 61 | |
| 62 | # Get the last component of the argument pathname |
| 63 | lastcomp() |
| 64 | { |
| 65 | local oifs |
| 66 | #local IFS |
| 67 | |
| 68 | oifs="${IFS}" |
| 69 | IFS=/ |
| 70 | set -- ${1} |
| 71 | IFS="${oifs}" |
| 72 | |
| 73 | case "${1}" in |
| 74 | "") case $# in |
| 75 | 0) ;; |
| 76 | *) shift ;; |
| 77 | esac |
| 78 | ;; |
| 79 | esac |
| 80 | while test $# -gt 1 |
| 81 | do |
| 82 | case "${2}" in |
| 83 | "") break ;; |
| 84 | esac |
| 85 | shift |
| 86 | done |
| 87 | |
| 88 | echo ${1} |
| 89 | } |
| 90 | |
| 91 | # Get the first component of the argument pathname |
| 92 | firstcomp() |
| 93 | { |
| 94 | local oifs |
| 95 | |
| 96 | oifs="${IFS}" |
| 97 | IFS=/ |
| 98 | set -- ${1} |
| 99 | IFS="${oifs}" |
| 100 | |
| 101 | basename $1 .d |
| 102 | } |
| 103 | |
| 104 | # make a temp file for the sed script... |
| 105 | tmp=$(mktemp /tmp/PROBE.XXXXXX) |
| 106 | |
| 107 | # and make the script |
| 108 | cat << 'END' > ${tmp} |
| 109 | s/</\</g |
| 110 | s/>/\>/g |
| 111 | s/\[0m/<\/FONT>/g |
| 112 | s/\[30m/<FONT color="BLACK">/g |
| 113 | s/\[31m/<FONT color="RED">/g |
| 114 | s/\[32m/<FONT color="GREEN">/g |
| 115 | s/\[35m/<FONT color="MAGENTA">/g |
| 116 | s/\[94m/<FONT color="BLUE">/g |
| 117 | s/\[90m/<FONT color="GRAY">/g |
| 118 | END |
| 119 | |
| 120 | probeopts= |
| 121 | suffix= |
| 122 | lastcomp= |
| 123 | |
| 124 | while test $# -gt 1 |
| 125 | do |
| 126 | case "$1" in |
| 127 | -) infile=stdin |
| 128 | basename=$2 |
| 129 | destfile=$( unique_name ${basename} PROBE .html ) |
| 130 | shift |
| 131 | title="$*" |
| 132 | suffix= |
| 133 | set -- |
| 134 | break |
| 135 | ;; |
| 136 | -*) probeopts="${probeopts} $1" |
| 137 | suffix="${suffix}$1" |
| 138 | shift |
| 139 | ;; |
| 140 | *) break ;; |
| 141 | esac |
| 142 | done |
| 143 | |
| 144 | |
| 145 | case ${infile} in |
| 146 | stdin) ;; |
| 147 | *) case $# in |
| 148 | 0) echo "$0: no input file" 1>&2; exit 1 ;; |
| 149 | esac |
| 150 | infile=$1 |
| 151 | destfile=$( lastcomp ${infile} ) |
| 152 | title=$( firstcomp ${infile} ) |
| 153 | title="${title} - $destfile ${probeopts}" |
| 154 | destfile="${destfile}${suffix}.html" |
| 155 | ;; |
| 156 | esac |
| 157 | |
| 158 | echo input file is ${infile} |
| 159 | |
| 160 | # Start the page |
| 161 | cat << HEAD > ${destfile} |
| 162 | <HTML> |
| 163 | <HEAD> |
| 164 | <TITLE> |
| 165 | ${title} |
| 166 | </TITLE> |
| 167 | </HEAD> |
| 168 | <BODY> |
| 169 | <PRE> |
| 170 | HEAD |
| 171 | |
| 172 | case "${infile}" in |
| 173 | stdin) sed -f ${tmp} >> ${destfile} |
| 174 | ;; |
| 175 | *) ${PROBE} ${probeopts} ${infile} | sed -f ${tmp} >> ${destfile} ;; |
| 176 | esac |
| 177 | |
| 178 | # end the file |
| 179 | cat << TAIL >> ${destfile} |
| 180 | </PRE> |
| 181 | </BODY> |
| 182 | TAIL |
| 183 | |
| 184 | # Done |
| 185 | rm -f ${tmp} |
| 186 | echo output file is ${destfile} |
| 187 | echo title is ${title} |