#! /bin/csh -f
# Last edited on 2003-12-14 23:30:15 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
# animations.
# 
# 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-0.0000.png - First frame
#   ${ra}/main-0.5000.png - Middle frame
#   ${ra}/main-1.0000.png - Last frame
#   ${ra}/main-m.gif - Animated gif
#   ${ra}/main-s.png - Strip with sample frames
# 
# If "-original" is given, makes an index "index-orig.html" for the
# "-orig" versions instead of the edited/rebuilt versions.
#  
# 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}".
#
# The thumbnail version of file "{FOO}.png" or "{FOO}-orig.png"
# 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;

# Clock value of frame to use as icon (format "N.N"):
set iconclock = "0.0";
# set iconclock = "0.5";

# 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-2003-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 images to show
set firstname = "main-0.0000${hotsuf}"
set middlename = "main-0.5000${hotsuf}"
set lastname = "main-1.0000${hotsuf}"
set stripname = "main-s${hotsuf}"
set movname = "main-m${hotsuf}"
set altmovname = "main-m${altsuf}"

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}
  <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.
  #    ${stripfile} = file name of the "hot" filmstrip.
  #    ${movfile} = file name of the full "hot" movie.
  #    ${moviconfile} = file name of the iconic version of the "hot" movie.
  #    ${altmovfile} = file name of the alternate movie.
  # 
  #    ${frabutt} = the HTML fragment of the single-frame buttons.
  #    ${strbutt} = the HTML fragment of the filmstrip button.
  #    ${movbutt} = the HTML fragment of the hot movie button.
  #    ${altmovbutt} = the HTML fragment of the alternate movie 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 frabutt = "[×]"
    set strbutt = "[×]"
    set movbutt = "[×]"
    set altmovbutt = "[×]"
    set povbutt = "[×]"
    set stripfile = "missing.png"
    set movfile = "missing.png"
    set moviconfile = "missing-i.png"
    set altmovfile = "missing.png"
  else if ( -r ${ra}/.absent ) then
    # Absentee student:
    set frabutt = "[×]"
    set strbutt = "[×]"
    set movbutt = "[×]"
    set altmovbutt = "[×]"
    set povbutt = "[×]"
    set stripfile = "absent.png"
    set movfile = "absent.png"
    set moviconfile = "absent-i.png"
    set altmovfile = "absent.png"
    # Ckeck if the POV-ray source exists, just in case:
    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 = "[×]"
    else
      set povbutt = '[<a href="'"${povfile}"'">fonte</a>]'
    endif

    # Default movie icon is "missing-i.png", eventually changed below:
    set moviconfile = "missing-i.png"
    set moviconok = 0

    # Create single-frame button strip ${frabutt}:
    set frabutt = ""
    foreach fc ( \
      ${firstname}/0.0 \
      ${middlename}/0.5 \
      ${lastname}/1.0 \
    )
      set franame = "${fc:h}" 
      set clock = "${fc:t}"

      # Get:
      #   ${frafile} = file name of selected full "hot" frame.
      #   ${fraiconfile} = file name of iconic versions of selected "hot" frame.
      #   ${clock} = clock value of the selected frame.
      
      # Look for frame image, of any type (defaults to "missing.png"): 
      set frafile = "missing.png"
      foreach ext ( png gif jpg )
        if ( -r "${ra}/${franame}.${ext}" ) then
          set frafile = "${ra}/${franame}.${ext}"; break
        endif
      end
      
      # Get frame icon image:
      set fraiconfile = "${ra}/${franame}-i.png"
      if ( "${frafile}" != "missing.png" ) then
        # Remove empty or outdated icon:
        if ( -r ${fraiconfile} ) then
          if ( ( -z ${fraiconfile} ) || { /usr/bin/test ${frafile} -nt ${fraiconfile} } ) then
            /bin/rm -f ${fraiconfile} 
          endif
        endif
        # Make sure that the icon does exist:
        if ( ! ( -r ${fraiconfile} ) ) then
          echo "=== creating thumbnail image for ${frafile} ==="
          convert ${frafile} \
              -geometry "x${iconHeight}" \
              +profile '*' \
              -colors 254 -dither \
            ${fraiconfile}
        endif
        set frabutt = "${frabutt}"'[<a href="'"${frafile}"'">'"${clock}"'</a>]'
      else
        # Remove thumbnail, if any
        if ( -r ${fraiconfile} ) /bin/rm -f ${fraiconfile}
        # Use the "missing" image icon:
        set fraiconfile = "missing-i.png"
        set frabutt = "${frabutt}[×]"
      endif
      echo "  clock = ${clock} frame = ${frafile} icon = ${fraiconfile}"
      
      # Save frame icon as movie icon:
      if ( ( "${fraiconfile}" != "missing-i.png" ) && ( ! ${moviconok} ) ) then
        set moviconfile = "${fraiconfile}"
        if ( "/${clock}" == "/${iconclock}" ) set moviconok = 1
      endif
    end

    # Get filmstrip file name (may have any extension)
    set stripfile = "missing.png"
    foreach ext ( png gif jpg )
      if ( -r "${ra}/${stripname}.${ext}" ) then
        set stripfile = "${ra}/${stripname}.${ext}"; break
      endif
    end
    if ( ( -r ${stripfile} ) && ( "${stripfile}" != "missing.png" ) ) then
      set strbutt = '[<a href="'"${stripfile}"'">quadros</a>]'
    else
      set strbutt = "[×]"
    endif
    echo "  file strip = ${stripfile}" 

    # Get file name of "hot" movie:
    set movfile = "${ra}/${movname}.gif"
    if ( ${rebuild} ) then
      if ( -r ${povfile} ) then
        # (Re)build movie if necessary:
        if ( ( ! ( -r ${movfile} ) ) || ( -z ${movfile} ) ) then
          echo "=== (re)building movie ${movfile} ==="
          ( cd ${ra} && make ${movfile:t} )
          echo "=== done rebuilding ${movfile} ==========================="
        endif
      else
        echo "*** File ${povfile} missing, can't rebuild ${movfile}"
      endif
    endif
    if ( ! ( -r ${movfile} ) ) then
      echo "*** File ${movfile} missing"
      set movfile = "missing.png"
    endif

    # Create "hot" movie button, or substitute "missing.png":
    if ( "${movfile}" != "missing.png" ) then
      set movbutt = '[<a href="'"${movfile}"'">filme</a>]'
    else
      set movbutt = "[×]"
    endif
    echo "  movie file = ${movfile}" 

    # Get name of alternate movie file:
    set altmovfile = "${ra}/${altmovname}.gif"
    if ( ! ( -r ${altmovfile} ) ) then
      echo "*** File ${altmovfile} missing"
      set altmovfile = "missing.png"
    endif

    # Create alternate movie button:
    if ( "${altmovfile}" != "missing.png" ) then
      set altmovbutt = '[<a href="'"${altmovfile}"'">'"${alttxt}"'</a>]'
    else
      set altmovbutt = "[×]"
    endif
    echo "  alternate movie file = ${altmovfile}" 
  endif

  # Format the movie icon
  set iconbutt = '<a href="'"${movfile}"'"><img src="'"${moviconfile}"'"></a>'
  
  # echo "${frabutt} ${strbutt} ${movbutt} ${altmovbutt} ${iconbutt}" 
  
  # Output the entry
  if ( ${n} == 0 ) then
    echo "    <tr valign=top>" >> ${htmfile}
  endif
  cat <<EOF >> ${htmfile}
    <td nowrap>${iconbutt}<br><font size=+3><b>${ra}</b></font><br>&nbsp;</td>
    <td nowrap><small>${frabutt}&nbsp;&nbsp;<br>${strbutt}&nbsp;&nbsp;<br>${movbutt}&nbsp;&nbsp;<br>${altmovbutt}&nbsp;&nbsp;<br>${povbutt}&nbsp;&nbsp;</small></td>
EOF
  @ n = ${n} + 1
  if ( ${n} >= 4 ) then
    echo "    </tr>" >> ${htmfile}
    @ n = 0
  endif
end

if ( ${n} > 0 ) then
  echo "    </tr>" >> ${htmfile}
endif

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