#!/usr/local/bin/tclsh8.1
## Modified from a script by Mark Handley
## I haven't tested this yet.

set files [glob *.{jpg,JPG}]

set lores [open "index.html" "w"]
puts $lores "<HEAD><TITLE>Images</TITLE></HEAD>
<BODY bgcolor=\"#000000\" text=\"#ffffff\" link=\"#0000ff\" vlink=\"#b20000\">"

foreach file $files {
    set shortfile [lindex [split $file "."] 0]
    puts "converting $file..."
    set output [exec identify $file]
    set size [lindex $output 1]
    #puts "size $size"
    set size1 [split $size x]
    set width [lindex $size1 0]
    set height [lindex $size1 1]
    #puts "height $height width $width"
    if {$height > "800" || $width > "800"} {
      set big true
    } else {
      set big false
    }
    if {$big == "true"} {
      set midres [open $shortfile.html "w"]
      puts $midres "<HEAD><TITLE>Image $shortfile</TITLE></HEAD> <BODY bgcolor=\"#000000\" text=\"#ffffff\" link=\"#0000ff\" vlink=\"#b20000\">"
      puts $midres "<a href=./$file><img src=midres-$file></a>"
      puts $midres "</body>"
      close $midres
    }
    if {$height > $width} {
	exec convert -size 120x160 $file lores-$file
	if {$big == "true"} {
	  exec convert -size 600x800 $file -resize 600x800 midres-$file
	}
    } else {
	exec convert -size 160x120 $file lores-$file
	if {$big == "true"} {
	  exec convert -size 800x600 $file -resize 800x600 midres-$file
	}
    }
    if {$big == "true"} {
      puts $lores "<a href=$shortfile.html><img src=lores-$file height=120></a>"
    } else {
      puts $lores "<a href=$file><img src=lores-$file height=120></a>"
    }
    puts "done"
}

puts $lores "</BODY>"
