#!/usr/local/bin/wish -file

#
# $Id
#

set TkGnats(lib) ./; ##TKGNATSLIB##
foreach f { tkpr_library.t } {
    source $TkGnats(lib)/$f
}

case $TkGnats(LogName) {root} {
    Msg "You cannot convert problem reports as root. Use your own login"
    Exit 1
}

exec sh -c "rm -f /usr/tmp/cvtpr/*"

# The default release for this host.
set DEFAULT_RELEASE "_release_"
set USER_GNATS_SITE $TkGnats(GNATS_SITE); ## may get from elsewhere later..
set fin [open [lindex $argv 0]] 

#########

set HOSTNAME [hostname]
set FROM [set TkGnats(LogName)]@$HOSTNAME
set REPLY_TO [set TkGnats(LogName)]@$HOSTNAME
set ORGANIZATION wxsystems
# Find out some information.
set SYSTEM "_system_"
set ARCH "_arch_"
set MACHINE "_machine_"
if { [info exists env(TMPDIR)] } {
    set TMPDIR $env(TMPDIR)
} else {
    set TMPDIR /tmp
}

set stamp [exec whoami][exec date +%j:%T]
set TEMP $TMPDIR/p$stamp
set BAD $TMPDIR/pbad$stamp
set REF $TMPDIR/pf$stamp
set COMMAND tksendpr
set USAGE "Usage: $COMMAND"
set REMOVE ""
set VERSION 3.00

## TBD
## --- may want to check SUBMITTER validity here ...
## TBD

set CategoryList [get_categories]
if {$CategoryList == ""} {
  Msg "$COMMAND: the categories list was empty!"
  Exit 1
}

# Assemble the problem report in gnats format from the fields in
# Before assembly some of the widgets are checked for validity.
proc send_report {oldid} {
    global stamp TkGnats
    # check all the fields first...
    foreach v {
	Confidential Severity Priority Class Category
    } {
	global $v
	case [set $v] "" {
	    Msg "An option from '$v' must be selected"
	    return -1
	}
    }
    foreach t {
	Originator Synopsis Description How-To-Repeat 
    } {
	global $t
	set x [set $t]
	set x [string trim $x " \n\t\f!:~-#_?"]
	case $x "" {
	    Msg "Field '$t' is not complete. Type something descriptive in."
	    return -1
	}
    }
    foreach t {
	Originator Release Synopsis Environment
    } {
	catch {global $t}
	set x [set $t]
	if {[string first "|" $x] != -1} {
	    Msg " '|' is an illegal character for the '$t' field"
	    return -1
	}
    }

    # ok, now send off the report

    set tmpfilename /tmp/tksendpr.$stamp
    set fout [open $tmpfilename "w"]
    global FROM REPLY_TO ORGANIZATION VERSION
    puts $fout "To: $TkGnats(GNATS_ADDR)"
    puts $fout "Subject: "
    puts $fout "From: $Originator"
    puts $fout "Reply-To: $Originator"
    puts $fout "X-send-pr-version: $VERSION"
    puts $fout ""
    #### End of std mail headers

    puts $fout ">Submitter-Id: $TkGnats(Submitter-Id)"
    puts $fout ">Organization: \n$ORGANIZATION"
    foreach v {
	Confidential Severity Priority Category Class
	Synopsis Originator Release 
	Environment Description How-To-Repeat 
    } {
	puts $fout ">${v}:	[set $v]"
    }

    flush $fout
    close $fout
    send_off $tmpfilename $oldid

    return 0
}

# mail the assembled problem report file to the gnats system
set counter 0
proc send_off {fname oldid} {
    global counter TkGnats
    incr counter
    mkdir -p /usr/tmp/cvtpr
    set newname "/usr/tmp/cvtpr/bug.$oldid.$counter"
    Msg "Sending Report to $TkGnats(GNATS_ADDR).." 0
    fmt -s -85 $fname > $newname
    puts stderr [cat $newname]
    gets stdin in

    # exec $TkGnats(Mailer) $TkGnats(GNATS_ADDR) < $fname
    # exec rm $fname
    Msg "Done" 0
    return ""
}

while {[gets $fin ln] >=0} {
    puts stderr <$ln>
    puts stderr "\n\n"
    ## static fields
    set Class sw-bug
    set Confidential no
    set How-To-Repeat "not available"
    set Environment "milmet sunOS 4.1.3"
    ##
    set l [split $ln |]
    set oldcat [set Category [lindex $l 0]]

    case $ln "#*" {
	continue
    }

# # # field 1 is junked 
    set x [lindex $l 2]
    case $x {1} {
	set Priority high
	set Severity critical
    } {2} {
	set Priority high
	set Severity critical
    } {3} {
	set Priority medium 
	set Severity serious
    } {4} {
	set Priority low 
	set Severity non-critical
    } {5} {
	set Priority low 
	set Severity non-critical
	set Class change-request
    } {6} {
	set Category cots
	set Priority high
	set Severity critical
    } default {
	set Priority high
	set Severity critical
    }

    set oldid [lindex $l 3]
    set Description "\n$oldcat/$oldid\n [lindex $l 4]"
    set Synopsis "~$oldid [string range [lindex $l 4] 0 50]"
    set Release [lindex $l 7]
    set Originator [lindex $l 8]
    if {"$Originator" == ""} {
	set Originator "markm"
    }

    while {1} {
	if {[lsearch $CategoryList $Category]<0} {

	    if {[info exists categorymaps($Category)]} {
		set Category $categorymaps($Category)
		break;
	    }

	    puts stderr "**** bad category $Category: ****"
	    puts stderr "\ncategories are \n\t$CategoryList\n"
	    puts stderr "enter a new category: " nonewline
	    gets stdin in
	    puts stderr ">>> $Category"

	    if {[lsearch $CategoryList $in]>=0} {
		set categorymaps($Category) $in
	    }

	} else {
	    break
	}
    }
    send_report $oldid
}
