#! /bin/bash -fu
# Last edited on 2009-04-08 03:40:10 by stolfilocal

usage="$0 [ -noicon | -isicon ] [ -suffix {SUFFIX} ] {IMAGENAME}.{EXT}"

# Writes to stdout an HTML fragment that displays a small version of
# the image file {IMAGENAME}.{EXT}, with a link to the full-size image.

# If "-isicon" or "-noicon" is specified, the program will try to use
# the {IMAGENAME}.{EXT} itself as its icon. If "-isicon", the image is
# not scaled; if "-noicon", it is scaled to width = 50 piexls. If
# neither option is given, this script will try to find an icon for
# {IMAGENAME}.{EXT}, using "get-icon-names-for-images -suffix {SUFFIX}".
# The {SUFFIX} defaults to "-icon".
#
# In all three cases, if the file to be used as icon is
# not readable or is out-of-date, the program prints a warning
# and uses a default sign as the icon.
#
# The output will include the file "{IMAGENAME}.comments" if it exists.

cmd=${0##*/}

function error()
{ 
  echo "${cmd}: $1" >&2; 
  echo "usage: ${usage}" >&2; 
  echo "<p><b>???</b></p>";
  exit 1;
}

isicon=0;
noicon=0;
suffix="-icon"
while [ $# -gt 0 ]; do
  case "$1" in
    -isicon ) 
      isicon=1; noicon=0; shift ;;
    -noicon ) 
      noicon=1; isicon=0; shift ;;
    -suffix ) 
      if [ $# -lt 2 ]; then error "missing suffix"; fi
      suffix="$2"; shift; shift ;;
    -* )
      error "unrecognized option $1" ;;
    * ) break;;
  esac;
done

if [ $# -ne 1 ]; then
  error "wrong number of parameters"; 
fi

fpath="$1"; shift;
# echo '${fpath} = '"${fpath}" >&2

# get file name and directory

if ! [ -r "${fpath}" ]; then
  error "can't read ${fpath}"
fi

fname="${fpath##*/}"

if [ "${fname}" != "${fpath}" ]; then
  dir="${fpath%/*}"
  cd ${dir} || error "can't connect to ${dir}"
  dir="${dir}/";
else
  dir="";
fi

if [ $isicon = 1 ]; then
  iname="${fname}"; iconwd="";
elif [ $noicon = 1 ]; then
  iname="${fname}"; iconwd="width=50";
else
  getiname=("${STOLFIHOME}/bin/get-icon-names-for-images" "-suffix" "${suffix}" )
  iname="$(echo ${fname} | ${getiname[@]})"; iconwd="";
fi

# If the file is not an icon, and the icon does not exists
# or is obsolete, complain and use the image itself:
#  
if [ "${iname}" = "${fname}" ] ; then
  echo "using ${dir}${iname} as its own icon" >&2 ;
  iconref="<img src=\"${dir}${iname}\" align=top ${iconwd} border=0>"
elif [ -r "${iname}" -a -r "${fname}" ]; then
  echo "using ${dir}${iname} as icon for ${dir}${fname}" >&2 ;
  iconref="<img src=\"${dir}${iname}\" align=top ${iconwd} border=0>"
else
  echo "** icon ${dir}${iname} for ${dir}${fname} missing or out-of-date" >&2;
  iconref="<b><tt>???</tt></b>"
fi
iconref="<a href=\"${dir}${fname}\">${iconref}</a>";

cmts="${fname%.*}.comments"
if [ ! -r "${cmts}" ]; then 
  cmts="/dev/null"
fi 

size="`cat ${fname} | wc -c | tr -d ' '`"
cat <<EOF
<table>
<tr>
  <td rowspan=2 valign=top> ${iconref} </td>
  <td valign=top> <tt><a href="${dir}${fname}">${dir}${fname}</a> [${size} bytes]</tt></td>
</tr>
<tr><td valign=top><small><pre>
`cat ${cmts} | sed -e 's:[{]:<b>:g' -e 's:[}]:</b>:g'`
</pre></small></td></tr>
</table>
EOF
