#! /bin/csh -f
# Last edited on 2003-09-13 20:40:14 by stolfi

set usage = "$0 [ -normalize ] IMGFILE..."

set norm = ( /bin/cat )

while ( ( $#argv > 0 ) && ( "x$1" =~ x-* ) ) 
  if ( "x$1" = "x-normalize ) then
    set norm = ( ppmnorm ); shift
  else 
    echo "usage: ${usage}"; exit 1
  endif
end

set files = ( $* )

foreach img ( $files )
  echo '=== '${img}' ==='
  if ( "/${img:e}" == "/gif" ) then
    set convert = ( giftopnm ${img} )
  else if ( "/${img:e}" =~ /ppm  ) then
    set convert = ( cat ${img} )
  else if ( "/${img:e}" =~ /png  ) then
    set convert = ( convert ${img} PPM:- )
  else
    echo "unrecognized extension: ${img}"; exit 1
  endif
  set thumb = ${img:r}-thumb.gif
  if ( -r ${thumb} ) /bin/mv ${thumb} ${thumb}~
  ${convert} \
    | pnmgamma 0.555555 \
    | ${norm} \
    | pnmscale -ysize 60 \
    | linear-ppm-to-png \
    > ${thumb}
end
