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

usage="$0 [ -pixels NNN ] [ -suffix {SUFFIX} ] FILENAME"

maxpx=5000
maxwx=129
maxwy=129

# Finds or creates an icon-sized version of image FILENAME,
# in the same directory.  Returns the *name* of the icon file.
#
# If FILENAME (minus extension) already ends in 
# one of the standard icon suffixes, or FILENAME is sufficiently small,
# the result is FILENAME itself.  Otherwise the program tries
# to create a PNG image with at most ${maxpx} pixels, in the 
# same directory, whose name is FILENAME with "{SUFFIX}" 
# (default "-icon") appended.

cmd=${0##*/}

function error()
{ 
  echo "${cmd}: $1" >&2; 
  echo "usage: ${usage}" >&2; 
  echo "(unknown)";
  exit 1;
}

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

if [ $# != 1 ]; then
  error "wrong number of parameters";
fi

fpath="$1"; shift;

# get file name and directory

if ! [ -r "${fpath}" ]; then
  error "could not read ${fpath}"
fi

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

getiname=( "${STOLFIHOME}/bin/get-icon-names-for-images" "-suffix" "${suffix}" )

iname="$( echo ${fname} | ${getiname[@]} )"
echo "assuming that the icon is ${iname}"

# If the file itself is an icon, or the icon exists
# and is up-to-date, we are done:
if [ "${iname}" == "${fname}" ] \
|| [ -r "${iname}" -a -r "${fname}" -a "${iname}" -nt "${fname}" ]; then
  echo ${iname}; exit 0;
fi

# Determine the command ${toppm} that converts the image to PPM format

for d in \
    /usr/bin \
    /usr/X11R6/bin \
    ${STOLFIHOME}/pkg/netpbm-1mar1994-1/PUB/${PLATFORM}/bin \
    ${STOLFIHOME}/bin/${PLATFORM} \
    ${STOLFIHOME}/bin/${PLATFORM} \
  ; do
  if [ -x $d/convert ]; then convert="$d/convert"; fi;
  if [ -x $d/pnmfile ]; then pnmfile="$d/pnmfile"; fi;
  if [ -x $d/imq-to-pgm ] ; then imqtopgm="$d/imq-to-pgm"; fi;
done
topng="${STOLFIHOME}/bin/linear-ppm-to-png"

uname=${fname}

toppm=( "cat" "${fname}" )
case ${uname} in
  *.gz) uname=${uname%.gz}; toppm=( "${toppm[@]}" '|' "gunzip" ) ;;
  *.Z)  uname=${uname%.Z};  toppm=( "${toppm[@]}" '|' "uncompress" ) ;;
esac;
echo "uname=${uname}" >& 2

case ${uname} in
  *.p[bgpn]m | *.P[BGPN]M )
    goodfmt=0;
;;
  *.PNG | *.png | \
  *.GIF | *.gif | \
  *.jpg | *.JPG | *.jpeg | *.JPEG )
    goodfmt=1;
    toppm=( "${toppm[@]}" '|' "${convert}" "-" "pnm:-" );
;;
  *.bmp | *.BMP | *.eps | *.EPS | \
  *.fits | *.FITS | \
  *.png | *.PNG | \
  *.tga | *.TGA |  \
  *.svg | *.SVG |  \
  *.tif | *.TIF | *.tiff | *.TIFF | \
  *.xbm | *.xwd )
    goodfmt=0;
    toppm=( "${toppm[@]}" '|' "${convert}" "-" "pnm:-" );
;;
  *.imq)
    goodfmt=0;
    toppm=( "${toppm[@]}" '|' "${imqtopgm}" );
;;
  *)
    echo "(unknown)"; exit 0
;;
esac

echo "conversion command: \"${toppm[@]}\"" >& 2

# If the icon does not exist, its name had better be a PNG file:
# 
case ${iname} in
  *.png | *.PNG ) ;;
  * ) error "cannot create non-PNG icons" ;;
esac

tname="/tmp/$$.ppm"
eval ${toppm[@]} > ${tname}

# If small enough, use self
tmp="$( $pnmfile ${tname} \
  | sed -e '/[0-9] by [0-9]/!d' -e 's/^.* \([0-9]*\) by \([0-9]*\) .*$/\1 \2/' )"
if [ "${tmp}" == "" ]; then
  /bin/rm ${tname}
  error "could not get size of ${fpath}";
fi
tmp=( ${tmp} )
if [ ${goodfmt} == 1 ] \
&& [ ${tmp[0]} -le ${maxwx} ] \
&& [ ${tmp[1]} -le ${maxwy} ] \
&& [ $((${tmp[0]} * ${tmp[1]})) -le ${maxpx} ]; then
  /bin/rm ${tname}
  echo ${fpath}; exit 0;
fi
cat ${tname} | ${topng} -pixels ${maxpx} > ${iname}
/bin/rm ${tname}
echo "${iname}"; exit 0

