#! /bin/csh -f 
# Last edited on 2008-02-04 18:28:18 by stolfi

set cmd = "$0"
set usage = "${cmd:t} [-reuse|-force] [-reuseindex] [ -title STRING ] INDEXDIR < RAWINDEX"

# Creates a structured set of HTML index pages given a raw subject
# index. Puts the pages in the directory "INDEXDIR".
# 
# The input file should contain a list of pairs KEY DIR, where KEY is
# an "indexing key", and DIR is a "standard image directory".
#
# An indexing key is a string delimited by braces {} that 
# does not contain any brace or newline..
# Capitalization and leading and trailing spaces are irrelevant.
# 
# A "standard image directory" should contain a standard set of files
# -- including a PNG image ("p.png") and its icon ("p-icon.png"), or
# GIF versions thereof -- and a caption file ("p.comments"). The
# latter usually contains one or more indexing keys.
# 
# A standard image directory should also include a "p.html-inc" file,
# that is the pat of the index referring to this image. This file is
# re-created automatically is missing or obseolte. The "-force" flag
# forces this file to be re-created for all images, while "-reuse"
# prevents it from being recreated.
# 
# The HTML index consists of a main page "index.html", which displays
# a list of subjects.  Clicking on one entry of this list 
# brings up an index page for the images whose caption contains
# that subject (ignoring differences in capitalization).
# 
# The "-reuseindex" flag reuses the main page "index.html" and the
# files "entries/?????" and "htitle/?????" created by a previous run
# of this script, and rebuilds only the secondary "index-?????*.html
# pages.

set force = 0
set reuse = 0
set reuseindex = 0
set mkopts = ( )
set title = "Image index"

while ( ( $#argv > 0 ) && ( "/$1" =~ /-* ) ) 
  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" == "/-reuseindex" ) then
    set reuseindex = 1; shift
  else if ( ( $#argv >= 2 ) && ( "/$1" == "/-title" ) ) then
    set title = "$2"; shift; shift
  else 
    echo "bad option $1";
    echo "usage: ${usage}"; exit 1
  endif
end

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

set tmp = "/tmp/$$"
set tbin = ${STOLFIHOME}/EXPORT/images/tools/bin
set pbin = ${STOLFIHOME}/bin

source "${STOLFIHOME}/EXPORT/images/tools/lib/${PLATFORM}/setpaths.csh"

set path = ( ${tbin} ${pbin} $path )

if ( ! ( -d ${htmldir} ) ) then
  echo "Creating the ${htmldir} directory..."
  mkdir ${htmldir} ${htmldir}/entries ${htmldir}/htitle
else
  echo "eaning up the ${htmldir} directory..."
  if ( $reuseindex ) then
    echo "Retaining the files ${htmldir}/{htitle,entries}/* ."
  else
    echo "Deleting the files ${htmldir}/{htitle,entries}/* ..."
    ( cd ${htmldir} && /bin/rm -f img index.html entries/????? htitle/????? )
  endif
  echo "Deleting the secondary pages index-*.html ..."
  ( cd ${htmldir} && /bin/rm -f index-[0-9]*.html )
endif

echo "Creating link "img" to current directory..."

set curdir = "`pwd`"
( cd ${htmldir} && /bin/rm -f img )
( cd ${htmldir} && ln -s ${curdir} img )

echo "Creating the normalized index file..."

set normix = ".norm-index.tbl"

cat \
  | map-field \
      -v inField=1 -v outField=1 \
      -v table=NORMAL-KEYS.tbl \
      -v forgiving=1 \
  | gawk '(NF == 3){ print $1, ("img/" $3); }' \
  | sort | uniq \
  > ${htmldir}/${normix}

echo "Changing to the ${htmldir} directory..."

cd ${htmldir} || exit 1

echo "Creating the image list..."

set imglist = ".image.list"

cat ${normix} \
  | gawk '/./{ print $2; }' \
  | sort | uniq \
  > ${imglist}

echo 'Creating the per-image ".html-inc" files...'
foreach imgdir ( `cat ${imglist}` )
  set pngimg = "${imgdir}/p.png"
  set gifimg = "${imgdir}/p.gif"
  if ( -r "${pngimg}" ) then
    set img = "${pngimg}" 
  else if ( -r "${gifimg}" ) then
    set img = "${gifimg}" 
  else
    set img = "NONE"
  endif
  if ( "/${img}" != "/NONE" ) 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 
      set sources = ( ${img} ${img:r}-icon.${img:e} ${img:r}.comments )
      set remake = `${STOLFIHOME}/bin/check-dependencies ${entry} ${sources}`
    endif
    if ( ${remake} == 1 ) then 
      ${STOLFIHOME}/bin/make-image-index-entry ${img} > ${entry}
    endif
  else
    echo "unreadable image: ${img}"; exit 1
  endif
end

if ( $reuseindex ) then
  echo "Reusing the master HTML index page."
else
  echo "Creating the master HTML index page..."

  set mh = "index.html"
  
  cat ${normix} \
    | format-subject-index \
        -v title="${title}" \
    > ${mh}
endif

echo "Creating the per-subject HTML index pages..."

foreach efile ( entries/????? )
  set pg = "${efile:t}"
  set tfile = "htitle/${pg}"
  set htmlfile = "index-${pg}.html"
  cat ${efile} \
    | egrep -v '[-][0-9][0-9]*[N]' \
    | make-image-index ${mkopts} -title "`cat ${tfile}`" ${htmlfile}
  # /bin/rm -f ${efile} ${tfile}
end