#!/bin/csh -f

# Take a bunch of images and make them viewable in html.
# Usage: tblimages [-01234] file1T.jpg file2T.jpg ...
# Specify the thumbnail images, not the full-sized ones.
# Copyright 2000,2002 by Akkana Peck, akkana@shallowsky.com.
# You may use and distribute this program under the GPL.

# First, see if we have a flag argument for #columns.
# If ncolumns is 1, then we'll add text to the right of each image.

set usetables
set ncolumns = 4

while ($#argv >= 1)
  if ("$argv[1]" !~ -*) break

  if ("$argv[1]" == "-0") then
    set ncolumns = 0
  else if ("$argv[1]" == "-1") then
    set ncolumns = 1
  else if ("$argv[1]" == "-2") then
    set ncolumns = 2
  else if ("$argv[1]" == "-3") then
    set ncolumns = 3
  else if ("$argv[1]" == "-4") then
    set ncolumns = 4
  else if ("$argv[1]" == "-5") then
    set ncolumns = 5
  else if ("$argv[1]" == "-6") then
    set ncolumns = 6
  else if ("$argv[1]" == "-7") then
    set ncolumns = 7
  else if ("$argv[1]" == "-8") then
    set ncolumns = 8
  else if ("$argv[1]" == "-9") then
    set ncolumns = 9
  else if ("$argv[1]" == "-p") then
    set usephp
  else if ("$argv[1]" == "-c") then
    set usecgi
  else if ("$argv[1]" == "-r") then
    set recursive
  else if ("$argv[1]" == "-b") then
    set border
  else if ("$argv[1]" == "-n") then
    set noheaders
  else if ("$argv[1]" == "-a") then
    set annotate
  else
    echo "Usage: tblimages [-12345678] [-c] {[-r] | imageT.jpg ...}"
    exit
  endif
  shift
end

set files = ( $argv )

if ($?recursive) then
  set files = ($files `find . -name "*T.jpg" `)
endif

if (! $?noheaders ) then
  cat <<EOSTART
<html>
<head>
<title>Images</title>
</head>

<body bgcolor="white" fg="black" link="blue" vlink="purple">
<h1>Images</h1>

EOSTART
endif

if ($?usetables) then
  echo "<table>"
endif

if ($?usetables) then
  set colno = 1
  echo "<tr>"
endif
set annotatestr = ""

foreach fil ( $files )
  set bigimage = `echo $fil | sed 's/T.jpg/.jpg/'`
  # set bigimage = `echo $fil | sed 's/T//'`
  if ($?usetables) then
    echo '<td>'
  endif
  if ($?usephp) then
    echo '<a href="showpix.php?pic='$bigimage'">'
  else if ($?usecgi) then
    echo '<a href="showpix.cgi?pic='$bigimage'">'
  else
    echo '<a href="'$bigimage'">'
  endif
  echo '<img align=right src="'$fil'"'
  if ($?border) then
    echo " border=2"
  else
    echo " border=0"
  endif
  echo ' alt="[' $fil ']">'
  echo "</a>"
  if ($?usetables) then
    echo "</td>"
    set annotatestr = "$annotatestr<th>$fil"
  endif

  if ($?usetables) then
    @ colno = ($colno + 1)
    if ($colno > $ncolumns) then
      if ($ncolumns == 0) then
        echo "<td>"
        echo $bigimage
      endif
      echo "</tr>"
      if ($?annotate) echo "<tr>$annotatestr</tr>"
      set annotatestr = ""
      set colno = 1
      echo ""
      echo "<tr>"
    endif
  endif
end

if ($?usetables) then
  echo "</tr>"
  if ($?annotate) echo "<tr>$annotatestr</tr>"
  echo "</table>"
endif

if (! $?noheaders) then
  cat <<EOEND

<hr>
<a href="http://www.shallowsky.com/photo.html">Akkana's Photo Page.</a><br>
<a href="http://www.shallowsky.com/">Shallow Sky home</a><br>
<address>
<a href="http://www.shallowsky.com/mailme.html">Mail Comments</a>
</address>

</body>
</html>
EOEND
endif
