#! /bin/csh -f
# Last edited on 2009-04-08 03:58:49 by stolfilocal

# Creates an HTML index page for a bunch of images.

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

set cmd = "$0"
set usage = "${cmd:t} [ -force ] [ -maximg {MAXIMG} ] [ -suffix {ICONSUFFIX} ] [ -noicon | -isicon ] [ -title {STRING} ] [ -name {NAME} ] < {IMAGELIST}"

# Writes an HTML page called "{NAME}.html" displaying all image files
# listed in the standard input. The {NAME} defaults to "index".

# For each image "foo.XXX", if there is a file called "foo.html-inc"
# in the same directory, that is newer than "foo.XXX". "foo{ICONSUFFIX}.XXX",
# and "foo.comments", then that file is merely copied into the index
# page. Otherwise, this script calls "make-image-index-entry" to build
# the "foo.html-inc" file.  The {ICONSUFFIX} defaults to "-icon".
#
# The "-force" option causes the "foo.html-inc" file to be recreated
# in any case, while the "-reuse" option reuses the existing
# "foo.html-inc" even if it is out-of-date. The options "-suffix", "-noicon" or "-isicon"
# if given, are passed to "make-image-index-entry".

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

set force = 0
set reuse = 0
set mkopts = ( ) 
set noicon = 0;
set isicon = 0;
set maximg = 50
set title = "Image index"
set pgname = "index"
set suffix = "-icon"
while ( ( $#argv > 0 ) && ( "x$1" =~ x-* ) ) 
  if ( "/$1" == "/-force" ) then
    set force = 1; set reuse = 0; shift
  else if ( "/$1" == "/-reuse" ) then
    set reuse = 1; set force = 0; shift
  else if ( "/$1" == "/-noicon" ) then
    set noicon = 1; set mkopts = ( ${mkopts} "$1" ); shift
  else if ( "/$1" == "/-isicon" ) then
    set isicon = 1; set mkopts = ( ${mkopts} "$1" ); shift
  else if ( ( $#argv >= 2 ) &&  ( "/$1" == "/-suffix" ) ) then
    set suffix = "$2"; shift; shift
  else if ( ( $#argv >= 2 ) &&  ( "/$1" == "/-maximg" ) ) then
    set maximg = "$2"; shift; shift
  else if ( ( $#argv >= 2 ) && ( "/$1" == "/-title" ) ) then
    set title = "$2"; shift; shift
  else if ( ( $#argv >= 2 ) && ( "/$1" == "/-name" ) ) then
    set pgname = "$2"; shift; shift
    set pgname = "${pgname:r}"
  else 
    echo "bad option $1";
    echo "usage: ${usage}"; exit 1
  endif
end

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

set mkopts = ( ${mkopts} "-suffix" "${suffix}" );

set tmp = "/tmp/$$"
set imlist = ${tmp}-list
set pglist = ${tmp}-pglist

tr ' \011' '\012\012' | egrep -v '^$' > ${imlist}
echo "${cmd:t}: `cat ${imlist} | wc -l` images found" 

if ( -z ${imlist} ) then
  /bin/rm ${imlist}
  exit 1
endif

# This is a cp -p instead of mv, in case it is a symbolic link
if ( -r ${pgname}.html ) then
  /bin/cp -p ${pgname}.html ${pgname}.html~
  /bin/rm ${pgname}.html
endif

# Create the index pages proper:

split -l ${maximg} ${imlist} "${pglist}."
set pages = ( ${pglist}.* )
set nexts = ( $pages ); shift nexts;
set firstpg = "${pages[1]}"
unset pvp 

foreach pag ( ${pages} )
  set HH = "${pgname}-${pag:e}.html"
  if ( -r ${HH} ) /bin/mv ${HH} ${HH}~
  if ( $#nexts > 0 ) then
    set nxp = "$nexts[1]"; shift nexts
  else
    unset nxp
  endif

  cat <<EOF > $HH
<!doctype html public "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <title>${title}
  </title>
</head>
<body>
<h1>${title}</h1>
EOF

  if ( ( "${pag}" == "${firstpg}" ) && ( $#pages > 1 ) ) then
    if ( -r README ) then
      echo '<hr>' >> ${HH}
      echo '<pre>' >> ${HH}
      cat README >> ${HH}
      echo '</pre>' >> ${HH}
      echo '<hr>' >> ${HH}
    endif
  endif

  if ( $?pvp || $?nxp ) then
    echo ' ' >> $HH
    echo '<hr>' >> $HH
    echo '<p>' >> $HH
    if ( $?pvp ) then
      echo '[<a href="'"${pgname}"'-'"${pvp:e}"'.html">Prev</a>]' >> $HH
    endif
    if ( $?nxp ) then
      echo '[<a href="'"${pgname}"'-'"${nxp:e}"'.html">Next</a>]' >> $HH
    endif
    echo '</p>' >> $HH
  endif

  echo '<hr>' >> $HH
  foreach img ( `cat $pag` )
    if ( -r "${img}" ) then
      set entry = "${img:r}.html-inc"
      if ( ( $reuse == 1 ) && ( -r ${entry} ) ) then
        set remake = 0
      else if ( ( $force == 1 ) || ( ! ( -r ${entry} ) ) ) then
        set remake = 1
      else
        if ( -r ${img:r}.comments ) then
          set sources = ( ${img:r}.comments )
        else
          echo "warning: ${img:r}.comments missing or unreadable"
          set sources = ( )
        endif
        if ( ${isicon} ) then
          set icon = "${img}"
          set sources = ( ${sources} ${img} )
        else if ( ${noicon} ) then
          set icon = "NONE"
        else
          set icon = "`echo "${img}" | ${STOLFIHOME}/bin/get-icon-names-for-images -suffix ${suffix}`"
          set sources = ( ${sources} ${icon} )
        endif
        set remake = `${STOLFIHOME}/bin/check-dependencies ${entry} ${sources}`
      endif
      if ( ${remake} == 1 ) then 
        ${STOLFIHOME}/bin/make-image-index-entry ${mkopts} ${img} > ${entry}
      endif
      echo ' ' >> $HH
      cat ${entry} >> ${HH}
      echo '<br>' >> $HH
    else
      echo "unreadable image: ${img}"; exit 1
    endif
  end

  if ( $?pvp || $?nxp ) then
    echo ' ' >> $HH
    echo '<hr>' >> $HH
    echo '<p>' >> $HH
    if ( $?pvp ) then
      echo '[<a href="'"${pgname}"'-'"${pvp:e}"'.html">Prev</a>]' >> $HH
    endif
    if ( $?nxp ) then
      echo '[<a href="'"${pgname}"'-'"${nxp:e}"'.html">Next</a>]' >> $HH
    endif
    echo '</p>' >> $HH
  endif
  set pvp = "${pag}"

  echo ' ' >> $HH
  cat <<EOF >> $HH
<hr>
<i>Generated on `date` by ${cmd:t}</i><br>
<i>Comments and corrections are welcome!</i><br>
<i>Please write to stolfi (at) ic.unicamp.br.</i>
</body>
</html>
EOF

end

# Create the master image index:

if ( $#pages == 1 ) then
  mv "${pgname}-aa.html" "${pgname}.html" 
else
  set HH = "${pgname}.html"
  if ( -r ${HH} ) /bin/mv ${HH} ${HH}~
  cat <<EOF > $HH
<!doctype html public "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <title>${title}</title>
</head>
<body>
<h1>${title}</h1>
EOF

  foreach pag ( ${pages} )
    set LL = "${pgname}-${pag:e}.html"
    echo "<p><a href="'"'"${LL}"'"'">${pag:e}</a></p>" >> $HH
  end

  cat <<EOF >> $HH
<hr>
<i>Generated on `date` by ${cmd:t}</i><br>
<i>Comments and corrections are welcome!</i><br>
<i>Please write to stolfi (at) ic.unicamp.br.</i>
</body>
</html>
EOF
  
endif

/bin/rm -f ${imlist} ${pglist}.*
