#!/bin/sh
# the next line restarts using wish \
exec wish "$0" ${1+"$@"}

proc tkgnats_error {msg} {
    bell
    wm withdraw .
    tk_dialog .tkgnatsError "TkGnats Error" "$msg" "error" 0 "OK"
    exit
}

proc tkgnats_usage {{str ""}} {
    tkgnats_error "${str}usage: tkgnats \[-lib 'tkgnatslib'\] \[-ini 'inifile'\] \
            \[-servers 'serversfile'\]"
}

proc tkgnats_process_args {} {
    global TkGnats argc argv env

    set TkGnats(CurrentProgram) tkgnats
    
    if {$argc != 0} {
	if {$argc%2 != 0} {
	    tkgnats_usage
	}
	for {set x 0} {$x<$argc} {incr x 2} {
	    set opt [lindex $argv $x]
	    set val [lindex $argv [expr $x+1]]
	    switch -exact -- $opt -lib {
                set TkGnats(TKGNATSLIB) $val
            } -ini {
                set TkGnats(TKGNATSINI) $val
            } -servers {
                set TkGnats(TKGNATSSERVERS) $val
            } default {
		tkgnats_usage "Illegal option pair:\n'$opt $val'\n\n"
	    }
	}
    }
    
    foreach var {TKGNATSLIB TKGNATSINI TKGNATSSERVERS} {
        if {![info exists TkGnats($var)]} {
            if {[info exists env($var)]} {
                set TkGnats($var) $env($var)
            }
        }
    }
    
    if {[info exists TkGnats(TKGNATSINI)]} {
        if {[file readable $TkGnats(TKGNATSINI)]} {
            source  $TkGnats(TKGNATSINI)
        } {
            tkgnats_error "TkGnats INI file '$TkGnats(TKGNATSINI)' not readable"
        }
    }
    
    if {[info exists TkGnats(TKGNATSLIB)]} {
        set TkGnats(lib) $TkGnats(TKGNATSLIB)
    }

    if {![info exists TkGnats(lib)]} {
        set TkGnats(lib) [pwd]; ##TKGNATSLIB##
    }
    set TkGnats(TKGNATSLIB) $TkGnats(lib)

    if {[file readable $TkGnats(lib)/tkgnatsini]} {
        source         $TkGnats(lib)/tkgnatsini
    }

    if {![info exists TkGnats(TKGNATSSERVERS)]} {
        set TkGnats(TKGNATSSERVERS) $TkGnats(lib)/servers
    } {
        if {![file readable $TkGnats(TKGNATSSERVERS)]} {
            tkgnats_error "TkGnats servers file '$TkGnats(TKGNATSSERVERS)' not readable"
        }
    }
    
    foreach var {TKGNATSLIB TKGNATSINI TKGNATSSERVERS} {
        if {[info  exists  TkGnats($var)]} {
            set env($var) $TkGnats($var)
        }
    }
}

proc tkgnats_get_servers {} {
    global TkGnats
    catch {source $TkGnats(TKGNATSSERVERS)}
    if {![info exists TkGnats(Servers)]} {
        set TkGnats(Servers) {}
    }
    if {$TkGnats(Servers) == {}} {
        set TkGnats(Servers) [list "Create/Query Problem Reports" {} {} {} {} {}]
    }
    set n 0
    foreach s [split $TkGnats(Servers) \n] {
        if {[llength $s] < 2} {
            continue
        }
        if {[llength $s] != 6} {
            tkgnats_error "TkGnats servers file '$TkGnats(TKGNATSSERVERS)' has incorrect entries: $s\n\nFields: Title GNATS_SERVER GNATS_PORT GNATS_ADDR ServerDir GNATSDatabaseAlias"
        }
        set TkGnats(server,[format "%04d" $n]) $s
        incr n
    }
}

tkgnats_process_args

foreach f { tkpr_library.tcl tkprhelp.tcl } {
    source $TkGnats(lib)/$f
}

tkgnats_get_servers

frame  .top
pack   .top

# Add optional buttons to the top of the main menu
if {[info exists TkGnats(MainMenu)]} {
    eval $TkGnats(MainMenu)
}

foreach s [lsort [array names TkGnats server,*]] {
    set b s_[lindex [split $s ,] 1]
    button .top.$b -relief raised -text [lindex $TkGnats($s) 0] \
            -command "TkGnats_exec {$TkGnats(WISHPATH)} $TkGnats(lib)/tkquerypr.tcl \
            -server [list $TkGnats($s)] &; schedule_reap"
    pack .top.$b -side top -fill x
}

button .top.about -relief raised -text "About TkGnats" \
        -command {helpMsg TkGnats_Version}

button .top.quit  -relief raised -text "Quit" -command {destroy .}

pack [frame .top.space1] -side top -fill x -pady 5

if {[info exists TkGnats(SiteReadMeFile)]} {
    regsub -all " " $TkGnats(SiteReadMeTitle) "_" Title
    button .top.readme -relief raised -text "$TkGnats(SiteReadMeTitle)" \
            -command {show_help $Title "[file_get_text $TkGnats(SiteReadMeFile)]"}
    pack .top.readme -side top -fill x
}

if {[info exists TkGnats(TkGnatsDocFile)]} {
    regsub -all " " $TkGnats(TkGnatsDocTitle) "_" Title
    button .top.doc -relief raised -text "$TkGnats(TkGnatsDocTitle)" \
            -command {show_help $Title "[file_get_text $TkGnats(TkGnatsDocFile)]"}
    pack .top.doc -side top -fill x
}

if {[info exists TkGnats(InfoReader)]} {
    button .top.info -relief raised -text "GNATS Info" \
            -command "TkGnats_exec $TkGnats(InfoReader) &; schedule_reap"
    pack .top.info -side top -fill x
}

if {[info exists TkGnats(MailReader)]} {
    button .top.mail -relief raised -text "Mail System" \
            -command "TkGnats_exec $TkGnats(MailReader) &; schedule_reap"
    pack .top.mail -side top -fill x
}

pack [frame .top.space2]  -side top -fill x -pady 5

pack .top.about .top.quit -side top -fill x

wm title      . "TkGnats - Main Menu"
wm iconbitmap . @$TkGnats(lib)/tkgnats.xbm
wm iconname   . "$TkGnats(LogName)'s TkGnats"
