#! /bin/tcsh -f
# Last edited on 2003-07-20 16:02:09 by stolfi

set cmd = "$0"; 
set usage = "${cmd} DIRECTORY"

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

set dir = "$1"; shift;
if ( ! ( -d ${dir} ) ) then
  echo "${dir} is not a directory"; exit 1
endif

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

find ${dir}/. \
    \( -name 'DSC[0-9][0-9][0-9][0-9][0-9].JPG' -o \
       -name 'DSC[0-9][0-9][0-9][0-9][0-9].TIF' \
    \) \
    -print \
  > .images

foreach img ( `cat .images` )
  
  # Small "browse" version
  set smimg = "${img:r}-s.JPG"
  echo -n ${smimg}' ...'
  if ( ! ( -r ${smimg} ) ) then
    convert ${img} -quality 95 -geometry '512' ${smimg}
    echo "done."
  else 
    echo "found."
  endif

  # Enhanced "browse" version
  set enimg = "${img:r}-e.JPG"
  echo -n ${enimg}' ...'
  if ( ! ( -r ${enimg} ) ) then
    convert ${img} -geometry '512' PPM:- \
      | ppmnorm -bpercent 1 -wpercent 0.1 \
      | convert PPM:- -quality 95 ${enimg}
    echo "done."
  else 
    echo "found."
  endif

  # Tiny "iconic" version
  set icimg = "${img:r}-i.JPG"
  echo -n ${icimg}' ...'
  if ( ! ( -r ${icimg} ) ) then
    convert ${enimg} -quality 99 -geometry '64' ${icimg}
    echo "done."
  else 
    echo "found."
  endif

end

find ${dir}/. \
    \( -name 'DSC[0-9][0-9][0-9][0-9][0-9].MPG' \
    \) \
    -print \
  > .movies

if ( ! ( -z .movies ) ) then
  echo "Cannot produce small images for these files:"
  cat .movies
endif
