#! /bin/csh -fe

set usage = "$0 IMAGEFILE"

# Writes to stdout an HTML fragment that displays 
# a small version of the specified image, with 
# a link to the full-size image.

# Let's suppose "IMAGEFILE" has the form "NAME.EXT".  This script will
# find or produce an icon for the image, using "make-icon-for-image".
# If that fails, it will will use "NAME.EXT"
# itself with a "width=50" specifier.  
#
# The output will include the
# file "NAME.comments" if it exists.

if ( $#argv != 1 ) then
  echo "${usage}"; exit 1
endif

set mkicon = "${STOLFIHOME}/bin/make-icon-for-image"

set img = "$1"
set dir = "${img:h}"
if ( "x${dir}" == "x${img}" ) set dir = "."

set icon = "`${mkicon} -pixels 5000 ${img}`"
set iconwd = ""
if ( ( "${icon}" == "(text)" ) \
  || ( "${icon}" == "(unknown)" ) \
  || ( ! ( -r "${dir}/${icon}" ) ) ) then
  set icon = "${img}"
  set iconwd = "width=50"
endif

set cmts = "${img:r}.comments"
if ( ! ( -r "${cmts}" ) ) then 
  set cmts = "/dev/null"
endif

set size = ( `wc -c ${img}` )
cat <<EOF
<table>
<tr>
  <td rowspan=2 valign=top> <img src="${icon}" align=top ${iconwd} border=0> </td>
  <td valign=top> <tt><a href="${img}">${img}</a> [${size[1]} bytes]</tt></td>
</tr>
<tr><td valign=top><small><pre>
`cat ${cmts}`
</pre></small></td></tr>
</table>
EOF
