#! /bin/csh -f
# Last edited on 2005-01-03 18:01:32 by stolfi

set usage = "$0 [-original] [-absentOK] [-iconHeight NN] TITLE"

# Creates an index page "index.html" or "index-orig.html" for computer
# graphics homework hand-ins. This version is specialized for still
# images.
# 
# Reads files 
# 
#   rausrs.txt : RA numbers and login names of all registered students 
#   aus.txt : RA numbers of absentees
#   intro.html (if present) : blurb to be included in page, before results.
# 
# Rejects input from absentee students, unless "-absentOK" is given.
# For each non-absentee student, expects to find files
# 
#   ${ra}/main.pov - Main POV-ray file (edited by instructor)
#   ${ra}/main.png - GIF image.
#  
# The above files are the versions that were edited and/or rebuilt
# by the teacher. The original version of file "{FOO}.{BAR}",
# as turned in by the student, is assumed to be "{FOO}-orig.{BAR}".
# However, the original image may have extension ".gif" or ".jpg"
# instead of ".png".
# 
# If "-original" is given, makes an index "index-orig.html" for the
# "-orig" versions instead of the edited/rebuilt versions.
#
# The thumbnail version of file "{FOO}.{BAR}" or "{FOO}-orig.{BAR}"
# is created, if needed, with name "{FOO}-i.png" "{FOO}-orig-i.png". 

# Name suffixes and button labels for "hot" and alternate versions:
set hotsuf = "";      set hottxt = "novo"
set altsuf = "-orig"; set alttxt = "orig"

# Options for {process-absentees}:
set absentopt = ( )

# Default icon height (pixels):
set iconHeight = 60;

# TRUE if the derived files should be rebuilt as needed:
set rebuild = 1

while ( ( $#argv > 0 ) && ( "/$1" =~ /-* ) ) 
  if ( ( $#argv >= 1 ) && ( "/$1" == "/-absentOK" ) ) then
    set absentopt = ( "-absentOK" ); shift; 
  else if ( ( $#argv >= 1 ) && ( "/$1" == "/-original" ) ) then
    set hotsuf = "-orig"; set hottxt = "orig";
    set altsuf = "";      set alttxt = "novo";
    set rebuild = 0; shift; 
  else if ( ( $#argv >= 2 ) && ( "/$1" == "/-iconHeight" ) ) then
    set iconHeight = "$2"; shift; shift; 
  else
    echo "bad option: $1";
    echo "usage: ${usage}"; exit 1
  endif
end

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

set title = "$1"; shift;

set tooldir = "${STOLFIHOME}/public_html/courses/MC930-2004-2-A/progs/tools"

setenv POVPUB ${STOLFIHOME}/pkg/povray-3.50c-1/PUB

set path = ( . ${tooldir} ${POVPUB}/${PLATFORM}/bin/ $path )

set rafile = "rausrs.txt"
set abfile = "aus.txt"
set hdrfile = "intro.html"

set htmfile = "index${hotsuf}.html"

# Names of hot and alternate image files to be shown (minus extension):
set imgname = "main${hotsuf}"
set altimgname = "main${altsuf}"

# Remove files of absentees and provide "absent" picture for them:
if ( -r ${abfile} ) then 
  process-absentees ${absentopt} `cat ${abfile} | egrep -v '^ *([#]|$)'`
endif

# Save old version of index page
if ( -e ${htmfile} ) then
  /bin/mv ${htmfile} ${htmfile}~
endif

# Write index page header:
cat <<EOF >> ${htmfile}
<html>
<head>
<title>${title} (${hottxt})</title>
</head>
<body bgcolor="#cbcbcb">

<h1>${title} (${hottxt})</h1>
<h2><tt>[<a href="index${altsuf}.html">${alttxt}</a>&gt</tt></h2>


EOF

# Copy preformatted prologue:
if ( -r ${hdrfile} ) then
  cat ${hdrfile} >> ${htmfile}
endif

# Get numbers of all registered students:
set ras = ( `cat ${rafile} | egrep -v '^ *([#]|$)' | gawk '//{print $1;}'` )

cat <<EOF >> ${htmfile}
<hr>
  <table cellspacing=1 cellpadding=1 border=0>
EOF

@ n = 0
foreach ra ( ${ras} )

  # Define, for student ${ra}:
  #
  #    ${povfile} = file name of "hot" source file.
  #    ${altpovfile} = file name of alternate source file.
  #    ${imgfile} = file name of the full "hot" image.
  #    ${iconfile} = file name of the iconic version of the "hot" image.
  #    ${altimgfile} = file name of the alternate image.
  #
  #    ${imgbutt} = the HTML fragment of the image button.
  #    ${altimgbutt} = the HTML fragment of the alternate image button.
  #    ${povbutt} = the HTML fragment of the source button.

  set povfile = "${ra}/main${hotsuf}.pov"
  set altpovfile = "${ra}/main${altsuf}.pov"

  if ( ! ( -d ${ra} ) ) then
    # non-existant directory:
    echo "*** ${ra} missing or not directory"
    set povbutt = "[×]"
    set imgbutt = "[×]"
    set altimgbutt = "[×]"
    set imgfile = "missing.png"
    set altimgfile = "missing.png"
    set iconfile = "missing-i.png"
  else if ( -r ${ra}/.absent ) then
    # Absentee student:
    set povbutt = "[×]"
    set imgbutt = "[×]"
    set altimgbutt = "[×]"
    set imgfile = "absent.png"
    set altimgfile = "absent.png"
    set iconfile = "absent-i.png"
    # Ckeck if the POV-ray source exists, just for safety:
    if ( -r ${povfile} ) then
      echo "*** ${ra}/.absent and ${povfile} both present"; exit 1
    else if ( -r ${altpovfile} ) then
      echo "*** ${ra}/.absent and ${altpovfile} both present"; exit 1
    endif
  else 
    # Ckeck whether the POV-ray source exists:
    if ( -r ${povfile} ) then
      set povbutt = '[<a href="'"${povfile}"'">fonte</a>]'
    else
      set povbutt = "[×]"
    endif

    # Get file name ${imgfile} of the "hot" image:
    if ( ${rebuild} ) then
      # The image name is fixed, try to rebuild it if needed:
      set imgfile = "${ra}/${imgname}.png"
      if ( -r ${povfile} ) then
        # (Re)build image if necessary:
        if ( ( ! ( -r ${imgfile} ) ) || ( -z ${imgfile} ) ) then
          echo "=== (re)building image ${imgfile} ==="
          ( cd ${ra} && make ${imgfile:t} )
          echo "=== done rebuilding ${imgfile} ==========================="
        endif
      else
        echo "*** File ${povfile} missing, can't rebuild ${imgfile}"
      endif
      if ( ! ( -r ${imgfile} ) ) then
        echo "*** File ${imgfile} missing"
        set imgfile = "missing.png"
      endif
    else
      # Look for existing image of some type, default "missing.png":
      set imgfile = "missing.png"
      foreach ext ( png gif jpg )
        if ( -r "${ra}/${imgname}.${ext}" ) then
          set imgfile = "${ra}/${imgname}.${ext}"; break
        endif
      end
    endif
    
    # If image exists, then get its icon, else use "missing-i.png":
    set iconfile = "${ra}/${imgname}-i.png"
    if ( "${imgfile}" != "missing.png" ) then
      # Remove empty or outdated icon:
      if ( -r ${iconfile} ) then
        if ( ( -z ${iconfile} ) || { /usr/bin/test ${imgfile} -nt ${iconfile} } ) then
          /bin/rm -f ${iconfile} 
        endif
      endif
      # Make sure that the icon does exist:
      if ( ! ( -r ${iconfile} ) ) then
        echo "=== creating thumbnail image for ${imgfile} ==="
        convert ${imgfile} \
            -geometry "x${iconHeight}" \
            +profile '*' \
            -colors 254 -dither \
          ${iconfile}
      endif
      set imgbutt = '[<a href="'"${imgfile}"'">img</a>]'
    else
      # Remove thumbnail, if any
      if ( -r ${iconfile} ) /bin/rm -f ${iconfile}
      # Use the "missing" image icon:
      set iconfile = "missing-i.png"
      set imgbutt = "[×]"
    endif

    # Get alternate image file, if any:
    set altimgfile = "missing.png"
    foreach ext ( png gif jpg )
      if ( -r "${ra}/${altimgname}.${ext}" ) then
        set altimgfile = "${ra}/${altimgname}.${ext}"; break
      endif
    end
    if ( ( -r ${altimgfile} ) && ( "${altimgfile}" != "missing.png" ) ) then
      set altimgbutt = '[<a href="'"${altimgfile}"'">'"${alttxt}"'</a>]'
    else
      set altimgbutt = "[×]"
    endif
  endif

  # Output the entry
  if ( ${n} == 0 ) then
    echo "    <tr>" >> ${htmfile}
  endif
  cat <<EOF >> ${htmfile}
    <td nowrap> <a href="${imgfile}"><img src="${iconfile}"></a>&nbsp;&nbsp;<br><font size=+2><b>${ra}</b></font>&nbsp;<br>${povbutt}&nbsp;${altimgbutt}&nbsp;<br>&nbsp;</td>
EOF
  @ n = ${n} + 1
  if ( ${n} >= 5 ) then
    echo "    </tr>" >> ${htmfile}
    @ n = 0
  endif
end

# Write index page trailer
if ( ${n} > 0 ) then
  echo "    </tr>" >> ${htmfile}
endif

cat <<EOF >> ${htmfile}
  </table>
<hr>
</body>
</html>
EOF