#!/bin/sh
# the next line restarts using wish -*- tcl -*- \
exec /bin/wish "$0" "$@"

package require Tnm 2.1

## A pcnfs printer browser based on scotty and wish.
##
## Copyright (c) 1995
##
## J. Schoenwaelder
## TU Braunschweig, Germany
## Institute for Operating Systems and Computer Networks
##
## Permission to use, copy, modify, and distribute this
## software and its documentation for any purpose and without
## fee is hereby granted, provided that this copyright
## notice appears in all copies.  The University of Braunschweig
## makes no representations about the suitability of this
## software for any purpose.  It is provided "as is" without
## express or implied warranty.

package require Tnm 2.1

proc NewView {hosts} {
    global counter default tk_strictMotif

    if {[catch {incr counter}]} { set counter 0 }
    set w ".top$counter"
    toplevel $w

    frame $w.menu -borderwidth 1 -relief raised -highlightthickness 0
    pack $w.menu -side top -fill x
    label $w.status -anchor w
    pack $w.status -side bottom -fill x
    text $w.text -height 5
    pack $w.text -side bottom -fill both -expand true

    menubutton $w.menu.file -text "File" -menu $w.menu.file.m
    menu $w.menu.file.m
    $w.menu.file.m add command -label "Close View" \
	    -accelerator "  Alt+W" \
	    -command "CloseView $w"
    bind $w <Alt-w>  "$w.menu.file.m invoke {Close View}"
    bind $w <Alt-W>  "$w.menu.file.m invoke {Close View}"
    bind $w <Meta-w> "$w.menu.file.m invoke {Close View}"
    bind $w <Meta-W> "$w.menu.file.m invoke {Close View}"
    pack $w.menu.file -side left

    frame $w.box
    pack $w.box -side top -fill x
    listbox $w.box.hosts -yscroll "$w.box.shosts set"
    scrollbar $w.box.shosts -command "$w.box.hosts yview"
    pack $w.box.hosts  -side left
    pack $w.box.shosts -side left -fill y
    listbox $w.box.printer -yscroll "$w.box.sprinter set"
    scrollbar $w.box.sprinter -command "$w.box.printer yview"
    pack $w.box.printer -side left
    pack $w.box.sprinter -side left -fill y

    listbox $w.box.misc -yscroll "$w.box.smisc set"
    scrollbar $w.box.smisc -command "$w.box.misc yview"
    pack $w.box.misc -side left -fill x -expand true
    pack $w.box.smisc -side left -fill y

    foreach h [lsort $hosts] {
        $w.box.hosts insert end $h
    }
    bind $w.box.hosts <Button-1> "after idle ShowPrinter $w"
}

proc CloseView {w} {
    if {[winfo children .] == "$w"} {
	destroy .
    } else {
	destroy $w
    }
}

proc ShowPrinter {w} {
    set host [$w.box.hosts get [lindex [$w.box.hosts curselection] 0]]
    wm title $w $host
    $w.box.printer delete 0 end
    $w.box.misc delete 0 end
    $w.text delete 0.0 end
    $w.status configure -text "$host: wait..."
    foreach j [job info] { $j destroy }
    update

    if {[catch {sunrpc pcnfs $host info status} info]} {
	$w.status configure -text "no information available: $info"
	return
    }
    $w.status configure -text $info
    foreach name [lsort [array names status]] {
	$w.box.misc insert end "$name = \"$status($name)\""
    }

    if {[catch {sunrpc pcnfs $host list} printer]} {
	$w.status configure -text "no printer information available: $printer"
	return
    }
    foreach p [lsort $printer] {
	$w.box.printer insert end $p
    }
    bind $w.box.printer <Button-1> "after idle StartShowStatus $w $host"
}

proc StartShowStatus {w host} {
    set printer [$w.box.printer get [lindex [$w.box.printer curselection] 0]]
    wm title $w "$printer@$host"
    $w.box.misc delete 0 end
    $w.text delete 0.0 end
    update
    foreach j [job info] { $j destroy }
    job create -command "ShowStatus $w $host $printer" -interval 10000
}

proc ShowStatus {w host printer} {
    $w.status configure -text "$printer@$host: wait..."
    update

    if {[catch {sunrpc pcnfs $host list config} msg]} {
	$w.status configure -text "no printer information available: $msg"
	return
    }

    $w.box.misc delete 0 end
    if {[info exists config($printer)]} {
	array set status $config($printer)
	foreach name [lsort [array names status]] {
	    $w.box.misc insert end "$name = \"$status($name)\""
	}
	unset status
    }

    if {[catch {sunrpc pcnfs $host status $printer status} msg]} {
	$w.status configure -text "no printer information available: $msg"
	return
    }
    foreach name [lsort [array names status]] {
	$w.box.misc insert end "$name = \"$status($name)\""
    }

    if {$status(queued) > 0} {
	if {[catch {sunrpc pcnfs $host queue $printer queue} msg]} {
	    $w.status configure -text "no queue information available: $msg"
	    return
	}
	$w.text delete 0.0 end
	$w.text insert end \
		"   job status    size user            system           file\n"
	foreach item [lsort -integer [array names queue]] {
	    array set job $queue($item)
	    $w.text insert end [format "%6s %6s %6s %-16s %-16s %s\n" \
		    $job(id) $job(status) $job(size) \
		    $job(user) $job(system) $job(file)]
	    unset job
	}
    }

    $w.status configure -text "$printer@$host: [clock format [clock seconds]]"
    update
}

wm withdraw .
option add *Text.font	fixed	startupFile

if {$argv == ""} {
    exit 0
}

NewView $argv
