#!/opt/tcl803/bin/sytcl
#
# package require Tclx
# package require Sybtcl
#
loadlibindex /opt/share/tcl/lib/ucodb/ucodb.tlib
set server KSUMMIT
set err [catch {set dbpipe1 [sybOpen metabase zorro SecretPassword $server]} res]
if $err {
        puts stderr "OUCH can't get sybase pipe"
        puts stderr "ERR  $res"
        exit 1
}

# typical diff output
#
# Index: Mask.dat
# ===================================================================
# RCS file: /home/cvssci/metabase/Mask.dat,v
# retrieving revision 1.23
# diff -r1.23 Mask.dat
# 1,2d0
# < 1926  GOH_TV                                                                          Apr  1 2002 12:00AM             0
# < 1927  GOH_X                                                                           Apr  1 2002 12:00AM             0
# 1028a1027,1030
# > 741   ndwfs13.                                                                1183            Oct 29 2003  2:20PM             0                       JP      0       0       0.0
# > 742   ndwfs14.                                                                1184            Oct 29 2003  2:20PM             0                       JQ      0       0       0.0
# > 744   ndwfs15.                                                                1185            Oct 29 2003  2:20PM             0                       JR      0       0       0.0
# > 740   q1013r15                                                                1190            Oct 29 2003  2:20PM             0                       JW      0       0       0.0
# 
# the first set of lines are in the cvs repos but NOT in the local file.  that's what we want.
# the last set of lines are in the extracted local file but NOT in the cvs repos
#
# get file

set infile [lindex $argv 0]
puts stderr "\n***---------DIFF2SQL compare WAIAHA w/CVS----------***"
puts stderr "***---------[clock format [clock seconds]]----------***"
#
set i 0
set cols ""
for_file line $infile {
	if {[string first "RCS file" $line] == 0} {
		set rfn [lindex $line 2]
		set tbl [file rootname [file tail $rfn]]
		set fqt metabase.dbo.$tbl
		set sqt $fqt
		set sqlcmd "set rowcount 1"
		doSQL 1
		set sqlcmd "select * from $fqt"
		doSQL 1
		set cols [sybCols 1]
		set sqlcmd "set rowcount 0"
		doSQL 1
		set kf [lindex $cols 0]
		continue
	}
	if {[cindex $line 0] != ">"} {continue}
	set line [crange $line 2 end]
	set cs $cols
	set dl [split $line \t]
	puts stderr "DL : $dl"
	foreach v $dl {
		set c [lvarpop cs]
		keylset data($i) $c $v
	}
	incr i
}
#
#  did we get any new data?
if {![info exists data]} {
#  nope.
	puts stderr "No new data here on waiaha.  No Tcl.  STOP."
	set tbl [file rootname [file tail $infile]]
	unlink Notify.$tbl.tcl
	exec touch Notify.$tbl.tcl
	exit 0
}
#
set outfile Notify.$tbl.tcl
set ofp [open $outfile w]
#
set Warnings ""
set c0 [lindex $cols 0]
set sqt $fqt
foreach i [lsort -integer [array names data]] {

	puts stderr "Diff rec:  cvs contains line different from local extract!"

	if {$tbl == "Mask"} {

	foreach k [keylkeys data($i)] {
		set $k [keylget data($i) $k]
	}

	set strings [list shiptoDate MillDate MillQual millseq guiname]
	foreach f $strings {
		if {([set $f] == "NULL") || ([set $f] == "")} { set $f "" ; set val($f) NULL} else {set val($f) "'[set $f]'"}
	}
	foreach f $cols {
	if {![lcontain $strings $f]} {
		if {[set $f] == ""} { set $f 0 ; set val($f) NULL} else {set val($f) "[set $f]" }
	}
	}
	set vl ""
#	so far, masks milled here are MillId 1.
#	the MillMasks code should really do this... this is experimental here.
	set val(MillId) 1
	foreach c $cols {
		lappend vl $val($c)
	}
#	set sqlcmd "insert into $fqt values([set $kf],'$guiname',$shiptoPid,'$shiptoDate',$ToolDiam,$ToolAngl,$MillTemp,$NCagent,$MillId,$BluId,$millbyPId,'$MillDate','$MillQual',$maskExp,$uid,'$stamp','$millseq',$status,$shipid,$BMF)"
	set sqlcmd "insert into $fqt values([join $vl ,])"
	puts $ofp "set Change($fqt,[set $kf]) \"$sqlcmd\""
	puts stderr "SQL: $sqlcmd"

#	doSQL 1


	} else {

#	if rec exists it is an update and should be a MaskBlu record. only status should change
	eval lassign \$line $cols

	set strings [list shiptoDate MillDate MillQual millseq BluName BluCreat BluDate Date_Use guiname loc stamp]

	foreach f $strings {
		if {[info exists $f]} {
		if {[set $f] == "NULL"} { set $f "" }
		}
	}
	foreach f $cols {
	if {![lcontain $strings $f]} {
#		puts stderr "Is numeric $f ''?  if so, set $f 0"
		if {[set $f] == ""} { set $f 0 }
		if {[keylget data($i) $f] == ""} {keylset data($i) $f 0}
	}
	}

	if {$tbl != "MaskBlu"} {
		lappend Warnings "WARNING got diff to existing record for table $tbl NOT MaskBlu"
		lappend Warnings "        not sure what to do about this!  Abort this change."
		lappend Warnings "	  new rec was $data($i)"
		continue
	}

	set ns [keylget data($i) status ]
	set kv [keylget data($i) $kf]
	set sqlcmd "update $tbl set status = $ns where $kf = $kv"
	puts stderr "SQL: $sqlcmd"
	puts $ofp "set Change($fqt,$kv) \"$sqlcmd\""
#	doSQL 1

	}

}
close $ofp
# exec cvs commit $outfile
puts stderr "[join $Warnings \n]"
#