#!/usr/local/bin/tcl
#
set libdir /u/de/tcl/w
#
echo "Usage: calling ListName tlibfile \[tlibfile tlibfile tlibfile...\]"
#
set lname [lindex $argv 0]
#
foreach library [lrange $argv 1 end] {
loadlibindex $libdir/$library
# make sure you load all the packages eh...  I bet this would be
# better done with scancontext...  too lazy to figure it out...
for_file line $libdir/$library {
	if {[crange [string trim $line] 0 0] != "#"} {continue}
	if {[lindex $line 0] == "#@package:"} {
		set cmd [lindex $line 2]
		catch {$cmd}
	}
}
}
#
set plist [lsort [info procs]]
#
foreach p $plist {

	echo "checking what is called by $p"

	foreach l [split [info body $p] \n] {
		set l [string trim $l]
		foreach cp $plist {
			set cl [expr [string length $cp] - 1]
			if {[crange $l 0 $cl] == $cp} {
#				echo "\t$p calls $cp"
				set ${p}($cp) 1
			}
			if {[regexp "\\\[$cp" $l]} {
#				echo "\t$p uses res of $cp"
				set ${p}($cp) 1
			}
		}
	}
}
#
# put output away safely... generic filename though...
#
set ofp [open $lname.callist w]
#
foreach p $plist {
	puts $ofp "proc $p :"
	if {[info var $p] != ""} {
	foreach cp [array names $p] {
		puts $ofp "\t $cp"
	}
	} else {
		puts $ofp ""
	}
	puts $ofp ""
}
close $ofp
#
