#! /bin/csh -f 

set usage = "$0 [ -test ] [ -show ] [ -gray ] DIRECTORY" 

# Converts a gallery portrait from PPM to GIF format, color and grayscale,
# gamma-encoded, scaled to all standard portrait sizes (025x035, 035x049,
# 050x070, ..., 280x392).
#
# The PPM file should have 5x7 aspect ratio, linear intensity scale,
# and preferably resolution 280x392 or higher.
#
# This script expects to find in the named DIRECTORY:
#
#   DIRECTORY/p.ppm            the starting PPM file.
#   DIRECTORY/p.comments       short description of image.
#
# The following files may be present in place of (or in addition to)
# the file DIRECTORY/p.ppm:
#
#   DIRECTORY/p-raw.ppm        original scanned image, before color correction .
#   DIRECTORY/p.parms          the color correction parameters.
#
# The script will automatically (re)create p.ppm from p-raw.ppm
# and p.parms, if the latter are present, and p.ppm is either missing or
# obsolete.
#
# The ".ppm" files may be compressed with "compress" or "gzip".
#   
# For each standard SIZE, the script will also (re)create, 
# if necessary, the files
#
#   DIRECTORY/p-SIZE.gif       scaled color version of image.
#   
#   DIRECTORY/p-SIZE-gray.gif  scaled grayscale version of image.
# 
# The grayscale versions will be created only if the 
# "-gray" switch is given. The script creates also:
# 
#   DIRECTORY/p.gif            a symbolic link to "p-280x392.gif"
#   DIRECTORY/p-icon.gif       a symbolic link to "p-050x070.gif"
#
#   DIRECTORY/p.html-inc       an html fragment, that inlines
#                              p-icon.gif and links to p.gif,
#                              for use by make-image-index
#
# The "-test" switch asks to create only one test version (140x196)
# In that case, "p.gif" and "p-icon.gif" will be links to that same file.
#
# The "-show" switch displays the input and output files, using "xv".  

# Program paths:

set toolbin = "$0"; set toolbin = "${toolbin:h}"
set path = ( /usr/local/bin /usr/bin /usr/ucb )

set mkgifs = ${toolbin}/make-scaled-gif-images

# Option parsing:

unset testing
set mkgifopts = ( ) 

while ( ( $#argv > 0 ) && ( "x$1" =~ x-* ) )
  if ( ( $#argv >= 1 ) && ( "x$1" == "x-test" ) ) then
    set testing 
    shift
  else if ( ( $#argv >= 1 ) && ( "x$1" == "x-show" ) ) then
    set mkgifopts = ( ${mkgifopts} "-show" )
    shift
  else if ( ( $#argv >= 1 ) && ( "x$1" == "x-gray" ) ) then
    set mkgifopts = ( ${mkgifopts} "-gray" )
    shift
  else
    echo "usage: ${usage}" ; exit 1
  endif
end

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

set dir = "$1"; shift;

# Define image sizes:
# Note: icon size must come first, full size last:

if ( $?testing ) then
  set sizes = ( "140x196" )
else
  set sizes = ( \
    "050x070" \
    "025x035" \
    "035x049" \
    "070x098" \
    "100x140" \
    "140x196" \
    "200x280" \
    "280x392" \
  )
endif

${mkgifs} ${mkgifopts} ${dir} ${sizes}