#!/opt0/tcl/bin/sytcl
#
# This tool will make an elaborate HTML report generator page
# for any sybase table.  arg:  FQTN
# hard-coded:  user pass htmlv
# args: fqtn server
#
# Make sure this path is right for your system
set tclroot /opt0/tcl
loadlibindex $tclroot/local/ucosyb.tlib
#
global dbpipe1 server sybmsg table user 
#
#
# This is the target directory for the HTML pages
#
echo "args should be htmlversion, www_user_dir, www_sub_dir, fqtn, sybserver"
if {[llength $argv] < 4} {
exit 1
}
#
set htserver www.ucolick.org
# set htserver www.keck.hawaii.edu:8080
lassign $argv htmlv udir tdir fqtn server
#
set target /local/www/$udir/$tdir
#
# file descriptors:  select page, browse page, dict page
#
set sfp [open $target/SEL${htmlv}_$fqtn.html w]
set bfp [open $target/BRO${htmlv}_$fqtn.html w]
set dfp [open $target/DIC${htmlv}_$fqtn.html w]
#
lassign [split $fqtn .] db usr tbl
#
set user guest
set pass rsvp
#
set base $db
# HTML version?  If you set this to 3, you will use table macros
# for dictionary and select form.
# set htmlv 3
# set htmlv 2
if {$htmlv == 3} {
	echo "WARNING, HTML3 in use, pages using <table> will be created."
}
set dbpipe1 [sybOpen $base $user $pass $server]
set dbpipe2 [sybOpen $base $user $pass $server]
#
global debugs
set debugs {}
#
# dictionary
#       This is the name of the data dictionary table -- if any!
#       You want to uncomment the next line if you don't implement it
#       read only to all users
# set dictionary "NONE"
if {$server == "UCO-ADMIN"} {
set dictionary "info.dbo.data_dict"
} else {
set dictionary "metabase.dbo.data_dict"
}
# establish lists
set cols ""
set colnames ""
#
proc getCols {usr tbl} {

	global sybmsg debugs db cols colnames types sizes nulls indices index

	echo "get col types and sizes"
        set sqlcmd "select a.name, c.name, a.length, a.status from \
$db.dbo.syscolumns a, $db.dbo.sysobjects b, $db.dbo.systypes c where \
a.id = b.id and b.name = '$tbl' and b.uid = user_id('$usr') and \
a.usertype = c.usertype"

        set sqt "syscolumns/sysobjects"
        set res [doSQL 1]

        if {[lindex $res 0] == "ERROR"} {
                echo "Something went wrong here:"
                echo "$sqlcmd\n$sybmsg(msgno): $sybmsg(msgtext)"
                exit 1
        }

	while {1 == 1} {
		set line [sybNext 1]
		if {$line == ""} {break}
		lassign $line c ct cs cn
		set types($c) $ct
		set nulls($c) $cn
		set sizes($c) $cs
		set index($c) 0
		lappend colnames $c
	}

	set sqlcmd "sp_helpindex $tbl"
	set sqt "sp_helpindex"
        set res [doSQL 1]

        if {[lindex $res 0] == "ERROR"} {
                echo "Something went wrong here:"
                echo "$sqlcmd\n$sybmsg(msgno): $sybmsg(msgtext)"
                exit 1
        }

	while {1} {
		set line [sybNext 1]
                if {$line == ""} {break}
		lassign $line ind_name junk ind_col
		set ind_col [string trim $ind_col]
		lappend indices $ind_col
		set index($ind_col) 1
	}
}
#
proc writeBoiler {n} {

	global sfp bfp dfp tbl fqtn dictionary htmlv htserver

	for_file line BoilerPlate.$n {

		regsub -all GENERIC_TABLE $line $tbl line
		puts $sfp "$line"

	}
	if {$n == 1} {
	if {$dictionary != "NONE"} {
	puts $sfp "If you want to know what the fields mean, check the"
	puts $sfp "<a href=DIC${htmlv}_$fqtn.html>data dictionary information</a> for this table." 
	}
	puts $sfp "You may also want to use the <a href=BRO${htmlv}_$fqtn.html>browser</a> form for this table to see one record at a time.<hr>"
	if {$htmlv == 3} {
	puts $sfp "<form method=\"POST\" action=\"http://$htserver/cgi-bin/plotSQL\">"
	} else {
	puts $sfp "<form method=\"POST\" action=\"http://$htserver/cgi-bin/searchSQL\">"
	}
	}

	if {$dictionary != "NONE"} {
	for_file line DictBoiler.$n {

		regsub -all GENERIC_TABLE $line $tbl line
		puts $dfp "$line"

	}
	}

	for_file line BrowseBoiler.$n {
		regsub -all GENERIC_TABLE $line $tbl line
		puts $bfp "$line"
	}


        if {$n == 1} {
	if {$dictionary != "NONE"} {
	puts $bfp "If you want to know what the fields mean, check the"
        puts $bfp "<a href=DIC${htmlv}_$fqtn.html>data dictionary information</a> for this table."
	}
	puts $bfp "You may also want to use the <a href=SEL${htmlv}_$fqtn.html>Search form</a> for this table to select lists of records.<hr>"
        puts $bfp "<form method=\"POST\" action=\"http://$htserver/cgi-bin/browseSQL\">"
	}
}
#
proc writeDict {} {

        global fqtn db tbl usr colnames dictionary htmlv
        global sybmsg dfp types nulls sizes index

	if {$htmlv == 3} {
	puts $dfp "<table border>"
	puts $dfp "<tr><th align=left >Field<th align=left >Type<th align=left>Description"
	} else {
        puts $dfp "<ul>"
	}
#
	echo "get dictionary data"

        foreach c $colnames {
#		echo "ddict col $c"
		set sqlcmd "select ddesc from $dictionary where dbase = '$db' and owner = '$usr' and tbln = '$tbl' and fldn = '$c'"
		set sqt $dictionary
		doSQL 1
		set dd [sybNext 1]
		set chara $types($c)
		if {!$nulls($c)} {
			append chara ", no nulls"
		}
		set idx ""
		if {$index($c)} {
			set idx *
		}
		if {$htmlv == 3} {
		puts $dfp "<tr><td><b>$c</b><td><i>$chara</i> $idx<td>[stringFix2 $dd out]"
		} else {
                puts $dfp "<li><b>$c</b> (<i>$chara</i> $idx) [stringFix2 $dd out]"
		}
        }

	if {$htmlv == 3} {	
	puts $dfp "</table>"
	} else {
	puts $dfp "</ul>"
	}
		
}
#
proc writeBody {} {

	global fqtn tbl usr colnames htmlv
	global sybmsg sfp bfp server types index

	set numeric "int real float smallint money smallmoney"

	if {$htmlv == 3} {
	puts $sfp "<input type=radio name=pltype value=line checked>Line Plot<br><input type=radio name=pltype value=scat>Scatter Plot<br>"
	puts $sfp "<input type=radio name=pltype value=data>No Plot or Report(data only)<br>"
	puts $sfp "<input type=radio name=pltype value=stats>Stats only (min/max/avg of all selected columns) -- this can take a while<br>"
	puts $sfp "<table>"
	puts $sfp "<tr><th align=left>Field<th align=left>Print?<th align=left>pltX<th align=left>PltY<th align=left>Func<th align=left>Match Value"
	} else {
        puts $sfp "<ul>"
	}
#
	set i 0
	foreach c $colnames {
		if {$i < 3} {
			set ch checked
		} else {
			set ch ""
		}
		set id1 ""
		set id2 ""
		if {$index($c)} {
			set id1 "<i>"
			set id2 "</i>"
		}
		if {$htmlv == 3} {
		if {[lsearch $numeric $types($c)] >= 0} {
		puts $sfp "<tr><td><b>$id1$c$id2</b><td><input type=\"checkbox\" name=p.$c value=1 $ch><td><input type=\"radio\" name=x value=$c><td><input type=\"checkbox\" name=y.$c value=1><td><input name=s.$c size=5><td><input name=m.$c size=30>"
		} else {
#		if {[string first date $types($c)] >= 0} {
#		puts $sfp "<tr><td><b>$id1$c$id2</b><td><input type=\"checkbox\" name=p.$c value=1 $ch><td><input type=\"radio\" name=x value=$c><td><input type=\"checkbox\" name=y.$c value=1><td><input name=s.$c size=5><td><input name=m.$c size=30>"
#		}
		puts $sfp "<tr><td><b>$id1$c$id2</b><td><input type=\"checkbox\" name=p.$c value=1 $ch><td><td><td><td><input name=m.$c size=30>"
		}
		} else {
		if {[lsearch $numeric $types($c)] >= 0} {
		puts $sfp "<li><b>$id1$c$id2</b> -- Print? <input type=\"checkbox\" name=p.$c value=1 $ch> Stat:<input name=s.$c size=5> Match:<input name=m.$c size=30>"
		} else {
		puts $sfp "<li><b>$id1$c$id2</b> -- Print? <input type=\"checkbox\" name=p.$c value=1 $ch> Match: <input name=m.$c size=30>"
		}
		}
		incr i
	}
	if {$htmlv == 3} {	
	puts $sfp "</table>"
	} else {
	puts $sfp "</ul>"
	}
	puts $sfp "You may alternatively, if you are familiar with SQL expressions, compose your own selection expression (what would come after the <b>where</b> in the select statement):<br>"
	puts $sfp "<TEXTAREA NAME=\"sqlq\" ROWS=5 COLS=60></TEXTAREA>"
	puts $sfp "<hr><input type=\"submit\" value=\"Search!\">"
	puts $sfp "<input type=hidden name=\"fqtn\" value=$fqtn> <input type=hidden name=\"server\" value=$server>"
	puts $bfp "<input type=hidden name=\"fqtn\" value=$fqtn> <input type=hidden name=\"server\" value=$server>"
}
#
#############################################################################
#
echo "get columns $tbl"
getCols $usr $tbl
#
echo "write boiler 1"
writeBoiler 1
#
echo "write body $tbl"
writeBody 
if {$dictionary != "NONE"} {
echo "write dictionary $tbl"
writeDict
}
# 
echo "write boiler 2"
writeBoiler 2
#
close $sfp
close $bfp
close $dfp
#
