#!/opt/tcl803/bin/tcl
#
set datdir /lopt/apache/cgi-bin/Tcl2K
# set datdir /net/osiris/lopt/apache/cgi-bin/Tcl2K
#
set htmdir /lopt/apache/u/de/Tcl2K
# set htmdir /usr/local/www/de/Tcl2K
set DataFile /lopt/apache/cgi-bin/Tcl2K/data/tclPapers.dat
#
package require Tclgdbm
#
#
proc getTok title {

        global DataFile

    set err [catch {set db [gdbm open $DataFile r]} res]

	if $err {
		puts stderr "ERROR opening db file!"
		puts stderr "      $res"
		return ""
	}

        set all ""
    catch {set all [gdbm fetch $db "submissions"]}
    set found 0
    foreach token $all {
        set t ""
        catch {set t [gdbm fetch $db "$token-title"]}
        if {[string first [crange $title 0 25] $t] == 0} {
            set found 1
            break
        }
        regsub -all "  " $t " " t
        if {[string first [crange $title 0 25] $t] == 0} {
            set found 1
            break
        }
    }


    if {$found} {
	
	set score [gdbm fetch $db $token-score]

    	gdbm close $db
        return [list $token $score]
    }
    gdbm close $db
    return ""
}
#
proc getReviews token {

	global DataFile ofp

   	set db [gdbm open $DataFile r]

	set found ""
        set revs ""
        catch {set revs [gdbm fetch $db "$token-reviews"]}

    	puts stderr "FOUND REVIEWS $revs for $token"

	set ri 1
	set st ""
	set wk ""
	set bot ""
	set adv ""
	foreach rev $revs {
        set revname [gdbm fetch $db "$rev-reviewer"]
#        puts $ofp "      One reviewer was $revname"
	set score [gdbm fetch $db "$rev-score"]
	set mult [gdbm fetch $db "$rev-multiplier"]
	set stren [gdbm fetch $db "$rev-strengths"]
	set weak [gdbm fetch $db "$rev-weaknesses"]
	set bottom [gdbm fetch $db "$rev-bottomline"]
	set advice [gdbm fetch $db "$rev-comments"]
	puts $ofp "Reviewer # $ri gave your paper a score of $score on a scale of 0 t0 4.0."
	puts $ofp "    The 'certainty factor' was $mult on a scale of 0 to 3.0."
	append st "\n$stren"
	append wk "\n$weak"
	append bot "\n$bottom"
	append adv "\n$advice"
	incr ri
    	}

    gdbm close $db

	puts $ofp "These are the combined comments of all reviewers."

	puts $ofp "\n\nSTRENGTHS OF THIS SUBMISSION:\n$st"

	puts $ofp "\n\nWEAKNESSES OF THIS SUBMISSION:\n$wk"

	puts $ofp "\n\nBOTTOM LINE:\n$bot"

	puts $ofp "\n\nADVICE/SUGGESTIONS TO THE AUTHOR:\n$adv"

}
#
# source $datdir/tclPaperslib.tcl
#
set file accept.papers
#
for_file line $file {

	if {[string trim $line] == ""} {continue}

	if {[string first @ $line] > 0} {
		set wi 0
		foreach w $line {
			if {[cindex $w 0] != "<"} {
				append name "$w "
			} else {
				set email [string trim [lindex $line $wi] <>]
				set authors($email) $name
				set name ""
			}
			incr wi
		}
	} else {
		if {[cindex $line 0] == "\t"} {
			set t [string trim $line]
			lappend title($email) $t
		}
	}

	

}

# set boiler [read_file hints.txt]

foreach e [array names authors] {

		set ofp [open hints/$e w]
		set n $authors($e)


		puts $ofp "Dear $n\n"
		foreach t $title($e) {
#			puts $ofp "    $t"
			puts $ofp "\n------------------------------------------------------------------------\n"
			puts $ofp "Your paper \n\t$t\nwas reviewed by several readers.\n\nWe feel it will be helpful for you to consider their opinions\nas you prepare your final draft and oral presentation.\nIf you wish for additional advice or help, send mail to\nde@ucolick.org and we will put you in touch with a reviewer \nwho can offer further suggestions."
			set tok [getTok $t]
			if {$tok == ""} {
				puts stderr "OOPS can't find paper"
				puts stderr "$t"
				continue
			} else {
				lassign $tok tok score
				puts stderr "Reviews for paper $tok, scored $score"
				puts stderr "$t"
				puts $ofp "\nOVERALL SCORE $score\n"
			}
			getReviews $tok
		}
			
		puts $ofp "\n------------------------------------------------------------------------\n"
		puts $ofp "We look forward to seeing you at Tcl2K!\n\nDe Clarke\nTom Poindexter\ncoChairs, Tcl2K\n\n"
		close $ofp
}
		
