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
21unique_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
63lastcomp()
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
92firstcomp()
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...
105tmp=$(mktemp /tmp/PROBE.XXXXXX)
106
107# and make the script
108cat << 'END' > ${tmp}
109s/</\&lt;/g
110s/>/\&gt;/g
111s/\[0m/<\/FONT>/g
112s/\[30m/<FONT color="BLACK">/g
113s/\[31m/<FONT color="RED">/g
114s/\[32m/<FONT color="GREEN">/g
115s/\[35m/<FONT color="MAGENTA">/g
116s/\[94m/<FONT color="BLUE">/g
117s/\[90m/<FONT color="GRAY">/g
118END
119
120probeopts=
121suffix=
122lastcomp=
123
124while test $# -gt 1
125do
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
142done
143
144
145case ${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        ;;
156esac
157
158echo input file is ${infile}
159
160# Start the page
161cat << HEAD > ${destfile}
162<HTML>
163<HEAD>
164<TITLE>
165${title}
166</TITLE>
167</HEAD>
168<BODY>
169<PRE>
170HEAD
171
172case "${infile}" in
173 stdin) sed -f ${tmp} >> ${destfile}
174        ;;
175     *) ${PROBE} ${probeopts} ${infile} | sed -f ${tmp} >> ${destfile} ;;
176esac
177
178# end the file
179cat << TAIL >> ${destfile}
180</PRE>
181</BODY>
182TAIL
183
184# Done
185rm -f ${tmp}
186echo output file is ${destfile}
187echo title is ${title}
188