
proc cryWolf {newmsgs} {

	global mfname mfdir msgtbl 
	global Banner RemindDay Mavens Facility Mail LogFiles AllsWell

#	globals
#	mfname 	   : name of mailfile
#	mfdir      : writable dir in which to build it
#	Mavens     : list of recipients
#	msgtbl     : database table of message sending info
#	RemindDay  : day of week (Mon Tue) on which to send persistent messages
#	Facility   : facility code of message (arbitrary string)
#	Mail	   : explicit path of correct mail sender
# 	LogFiles   : paths of any relevant logfiles to read
# 	Banner     : banner string
#	AllsWell   : flag;  if 1, send an All's Well if no msgs --
#	 	   : otherwise, no news is good news.

	if {![info exists AllsWell]} {set AllsWell 0}

	set mailmsg ""
	set today [clock format [clock seconds] -format "%m/%d/%Y"]
	set weekday [clock format [clock seconds] -format %a]
#
#	We have a database table called messages, whose fields are
#	msgtxt, msgdate, sendct, lastsend, facility...
#	the pkey is the msgtext, so be careful what you log.
#
#	We have list of lines of text incoming, and
#	we are building a mail message body to be sent to Mavens.
#	We don't have to worry about preserving history (except
#	for regulating message delivery) since the logfiles keep all
#	occurrences of every message.
#
#	If a message in messages was NOT found today, it is resolved
#		and must vanish from the messages list, with a
#		confirming message in the output.
#	If a message in messages IS found today, we need to figure out
#		its history and mail or not mail it....
#		If the sendct is < 3 then mail it.
#		If the sendct is >= 3 and today is "remind day", send the message.
#		Flag it if it's been sent more than 6 times already.
#		If a message found today is NOT in messages, then it must
#			be added and sent for the first time.
#
#
	set oldmsgs ""
	set sqlcmd "select msgtxt from $msgtbl where facility = '$Facility'"
	set sqt $msgtbl
	doSQL 1
	while {1} {
		set mt [sybNext 1]
		if {$mt == ""} {break}
		lappend oldmsgs [stringFix2 $mt out]
	}
	
#
#	First weed out the resolved errors
#

	lassign [intersect3 $oldmsgs $newmsgs] killem sameold addem

	puts stderr "oldmsgs : $oldmsgs"
	puts stderr "newmsgs : $newmsgs"
	puts stderr "sameold : $sameold"
	puts stderr "addem : $addem"
	puts stderr "killem : $killem"

	set resolved ""
	foreach k $killem {
		lappend resolved "RESOLVED $today : $k"
		set k [stringFix2 $k in]
		set sqlcmd "delete from $msgtbl where msgtxt = '$k'"
		doSQL 1
	}
#
# 	now insert the new errors
#

	foreach a $addem {
		set sqlcmd "insert into $msgtbl values ('$a','$today',0,NULL,'$Facility')"
		doSQL 1
	}
#
#	now process the msg table to add lines to our mail message
# 
#

	set sqlcmd "select * from $msgtbl where sendct < 3 and facility = '$Facility' order by sendct, msgtxt"
	doSQL 1
	set cols [sybCols 1]
	while {1} {
		set line [sybNext 1]
		if {$line == ""} {break}
		eval lassign \$line $cols
		if {$sendct == 0} {
			set flag "  NEW     :"
		} else {
			set flag "  RECENT  :"
		}
		lappend mailmsg  "$flag [crange $msgdate 0 11] $msgtxt"	
	}
#
	if {$weekday == "$RemindDay"} {
	set sqlcmd "select * from $msgtbl where sendct >= 3 and facility = '$Facility' order by sendct, msgtxt"
	doSQL 1
	set cols [sybCols 1]
	while {1} {
		set line [sybNext 1]
                if {$line == ""} {break}
                eval lassign \$line $cols
		set flag "  "
		if {$sendct > 6} {
			set flag **
		}
		lappend mailmsg "$flag RECURRING: [crange $msgdate 0 11] $msgtxt"
	}
	}
#	
#
#	Old messages
#
	if {$weekday == "$RemindDay"} {
	set sqlcmd "update $msgtbl set sendct = sendct+1, lastsend = '$today' where sendct >= 3 and facility = '$Facility'"
	doSQL 1
	}
#	
#	Recent messages
#
	set sqlcmd "update $msgtbl set sendct = sendct+1, lastsend = '$today' where sendct < 3 and facility = '$Facility'"
	doSQL 1

## now put the message together and send it

	if {$mailmsg != ""} {
	lappend mailmsg "----------\nFor more info check the log files : $LogFiles"
	}

	if {$AllsWell} {
		if {$mailmsg == ""} {
			lappend mailmsg ""
			lappend mailmsg "All is Well"
			lappend mailmsg ""
		}
	}

	if {$mailmsg != ""} {
	set ofp [open $mfdir/$mfname w]
	puts $ofp "\n----------------\n$Banner\n----------------\n"
	foreach l $mailmsg {
		puts $ofp "$l"
	}
	puts $ofp "---------------------------------------------------------"
	foreach l $resolved {
		puts $ofp "$l"
	}
	close $ofp

	system "$Mail -s Messages_from_$Facility $Mavens < $mfdir/$mfname"
	}

}
