#! /bin/csh -f 

set usage = "$0 [ -show ] DIRECTORY SIZE..." 

# Converts a pair of PPM images DIRECTORY/p-0.ppm, DIRECTORY/p-1.ppm
# to color GIF format, gamma-encoded, scaled to the specified sizes.
#
# Each size must be of the form WxH, where W and H are integers:
# e.g. 140x392, 025x035, etc..
#
# This script expects to find in the named DIRECTORY:
#
#   DIRECTORY/p-0.ppm          the starting PPM file, black background.
#   DIRECTORY/p-1.ppm          the starting PPM file, white background.
#   DIRECTORY/p.comments       short description of image.
#
# The ".ppm" files may be compressed with "compress" or "gzip".
#   
# For each given SIZE, the script will also (re)create, if necessary, the files
#
#   DIRECTORY/p-SIZE.gif       scaled color version of image.
# 
#   DIRECTORY/p.gif            a symbolic link to "p-LASTSZ.gif"
#   DIRECTORY/p-icon.gif       a symbolic link to "p-FIRSTSZ.gif"
#
#   DIRECTORY/p.html-inc       an html fragment, that inlines
#                              p-icon.gif and links to p.gif,
#                              for use by make-image-index
#   
# where FIRSTSZ and LASTSZ are the first and last of the given SIZEs.
#
# The "-show" switch displays the input and output files, using "xv".  

# Get program paths:

set path = ( /usr/local/bin /usr/bin /usr/ucb )
set tooldir = "$0"; set tooldir = "${tooldir:h}"
set pbmbin = "/n/lac/pkg/netpbm-1mar1994-1/PUB/${PLATFORM}/bin"
set gnubin = "/n/gnu/bin"

set mktgif  = "/home/staff/stolfi/PUB/bin/linear-ppm-pair-to-gif"
set mkhtml  = "/home/lac/stolfi/PUB/bin/make-image-index-entry"
set chkdep  = "/home/lac/stolfi/PUB/bin/check-dependencies"
set xv      = "/n/image/bin/xv"

# Option parsing:

unset show

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

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

set dir = "$1"; shift;
set sizes = ( `echo $* | sed -e 's/x/./g'` )
set fullsize = "${sizes[$#sizes]}"
set iconsize = "${sizes[1]}"

# Check (and uncompress if necessary) the input PPM files

set baseppms = ( "${dir}/p-0.ppm" "${dir}/p-1.ppm" )
foreach f ( ${baseppms} )
  if ( -r ${f} ) then
    # OK
  else if ( -r ${f}.Z ) then
    /bin/uncompress ${f}.Z
  else if ( -r ${f}.gz ) then
    ${gnubin}/gunzip ${f}.gz
  else 
    echo "$0"': '" ${f} not found" ; exit 1
  endif
end

set comments = "${dir}/p.comments"
if ( ! ( -r ${comments} ) ) then
  echo "$0"': '" ${comments} not found" ; exit 1
endif

# Generate scaled versions:

foreach sz ( ${sizes} )
  set nx = ${sz:r}; set ny = ${sz:e}
  
  set cgif = "${dir}/p-${nx}x${ny}.gif"
  if ( "`${chkdep} ${cgif} ${baseppms} ${mktgif}`" == 1 ) then
    echo "=== generating transparent ${cgif} from ${baseppms} ==="
    if ( "$sz" == "${fullsize}" ) then
      if ( -r ${cgif}~ ) /bin/mv ${cgif}~ ${cgif}~~
      if ( -r ${cgif} ) /bin/mv ${cgif} ${cgif}~
    endif
    nice ${mktgif} \
        -xsize ${nx} -ysize ${ny} \
        -gamma 1.9 \
      ${baseppms} \
      > ${cgif}
  endif
end

/bin/rm -f ${dir}/p.gif ${dir}/p-icon.gif
/bin/ln -s p-${fullsize:r}x${fullsize:e}.gif ${dir}/p.gif
/bin/ln -s p-${iconsize:r}x${iconsize:e}.gif ${dir}/p-icon.gif
${mkhtml} ${dir}/p.gif > ${dir}/p.html-inc

if ( $?show ) then
  ( set nonomatch ; cd $dir && ${xv} p.gif* p-*.ppm* & )
endif