#! /bin/csh -f

# Creates an HTML index page for all the images under the current directory.

set path = ( /usr/ucb /usr/bsd /usr/bin /bin )

set cmd = "$0"
set usage = "${cmd:t} [ -force ] [ -alpha ] [ -title STRING ] [ NAME ]" 

# Writes an HTML page called "index.html" displaying
# all image files under the current directory.
# Omits image files that are icons of other images in the same directory.

# The index.html file will include only 25 images or less.
# If there are more than 25 images, additional pages
# called "index-ab.html", "index-ac.html", etc.
# will be created for them.

set mixops = ( )
set alphasrt = 0
set title = "Images in `pwd`"
while ( ( $#argv > 0 ) && ( "x$1" =~ x-* ) ) 
  if ( "x$1" == "x-force" ) then
    set mixops = ( ${mixops} "-force" ); shift
  else if ( ( $#argv >= 2 ) && ( "x$1" == "x-title" ) ) then
    set title = "$2"; shift; shift
  else if ( "x$1" == "x-alpha" ) then
    set alphasrt = 1; shift
  else 
    echo "usage: ${usage}"; exit 1
  endif
end

if ( $#argv >= 1 ) then
  set pgname = "$1"; shift
  set pgname = "${pgname:r}"
else
  set pgname = "index"
endif

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

set tmp = "/tmp/$$"
set imlist = ${tmp}-list
set iconlist = ${tmp}-icons
set biglist = ${tmp}-big
set ixlist = ${tmp}-full
set srtlist = ${tmp}-srt

set bool = "${STOLFIHOME}/bin/${PLATFORM}/bool"
set geticons = "${STOLFIHOME}/bin/get-icon-names-for-images"
set mkindex =  "${STOLFIHOME}/bin/make-image-index"

# Find all images
find ./ -type f -print \
  | egrep \
      -i -e '\.(png|p[bgpn]m|gif|jpg|jpeg|bmp|eps|fits|png|tga|tif|tiff|xbm|xwd|imq)(\.Z|\.gz|)$' \
  | sort \
  > ${imlist}
  
# Get those that aren't icon-like:
cat ${imlist} \
  | egrep -v -e '[-_](thumb|icon|i)[.](png|PNG|gif|GIF|jpg|JPG|jpeg|JPEG)' \
  | sort | uniq \
  > ${biglist}

# Find their icons:
cat ${biglist} \
  | ${geticons} \
  | sort | uniq \
  > ${iconlist}
  
# Get all the images except icons of other images
${bool} 1-2 ${imlist} ${iconlist} > ${ixlist}

if ( $alphasrt ) then
  cat ${ixlist} | sort > ${srtlist}
else
  ls -dt `cat ${ixlist}` > ${srtlist}
endif
  
cat ${srtlist} \
  | ${mkindex} ${mixops} -title "${title}" ${pgname}

# /bin/rm -f ${imlist} ${iconlist}
