#!/opt/tcl/bin/sytcl
#
# script to read J random fits header file and try to figure out
#	what kind it is;  also to generated new memes if required
#	
#	First:  parse the sucker and get everything you can out
#		of it, into the good ol' collected array...
#	Second: try to figure out its likeness to any other bundle
#		we already know, or whether any subsection of it matches
#		a bundle we already know.
#	Third:  if it looks similar, note the differences and query
#		if it looks very different, ask if it's a new bundle.
#
# 1 arg: headerfile name 
#
set tclroot /opt/share/tcl
loadlibindex $tclroot/lib/ucodb/ucodb.tlib
loadlibindex $tclroot/lib/ucodb/memes.tlib
#
global sybmsg
set dbpipe1 [sybOpen keck guest rsvp UCO-ASTRO]
set maven "de"
#
#
# MAIN
# 2nd arg is which format:  H(uman-readable) or F(ITS)
#
set chat "puts stderr"
#
if {([llength $argv] > 4) || ([llength $argv] < 2)} {
	eval $chat \"Usage: fitsniff|fitsnort inFile inFileType\"
	eval $chat \"     : fitsdiff Header1name Header2name ?Context1 Context2?"
	eval $chat \"inFileType is H for human-readable, F for true FITS\"
	exit 0
}
set ctxt ""
lassign $argv hdrfile which ctxt c2
#
set mdb keck
set memtbl $mdb.dbo.Memes
set mebtbl $mdb.dbo.Mbundles
set mcotbl $mdb.dbo.Mcontexts
#
#
set logfile $hdrfile.snif
set err [catch {set ofp [open $logfile w]}]
if {$err} {
	set logfile /tmp/$logfile
}
set err [catch {set ofp [open $logfile w]}]
if {$err} {
	eval $chat \"If you can't write on /tmp you have much worse problems\"
	eval $chat \"to deal with than a strange FITS header.\"
	exit 1
}
#
set myname $argv0
if {$myname == "fitsnort"} {
	set mode N
	puts $ofp "SNORT of file $hdrfile by [id user] at [fmtclock [getclock]]\n"
	set chat "puts $ofp"
} elseif {$myname == "fitsniff"} {
	set mode M
	puts $ofp "SNIFF of file $hdrfile by [id user] at [fmtclock [getclock]]\n"
} else {
	set mode D
	puts $ofp "DIFF of headers $hdrfile $which by [id user] at [fmtclock [getclock]]\n"
	set hdr1 $hdrfile
	set hdr2 $which
	set c1 $ctxt
	set res [diffHdr $hdr1 $hdr2 $c1 $c2]
	foreach k [keylkeys res] {
	puts stderr "$k : [keylget res $k]"
	}
	exit 0
}
#
set err [catch {set ifp [open $hdrfile r]}]
if {$err} {
	eval $chat \"Come on now, I can't open $hdrfile.\"
	if {![file exists $hdrfile]} {
		eval $chat \"It doesn't exist.\"
		exit 1
	} else {
		eval $chat \"Do you have privs to read it?\"
		exit 1
	}
}
#
#
set hdrtext ""
while {1} {

	if {$which == "F"} {
		set err [catch {set line [read $ifp 80]}]
		append hdrtext "$line\n"
		if {$err} {break}
		if {[clength $line] < 80} {
			eval $chat \"Got short line $line\"
			break
		}
	} else {
		set bc [gets $ifp line]
		append hdrtext "$line\n"
		if {$bc < 0} {break}
	}
	if {[string trim $line] == ""} {continue}

}

set res [assessHdr $hdrtext]
foreach k [keylkeys res] {
	puts stderr "$k : [keylget res $k]"
}
set mid [keylget res mid]
set size [byteSize $mid]
puts stderr "SIZE: $size"
chkUnknown
foreach e [lsort [array names Unknown]] {
	puts stderr "$e:"
	foreach k [keylkeys Unknown($e)] {
		puts stderr "  $k : [keylget Unknown($e) $k]"
	}
}
clearGlobals
#
if {$myname == "fitsniff"} {
	eval $chat \"Don't forget to read $logfile for more information.\"
	mkNHmanual
} else {
	
}
#
#
close $ofp
#
