#!/opt/tcl/bin/sytcl
#!/usr/local/tcl74/bin/dbtcl
#
# set tclroot /usr/local/tcl74
set tclroot /opt/share/tcl
loadlibindex $tclroot/lib/ucodb/ucodb.tlib
# loadlibindex $tclroot/lib/ucodb/memes.tlib
loadlibindex /u/de/cvs/tcldb/memes/memes.tlib
#
#set efp [open /tmp/mbc.log w]
set dbpipe1 [sybOpen keck guest rsvp UCO-ASTRO]
# puts $efp "got dbpipe $dbpipe1"
#
global sybmsg
#set sybmsg(nullvalue) 0
#
set agntbl keck.dbo.Agents
set atytbl keck.dbo.AgentTypes
set pattbl keck.dbo.Mpaths
set memtbl keck.dbo.Memes
#
proc getAgids {pc} {

	global pattbl agntbl

	set sqlcmd "select sendr, rcvr, ctrlr from $pattbl where pcontext like '$pc%'"
	doSQL 1
	while {1} {
		set line [sybNext 1]
		if {$line == ""} {break}
		lassign $line s r c
		lappend agids $s
		lappend agids $r
		lappend agids $c
	}
	set agids [lrmdups $agids]
	return $agids
}
#
###########MAIN

set myname [file tail $argv0]
set mail 0
if {[llength $argv] < 1} {
	puts stderr "Usage:  aba|abl|abn targetName ?outFile?"
	exit 0
}
#
lassign $argv target outfile
#
set user [id user]
global env
set home ?
catch {set home $env(HOME)}
#       make a wild guess
if {$home == "?"} {
        set home /u/$user
}
if {[file isdirectory $home/cvs]} {
        set cvsdir $home/cvs 
} elseif {[file isdirectory $home/CVS]} {
	set cvsdir $home/CVS
} else {
	set cvsdir ""
}
#
# set report ffmt to c (context) or h (header)
set rts(aba) "author"
set rts(abn) "name"
set rts(abl) "lang"
set rts(abc) "acontext"
set rts(abp) "PC"
#
set rt $rts($myname)
#
if {$outfile == ""} {
	set outfile $target.$myname.out
	puts stderr "no output file name, default to $outfile"
}
if {[string first @ $outfile] > 0} {
        set uniq [getclock]
        set recip $outfile
        set outfile /tmp/$myname.$uniq
        set mail 1
} 
#
# what types of tuples are there?
#
set sqlcmd "select * from $atytbl"
doSQL 1
while {1} {
	set line [sybNext 1]
	if {$line == ""} {break}
	lassign $line at atd junk junk
	set atypes($at) $atd
}
#
# There's no disambiguation in here ya know.
#
set sqt $agntbl
case $rt in {
{name} {
set sqlcmd "select * from $agntbl where name = '$target'"
} 
{author} {
set sqlcmd "select * from $agntbl where author = '$target' order by aname"
}
{lang} {
set sqlcmd "select * from $agntbl where lang = '$target' order by aname"
} 
{acontext} {
set sqlcmd "select * from $agntbl where ref = '$target' order by aname"
} 
{PC} {
# This is all wrong, don't try this at home kids
set agids [getAgids $target]
set sqlcmd "select * from $agntbl where agentid in ([join $agids ,])"
} 
}
#
doSQL 1
#
set ofp [open $outfile w]
#
set cols [sybCols 1]
while {1} {
	set line [sybNext 1]
	if {$line == ""} {break}
	eval lassign \$line $cols
	set at $atypes($atype)
	set essay ""
	if {$docURI != ""} {
		set ul [split $docURI :]
		lassign $ul type path
		set i 0
	        foreach e [split $path /] {
       	          if {[string first CVS [string toupper $e]] >= 0} {
			incr i
       	                break
       	          }
       	          incr i
       	 	}
       	 	set fn [join [lrange [split $path /] $i end] /]
       	 	if {$fn == ""} {
       	         puts stderr "  $aname nonsense path : $srcURI"
       	         continue
       	 	}
#      	 	puts stderr "  $aname local path is $cvsdir/$fn"
	
       	 	if {![file exists $cvsdir/$fn]} {
       	         puts stderr "  NO SUCH FILE $cvsdir/$fn, skip it"
       	         continue
       	 	}

		set err [catch {set ess [open $cvsdir/$fn r]} res]
		if {$err} {
			puts stderr "can't open file $cvsdir/$fn"
			puts stderr "  $res"
		} else {
			set essay [read $ess]
			close $ess
		}
	}
	puts $ofp "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
	puts $ofp "\\modname\{$aname\}"
	puts $ofp "\\modfunc\{$adesc\}"
	puts $ofp "\\modlang\{$lang\}"
	puts $ofp "\\modtype\{$at\}"
	puts $ofp "\\modauth\{$author\}"
	regsub -all _ $rel - rel
	puts $ofp "\\modrev\{devel $rev release $rel\}"
	puts $ofp "\\srcref\{$srcURI\}"
	puts $ofp "$essay"
}
#------------------------------------------------------------------------
#
# THE END
puts $ofp "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
puts stderr "wrote results to $outfile"
#
#close $efp
close $ofp
if {$mail} {
        system "/usr/ucb/Mail -s Your_Agent_Report $recip < $outfile"
        system "/bin/rm $outfile"
}
#
sybClose 1
#
