#!/usr/bin/wish -f

# Printer config tool
# (C) Copyright 1994 by Red Hat Software
# (C) Copyright 1995 by Michael Callahan
# Version 3.0 additions made by Michael Fulbright (Red Hat Software)
# Smb printing support supplied by Miquel de Icaza
# NCP printing support supplied by Fernando Lozano, BL Informtica Ltda.
# uniprint support supplied by Osamu Aoki, Cupertino, USA
# Direct to port support supplied by Josh Buysse, University of Minnesota

if {[catch {set env(CONTROL_PANEL_LIB_DIR)}] != 0} {
    set env(CONTROL_PANEL_LIB_DIR) /usr/local/share/printtool
}
if {[catch {source $env(CONTROL_PANEL_LIB_DIR)/dialog.tcl}] != 0} {
    puts "Couldn't load dialog.tcl"
    puts "Start from control-panel or set environment variable"
    puts "CONTROL_PANEL_LIB_DIR to the control-panel library dir."
    puts "(normally this is /usr/share/printtool)"
    exit 0
}
if {[catch {source $env(CONTROL_PANEL_LIB_DIR)/bindings.tcl}] != 0} {
    puts "Couldn't load bindings.tcl"
    puts "Start from control-panel or set environment variable"
    puts "CONTROL_PANEL_LIB_DIR to the control-panel library dir."
    puts "(normally this is /usr/share/printtool)"
    exit 0
}

set warn_samba 1
set warn_ncpfs 1

catch {source $env(CONTROL_PANEL_LIB_DIR)/printtool.warn}

#########################################################
## @@ Random Data
set version "4.5"
set printer_count 0
set delete_index ""
set trigger 0
set trigger_2 0
set trigger_help 0
set tmp_auto_pseof ""
set tmp_auto_texteof ""
set tmp_auto_sendeof ""
set tmp_auto_crlf_trans ""
set tmp_auto_ascps_trans ""
set tmp_auto_rev_pages ""
set tmp_auto_nup "1"
set gs_installed_drivers ""
set locale ""
set current_printer_type ""
set autodetecting 0

#
# msf - printer_list and resolution_list have been superceded by the
#       printer database entries.
#
# paper_size_list will still be used.
#
set printer_list "PostScript laserjet ljet2p ljet3 ljet4 ljetplus epson epsonc ibmpro jetp3852 deskjet djet500 djet500c bj10e bj200 cdeskjet cdjcolor cdjmono cdj500 cdj550"
set resolution_list "300x300 360x360 400x400 600x600 800x800"
set paper_size_list "letter legal ledger a3 a4 b5 b4"

#
# msf - we will read in a printer database and the following array variables
#       are defined
#  
#
#  printerdb_entry() - array of names of entries in printer database, not
#                      to be confused with the printcap file.
#                      This string is used internally to refer to drivers.
#                      Index from 0 to (# of printerdb entries) - 1
#  printerdb_descr() - array of one-line descriptions of driver.
#                      This string will be used in listbox of drivers
#                      from which the user choses a driver.
#                      Probably indexed by printerdb_entry name for driver.
#  printerdb_descr2entry() - takes a description as index, returns matching
#                            entry name. Use to back-reference from
#                            sorted description list to entry list.
#  printerdb_about() - array of arbitrarily long strings which give more
#                      details information about the driver.
#                      Probably indexed by printerdb_entry name for driver.
#  printerdb_GSdriver() - array of one-line descriptions of driver.
#                      This is the GS driver to use in the filter.
#                      Probably indexed by printerdb_entry name for driver.
#  printerdb_res()   - array of list of lists.
#                      Each list entry is format {XDPI} {XYPI} {Comment}.
#                      Probably indexed by printerdb_entry name for driver.
#  printerdb_color() - array of list of lists.
#                      Each list entry is format {Color depth} {Comment}.
#                      Probably indexed by printerdb_entry name for driver.
#                      For uniprint style command line, each list entry 
#                      is format {upp_file} {upp_comment}.
#                      upp_file is corresponding filename excluding .upp, and
#                      upp_comment is obtained from first line of upp_file
#                      
#
set printerdb_entry(0) ""
set printerdb_descr(0) ""
set printerdb_about(0) ""
set printerdb_GSDriver(0) ""
set printerdb_color(0) ""
set printerdb_res(0) ""
set printerdb_descr2entry(0) ""

#
# auto_printer is the GS driver to use for selected filter
# rest are self explanatory
#
# msf - Added auto_printerdb_entry to store printerdb entryname
#
set auto_printer ""
set auto_printerdb_entry ""
set auto_resolution ""
set auto_paper_size ""
set auto_flag "*auto*"
set auto_pseof ""
set auto_texteof ""
set auto_sendeof ""
set auto_eof ""
set auto_crlf_trans ""
set auto_ascps_trans ""
set auto_rev_pages ""
set auto_color "Default"
set auto_nup "1"
set auto_rtlft_margin "18"
set auto_topbot_margin "18"
set auto_GSextra ""

set auto_direct_printer_ip ""
set auto_direct_port ""

#
# new filter related locations and filenames
#

set filtersrcdir "/usr/local/share/printfilters"
set libexecdir "/usr/local/libexec"
set sbindir "/usr/local/sbin"

#
# The location of PPD files
#

set ppddir "/usr/share/postscript/ppd/"

#set filter_template "$env(CONTROL_PANEL_LIB_DIR)/filter-template"

#########################################################
## @@ User interface

frame .menuf -relief raised -borderwidth 2
menubutton .menuf.fsm -text "PrintTool" -menu .menuf.fsm.menu
menu .menuf.fsm.menu
.menuf.fsm.menu add command -label "Reload" -command menu_reload
.menuf.fsm.menu add separator
.menuf.fsm.menu add command -label "About" -command menu_about
.menuf.fsm.menu add separator
.menuf.fsm.menu add command -label "Quit" -command menu_quit
menubutton .menuf.nfs -text "lpd" -menu .menuf.nfs.menu
menu .menuf.nfs.menu
.menuf.nfs.menu add command -label "Restart lpd" -command menu_restart_lpd
menubutton .menuf.tests -text "Tests" -menu .menuf.tests.menu
menu .menuf.tests.menu
.menuf.tests.menu add command -label "Print ASCII test page" -command print_ascii_testpage
.menuf.tests.menu add command -label "Print Postscript test page" -command print_ps_testpage
.menuf.tests.menu add command -label "Print ASCII directly to port" -command print_ascii_direct_testpage
menubutton .menuf.help -text "Help" -menu .menuf.help.menu
menu .menuf.help.menu
.menuf.help.menu add command -label "General Help" -command menu_genhelp
.menuf.help.menu add command -label "Troubleshooting" -command menu_trouble


pack .menuf.fsm .menuf.nfs .menuf.tests .menuf.help -side left -in .menuf
tk_menuBar .menuf .menuf.fsm .menuf.nfs .menus.tests .menus.help

label .header -font fixed -text "Printer Queues in /etc/printcap"

frame .main -relief flat -borderwidth 2
listbox .list -font fixed -yscrollcommand ".sb set" -setgrid 1 -exportselection 0 -selectmode single
scrollbar .sb -command ".list yview"
pack .list -side left -expand true -fill both -in .main
pack .sb -side left -fill y -in .main

frame .buttons
button .edit -text "Edit" -width 10 -command button_edit
button .add -text "Add" -width 10 -command button_add
button .delete -text "Delete" -width 10 -command button_delete

# MLP
button .autodetect -text "AutoDetect" -width 10 -command button_autodetect
# MLP
pack .edit .add .delete .autodetect -side left -expand true -ipady 1 \
	-padx .5c -in .buttons


pack .menuf -side top -fill x
# The following large padx is so that the header
# lines up properly with the stuff in the listbox
pack .header -side top
pack .main -side top -expand true -fill both -padx 4
pack .buttons -side top -fill x -padx 4 -pady 4

wm title . "Red Hat Linux Print System Manager"
update
scan [wm geometry .] "%d%*c%d" xmin ymin
wm minsize . $xmin $ymin

bind .list <Double-Button-1> "
    .list selection clear 0 end
    .list selection set \[.list nearest %y\]
    button_edit
"

## End of main user interface
###########################################################
## @@ Random Functions

proc {get_locale} {} {
#----------------------------------------------------
# Get the two-character locale code.
#----------------------------------------------------

    global locale

    #------------------------------------------------
    # Open pipe to locale command.
    #------------------------------------------------
    if [catch {open "| locale "} input] {
	# If the open failed, then something is wrong. 
	# Let's assume English locale, and continue.
	set locale "en"
	return
    }

    while {[gets $input line] >= 0} {
	set parts [split $line "="]
	if { [lindex $parts 0] == "LANG" } {
	    # Locale command doesn't give us a standard LANG arguement
	    # so we go with english.  adb 7/13/01
	    if { [lindex $parts 1] == "POSIX" || [lindex $parts 1] == "C" } {
		set locale "en"
		return
	    }
	    set lang [string range [lindex $parts 1] 0 1]
	}
	if { [lindex $parts 0] == "LC_ALL" } {
	    set lc_all [string range [lindex $parts 1] 0 1]
	}
    }

    if { [info exists lang] } {
	set locale $lang
    } elseif { [info exists lc_all] } {
	set locale $lc_all
    } else {
	set locale "en"
    }

}

proc load_ppdxref_file {} {
#----------------------------------------------------
# Load the PPD cross-reference file into an associative
# array.
# $xref(printer-name) = ppd-filename
#----------------------------------------------------
    global filtersrcdir
    global xref

    if {[catch {set fd [open "$filtersrcdir/ppd_xref.rdb" r]}] == 1} {
	# there is no printerdb
	puts "Error cannot find $filtersrcdir/ppd_xref.rdb!"
	return
    }

    # Skip initial comments:
    while {[gets $fd s] != "-1"} {
	# strip spaces at the front
	set cp [string trimleft $s]

	# if not a comment line then break?
	if {[regexp "^\#" $cp] != 1} {
	    break
	}
    }

    set fnames $s

    # read field types
    gets $fd ftypes

    # read first data line
    gets $fd s

    set i 0
    while {[gets $fd s] != "-1"}  {
	
	set pfields [split $s "\t"]

	set xref([lindex $pfields 1]) [lindex $pfields 0]

	incr i
    }
    set xref_count $i

    close $fd
}

proc reload {} {
    global print_count printer_comments printer_entry
    global printer_names printer_type printer_info
    global version

    set print_count 0
    if {[catch {set fd [open "/etc/printcap" r]}] == 1} {
	# there is no /etc/printcap
	puts "There is no /etc/printcap. Creating one now..."
	if {[catch {set fd [open "/etc/printcap" w]}] == 1} {
	    puts "Cannot create /etc/printcap, exiting"
	    exit 1
	}
	puts $fd "#"
	puts $fd "# This printcap is being created with printtool v.$version"
	puts $fd "# Any changes made here manually will be lost if printtool"
	puts $fd "# is run later on."
	puts $fd "# The presence of this header means that no printcap"
	puts $fd "# existed when printtool was run."
        puts $fd "#"
	close $fd
	if {[catch {set fd [open "/etc/printcap" r]}] == 1} {
	    puts "Cannot create /etc/printcap, exiting"
	    exit 1
	}
    }

    # We read the /etc/printcap file quickly into the arrays
    # printer_comments() and printer_entry().  printer_comments($i)
    # contains the comments which precede a given entry.  The special
    # line "##PRINTTOOL## <type> <info>" allows the printtool to
    # store extra information about the printer; <type> is one of the
    # allowed printer types, and <info> is a TCL list, saved in the
    # array printer_info.  

    set i 0
    set printer_comments(0) ""
    set printer_entry(0) ""
    set printer_type(0) ""
    set printer_info(0) ""
    while {[gets $fd s] != "-1"} {
	# Recognize comments and whitespace lines
	set cp [lindex $s 0]p
	if {[regexp "^\#" $cp] == 1 || [regexp {^[ 	]*$} $s] == 1} {
	    # Is it a special printtool line?
	    if {[regexp "^\\#\#PRINTTOOL3\#\#" $cp] == 1} {
		set printer_type($i) [lindex $s 1]
		set printer_info($i) [lrange $s 2 [expr [llength $s]-1]]
	    } elseif {[regexp "^\\#\#PRINTTOOL\#\#" $cp] == 1} {
		set printer_type($i) [lindex $s 1]
		set printer_info($i) [lrange $s 2 [expr [llength $s]-1]]
	    } else {
		set printer_comments($i) "$printer_comments($i)$s\n"
	    }
	    continue
	}
	
	# If we get to here we have a printer definition
	# Read it in--keep reading until we reach a line that doesn't
	# end in the continuation mark, or EOF
	set printer_entry($i) "$s\n"
	while {[regexp "\\\\$" $s] && [gets $fd s] != "-1"} {
	    set printer_entry($i) "$printer_entry($i)$s\n"
	}
	incr print_count
	incr i
	set printer_comments($i) ""
	set printer_entry($i) ""
	set printer_type($i) ""
	set printer_info($i) ""
    }

    close $fd

    # NB: the printer_entry and printer_comments arrays have different
    # array bounds.  printer_entry is defined from 0 to $print_count-1.
    # printer_comments is defined from 0 to $print_count.
    # printer_comments($print_count) consists of comments following
    # the last printer.

    # Now go through to examine each entry
    for {set i 0} {$i < $print_count} {incr i} {
	# Canonicalize entry
	set e $printer_entry($i)
	regsub -all "\\\\\n\[ \t\]*" $e " " e
	regsub -all ":\[ \t\]*:" $e ":" e
	regsub "\[ \t\]*\n\$" $e "" e
	set printer_entry($i) $e

	# First field gives the printer names
	regexp {^([^:]*):} $printer_entry($i) bogus printer_names($i)

	# Fix old mx% bug -- should be mx#, but to avoid duplicate
	# entries we just kill the old mx% specifications
	regsub -all {:(..)%([0-9]*):} $printer_entry($i) {:} printer_entry($i)

	# Fix printer_type
	if {$printer_type($i) == ""} {
	    set printer_type($i) UNKNOWN
	} else {
	    # Make sure this type is known
	    if {[info procs "$printer_type($i)_summaryline"] == ""} {
		set printer_type($i) UNKNOWN
		set printer_info($i) {}
	    }
	}
#
# msf - following check will no longer work, since we are not using
#       the same filter template
#
#       Instead we should have the magic filter script have a special
#       header at its start which we can try and recover this information
#       from.

#
#	if {$printer_type($i) == "UNKNOWN"} {
#	    set res [guess_printer_type $i]
#	    if {[llength $res] > 0} {
#		set printer_type($i) [lindex $res 0]
#		set printer_info($i) [lrange $res 1 [expr [llength $res]-1]]
#	    }
#	}
    }
}

proc guess_printer_type {i} {
    global print_count printer_comments printer_entry
    global printer_names printer_type printer_info

#
# msf - this shouldnt be called currently, needs to be rewritten
#
    puts "ERROR - guess_printer_type called!!"
    exit 1

    if {[printcap_field $i "rm=" ""] != "" && \
	    [printcap_field $i "rp=" ""] != ""} {
	return {REMOTE}
    }

    set spool_dir [printcap_field $i "sd=" ""]
    set i_filter [printcap_field $i "if=" ""]
    if {$i_filter == "$spool_dir/filter"} {
	# possibly a filter generated by this tool
	if {[catch {set fd [open $i_filter "r"]}] == 1} {
	    return {}
	}
	gets $fd s
	gets $fd s
	gets $fd s
	if {[regexp "^DEVICE=\(.*\)\$" $s dummy auto_printer] != 1} {
	    close $fd
	    return {}
	}

	gets $fd s
	if {[regexp "^RESOLUTION=\(.*\)\$" $s dummy auto_resolution] != 1} {
	    close $fd
	    return {}
	}

	gets $fd s
	if {[regexp "^PAPERSIZE=\(.*\)\$" $s dummy auto_paper_size] != 1} {
	    close $fd
	    return {}
	}

	gets $fd s
	if {[regexp "^SENDEOF=\(.*\)\$" $s dummy auto_pseof] != 1} {
	    close $fd
	    return {}
	}

	close $fd
	return [list "LOCAL" $auto_printer $auto_resolution $auto_paper_size \
		    $auto_pseof]
    }
    return {}
}

proc printcap_field {i field default} {
    global printer_entry
    if {[regexp ":$field\(\[^:\]*\):" $printer_entry($i) dummy val] == 1} {
	return $val
    } else {
	return $default
    }
}

proc set_printcap_field {i field val} {
    global printer_entry
    if {[regsub ":$field\[^:\]*:" $printer_entry($i) ":$field$val:" \
	     printer_entry($i)] == 0} {
	# not there already: add to end (recall $printer_entry($i) ends w/":")
	set printer_entry($i) "$printer_entry($i)$field$val:"
    }
}

proc set_printcap_field_if_not_null {i field val} {
    if { $val != "" } {
	set_printcap_field $i "$field" "$val"
    }
}

proc remove_printcap_field {i field} {
    global printer_entry
    regsub ":$field\[^:\]*:" $printer_entry($i) ":" printer_entry($i)
}

proc printcap_switch {i field} {
    global printer_entry
    return [regexp ":$field:" $printer_entry($i)]
}

proc set_printcap_switch {i field val} {
    global printer_entry
    if {$val == 0} {
	regsub ":$field:" $printer_entry($i) ":" printer_entry($i)
    } else {
	if {[regexp ":$field:" $printer_entry($i)] == 0} {
	    set printer_entry($i) "$printer_entry($i)$field:"
	}
    }
}

proc redisplay {} {
    global print_count printer_names printer_type

    .list delete 0 end
    for {set i 0} {$i < $print_count} {incr i} {
	.list insert end [format "%-20s  %s" $printer_names($i) \
			      [$printer_type($i)_summaryline $i] ]
    }
}


#
# Routines to read the print filter information encoded on the
# PRINTOOL(3) comment line in the /etc/printcap
#
# This method of storing this is into is an ugly, temporary hack!
#
#

#
# parse_auto_filter_params { info }
#
# Pass this route the printer_info for the printer of interest.
# The printer_info is everything beyond the printer_type in the comment line
#
# Sets the following (global) auto_filter parameters based on $info:
#
# auto_printer
# auto_resolution
# auto_paper_size
# auto_eof
# auto_printerdb_entry
# auto_color
# auto_crlf_trans
# auto_rev_pages

#
#
#  also checks into several filter config files to get additional auto_filter
#  parameters
#
proc parse_auto_filter_params { index info spool_dir } {
    global auto_printer auto_resolution auto_paper_size auto_flag
    global printerdb_count printerdb_entry printerdb_GSDriver
    global auto_printerdb_entry
    global auto_color auto_crlf_trans auto_rev_pages
    global auto_ascps_trans
    global aout_eof auto_pseof auto_texteof auto_sendeof
    global auto_GSextra
    global auto_nup
    global auto_rtlft_margin auto_topbot_margin

#
# see if the printcap entry has any auto_filter info
#
    if {[llength $info] >= 4} {
	set auto_printer [lindex $info 0]
	set auto_resolution [lindex $info 1]
	set auto_paper_size [lindex $info 2]
	set auto_eof [lindex $info 3]
#
#       see if we have a PRINTTOOL3 info line, or older one 
#       which will need to be converted to a PRINTTOOL3 format
#
	if { [llength $info] > 4} {
	    set auto_printerdb_entry [lindex $info 4]
	    set auto_color [lindex $info 5]
	    set auto_crlf_trans [lindex $info 6]
	    set auto_rev_pages [lindex $info 7]
	} else {
#           we have to find best match in printer database
	    rhs_info_dialog "
It appears this entry in your printcap 
was created by an older version of printtool.
The current printer database will now be 
searched for a compatible printer driver.

A copy of the current '/etc/printcap' file 
will be stored in '/etc/printcap.prior'.
You may want to make a copy of this file
after leaving this dialog box.

It is recommended that you check the driver
selected by the conversion process in order 
to confirm a proper replacement was chosen. 
In particular, the desired print resolution
and color depth may have been inadvertantly 
changed. Simply set these values back to the
 desired settings.

This conversion will not be required again.
"

            if [catch {exec cp /etc/printcap /etc/printcap.prior}] {
		rhs_error_dialog "Unable to make backup of /etc/printcap."
	    }
	    set auto_printerdb_entry ""
	    for {set j 0} {$j < $printerdb_count} {incr j} {
		set pentry $printerdb_entry($j)
#		puts "Looking for $auto_printer, found $printerdb_GSDriver($pentry)"
		if {[string compare $auto_printer $printerdb_GSDriver($pentry)] == 0} {
		    set auto_printerdb_entry $pentry
		    break
		}
	    }
	    if {$auto_printerdb_entry == ""} {
#		puts "Couldnt find driver in printerdb, resetting printer driver"
		rhs_error_dialog "Unable to find compatible driver in the printer database
file. You will need to reselect the driver from the list of available printer types."
		set auto_printerdb_entry $printerdb_entry(0)
	    }
	    set auto_color "Default"
#
#           we want to update printer_info to be new format which includes
#           the printerdb entry and the selected color depth
#
	    set auto_crlf_trans ""
	    set auto_rev_pages ""
	    lappend info $auto_printerdb_entry $auto_color \
		    $auto_crlf_trans $auto_rev_pages
#
#           auto_eof isn't really used anymore
#           guess new eof options based on old eof setting
#
	    if { $auto_eof != "" } {
		set auto_pseof "YES"
		set auto_texteof "YES"
		set auto_sendeof "YES"
	    }
	}
    } else {
	set auto_printer ""
	set auto_resolution ""
	set auto_paper_size ""
	set auto_eof ""
	set auto_pseof "NO"
	set auto_texteof "NO"
	set auto_sendeof "NO"
	set auto_crlf_trans ""
	set auto_rev_pages ""
	set auto_printerdb_entry ""
        set auto_GSDriver ""
	set auto_color ""
    }

#
#    look into filter config files
#
    if {$spool_dir == ""} {
	set auto_ascps_trans "NO"
    } else {

	catch {exec sed -n -e "s/export ASCII_TO_PS=//p" \
		< $spool_dir/general.cfg} auto_ascps_trans
	# MLP:
	set auto_ascps_trans [printcap_field $index "ASCII_TO_PS=" "$auto_ascps_trans"]
	if {$auto_ascps_trans != "YES" && $auto_ascps_trans != "NO"} {
	    set auto_ascps_trans "YES"
	}

	catch {exec sed -n -e "s/PS_SEND_EOF=//p" \
		< $spool_dir/postscript.cfg} auto_pseof
	# MLP:
	set auto_pseof [printcap_field $index "PS_SEND_EOF=" "$auto_pseof"]
	if {$auto_pseof != "YES" && $auto_pseof != "NO"} {
	    set auto_pseof "YES"
	}

	if [catch {exec sed -n -e "s/EXTRA_GS_OPTIONS=//p" \
		< $spool_dir/postscript.cfg} auto_GSextra] {
	    set auto_GSextra ""
	} else {
	    # trim off the quotation marks if they exist
	    set auto_GSextra [string trim $auto_GSextra \"]
	}
	# MLP:
	set auto_GSextra [printcap_field $index "EXTRA_GS_OPTIONS=" "$auto_GSextra"]

	if [catch {exec sed -n -e "s/NUP=//p" \
		< $spool_dir/postscript.cfg} auto_nup] {
	    set auto_nup "1"
	    # MLP:
	    set auto_nup [printcap_field $index "NUP=" "$auto_nup"]
	} else {
	    # MLP:
	    set auto_nup [printcap_field $index "NUP=" "$auto_nup"]
	    if { [regexp {[^0-9]+} $auto_nup junk1 junk2] } {
		puts "Error in NUP value from postscript.cfg"
		set auto_nup "1"
	    }
	    if {$auto_nup != "1" && $auto_nup != "2" && $auto_nup != "4" && \
		    $auto_nup != "8" } {
		set auto_nup "1"
	    }
	}
	
	if [catch {exec sed -n -e "s/RTLFTMAR=//p" \
		< $spool_dir/postscript.cfg} auto_rtlft_margin] {
	    set auto_rtlft_margin "18"
	    # MLP:
	    set auto_rtlft_margin [printcap_field $index "RTLFTMAR=" "$auto_rtlft_margin"]
	} else {
	    set auto_rtlft_margin [string trim $auto_rtlft_margin]
	    # MLP:
	    set auto_rtlft_margin [printcap_field $index "RTLFTMAR=" "$auto_rtlft_margin"]
	    if { [regexp {[^0-9]+} $auto_rtlft_margin junk1 junk2] } {
		puts "Error in RT/LFT margin value from postscript.cfg"
		set auto_rtlft_margin "18"
		puts "Setting to default value of $auto_rtlft_margin pts."
	    } elseif { $auto_rtlft_margin == "" } {
		set auto_rtlft_margin "18"
	    } 
	}

	if [catch {exec sed -n -e "s/TOPBOTMAR=//p" \
		< $spool_dir/postscript.cfg} auto_topbot_margin] {
	    set auto_topbot_margin "18"
	    # MLP:
	    set auto_topbot_margin [printcap_field $index "TOPBOTMAR=" "$auto_topbot_margin"]
	} else {
	    set auto_topbot_margin [string trim $auto_topbot_margin]
	    # MLP:
	    set auto_topbot_margin [printcap_field $index "TOPBOTMAR=" "$auto_topbot_margin"]
	    if { [regexp {[^0-9]+} $auto_topbot_margin junk1 junk2] } {
		puts "Error in TOP/BOT margin value from postscript.cfg"
		set auto_topbot_margin "18"
		puts "Setting to default value of $auto_rtlft_margin pts."
	    } elseif { $auto_topbot_margin == "" } {
		set auto_topbot_margin "18"
	    } 
	}

	catch {exec sed -n -e "s/TEXT_SEND_EOF=//p" \
		< $spool_dir/textonly.cfg} auto_texteof
	# MLP:
	set auto_texteof [printcap_field $index "TEXT_SEND_EOF=" "$auto_texteof"]
	if {$auto_texteof != "YES" && $auto_texteof != "NO"} {
	    set auto_texteof "NO"
	}

	if {$auto_texteof == "YES" || $auto_pseof == "YES"} {
	    set auto_sendeof "YES"
	} else {
	    set auto_sendeof "NO"
	}
    }

    return $info
}




#
# Routines to handle the printer database
#
#
#

#
# Function to read a printer entry from the database
#
# Pass it the file descriptor of the open database
#
# If valid entry found, name of entry will be returned.
#        Null string returned if no more valid entries.
#
#
# Position in database on return will point past the entry read in
# and GetNextField{} should be called to start reading entry in
#
#
proc FindNextEntry { fd } {

    global printerdb_curdbline

#
# search through file until we find a line marking the start of an entry
#

    while {[gets $fd s] != "-1"} {
	incr printerdb_curdbline

	# strip spaces at the front
	set cp [string trimleft $s]

	# is this a comment line?
	if {[regexp "^\#" $cp] == 1} {
	    continue
	}
	# is this the line we're seeking
	if {[regexp "^StartEntry:" $cp] == 1} {
	    return [lindex [split $cp :] 1]
	}
    }

    # we reached EOF and found no more entries
    return {}
}


#
# GetNextField { fd }
#
# Read the next field from file descriptior fd from inside a printer entry.
# ASSUMES that FindNextEntry{} has been called an that
# file is positioned at start of an entry.
# Handles comment lines and continuation markers.
# If end of entry reached, returns 'EndEntry'.
# Otherwise, a list is returned of the format:
# { {FieldName} {param0} {param1} {param2} etc }
# 
#
proc GetNextField { fd } {
    global printerdb_curdbline
    global printerdb_debug

    # First we want to find the line marking the start of a new field
    while {[gets $fd s] != "-1"} {
	incr printerdb_curdbline

	if { $printerdb_debug } {
	    puts "In GetNextField, read line number $curdbline"
	    puts "        ->   $s"
	}

	# strip spaces at the front
	set cp [string trimleft $s]

	# is this a comment line?
	if {[regexp "^\#" $cp] == 1} {
	    continue
	}
	# is something messed up?
	if {[regexp "^StartEntry:" $cp] == 1} {
	    puts "Somehow we've hit the start of a new entry.\n"
	    puts "Printer data base file is corrupt near line $printerdb_curdbline.\n"
	    exit 0
	}
	# if it the end of an entry
	if {[regexp "EndEntry" $cp] == 1} {
	    return [list EndEntry]
	}
	# Ok, is it a field? We can tell if it has a word followed by a colon
	if {[string first : $cp] != -1} {
	    set fieldlist [split $cp :]
	    set fieldname [lindex $fieldlist 0]
	    set fieldparams [string trim [lindex $fieldlist 1]]

	    if { $printerdb_debug } {
		puts  "fieldlist   -> $fieldlist"
		puts  "fieldname   -> $fieldname"
		puts  "fieldparams -> $fieldparams"
	    }

	    # if fieldparams ends with a continuation marker, we need to 
            # keep reading lines.
	    if {[regexp "\\\\$" $fieldparams] == 1} {
		if { $printerdb_debug } {
		    puts "We found trailing continuation marker"
		}

		# remove trailing continuation mark
		set fieldparams [string trimright $fieldparams " \\"]

		while {[gets $fd s] != "-1"} {
		    incr printerdb_curdbline

		    if { $printerdb_debug } {
			puts "Line1: $s"
		    }
		    # strip spaces at both ends
		    set cp [string trim $s]

		    # is this a comment line?
		    if {[regexp "^\#" $cp] == 1} {
			continue
		    }

		    # ok, append to current line
		    append fieldparams " " $cp
		    if { $printerdb_debug } {
			puts "Appended: $fieldparams"
		    }

	            # is this another continuation line?
		    # if so, cleanup end of line and keep going
		    if {[regexp "\\\\$" $fieldparams] == 1} {
			set fieldparams [string trimright $fieldparams " \\"]
			continue
		    } else {
			# we're done
			break
		    }
		}

	    }
	    # finished reading this param, time to move to next
#	    puts "| $fieldparams |"
#	    puts "len of fieldparams is [string length $fieldparams]"
	    return [concat $fieldname  $fieldparams]
	}
    }

    # we reached EOF and found no more entries
    return {}
}

#
# see if ghostscipt is installed and which drivers compiled in
# 
proc find_gs {} {
    global gs_installed_drivers

    set gs_installed_drivers ""

    if [catch {open "|/usr/bin/gs -help |& cat"} input] {
	puts "open result was $input"
    }

    if { [string first "couldn't execute" $input] != -1 } {
	rhs_error_dialog "Ghostscript does not appear to be installed.
This severely restricts your printing options unless either:

 a) You want to setup your printer to print text only, or
 b) Your printer can handle PostScript natively.

If neither of these apply, it would be best to install ghostscript now.
"
set gs_installed_drivers {TEXT POSTSCRIPT}
return
}

    while {[gets $input line] >= 0} {
	append gshelp $line \n
    }
    catch {close $input}

    set avail [string first "Available devices:" $gshelp]
    incr avail [string length "Available devices:\n"]
    set availstr [string range $gshelp $avail end]
    set nextfield [string first ":" $availstr]

    for {set i $nextfield} {[string index $availstr $i] != "\n"} {incr i -1} {
	if { $i <= 1 } {
	    puts "Error - couldnt find available devices in find_gs"
	    puts "        this should not happen!"
	}
    }
    incr i -1
    set availstr [string range $availstr 0 $i]
    regsub -all \t $availstr {} availstr2
    set gs_installed_drivers [split $availstr2]
#
# if pnm2ppa is installed, and the gs driver "pnmraw" is also installed,
# add "ppa" to the list of installed drivers.
    if {[catch {open "|/usr/bin/pnm2ppa -h"} input] == 0}  {
	if {[regexp "pnmraw" $gs_installed_drivers ] == 1} {
	     lappend gs_installed_drivers {ppa} 
	}
    }
#
# following two DO NOT depend upon ghostscript being installed
# please handle this more cleanly in future!
#
    lappend gs_installed_drivers {TEXT} {POSTSCRIPT}
}

#
# see if mpage is installed
# 
proc find_mpage {} {

    if [catch {open "|/usr/bin/mpage -x"} input] {
#	puts "open result was $input"
    }

    if { [string first "couldn't execute" $input] != -1 } {
	rhs_error_dialog "Mpage does not appear to be installed.
This severely restricts your printing options unless
you want to setup your printer to print text only.

It would probably be best to install mpage now.
"
}
}


#
# see if smbclient is installed
# 
proc find_samba {} {
    global warn_samba

    if [catch {open "|/usr/bin/smbclient -x"} input] {
#	puts "open result was $input"
    }
    
    if { [string first "couldn't execute" $input] != -1 } {
	set foo [rhs_dialog .foo "Error" "Samba does not appear to be installed.

You will not be able to print to a SMB/Windows 95/NT printer without it installed.

Please quit and install samba before configuring any SMB/Windows 95/NT printer entries.

If you are not interested in support of SMB/Windows 95/NT remote printers,

please choose 'Ignore', and you will not see this message in the future.
" error 0 "Ok" "Ignore"]
         if { $foo != 0 } {
	     set warn_samba 0
	 }
     }
}

#
# see if nprint is installed
# 
proc find_nprint {} {
     global warn_ncpfs

    if [catch {open "|/usr/bin/nprint -x"} input] {
#	puts "open result was $input"
    }

    if { [string first "couldn't execute" $input] != -1 } {
	set foo [rhs_dialog .foo "Error" "ncpfs does not appear to be installed.

You will not be able to print to a NetWare printer without it installed.

Please quit and install ncpfs before configuring any NetWare printer entries.

If you are not interested in support of NetWare remote printers, 

please choose 'Ignore' and you will not see this message in the future.
" error 0 "Ok" "Ignore"]
	if { $foo != 0 } { 
	    set warn_ncpfs 0
	}
    }
}


#
# load in the printer database
#

proc reload_printerdb {} {
    global printerdb_entry printerdb_descr
    global printerdb_about printerdb_GSDriver
    global printerdb_res   printerdb_color
    global printerdb_count
    global printerdb_descr2entry
    global printerdb_curdbline
    global filtersrcdir

#
#   counter to let us know where printerdb integrity errors occur
#
    set printerdb_curdbline 0
    set printerdb_debug 0
    set printerdb_count 0
    if {[catch {set fd [open "$filtersrcdir/printerdb" r]}] == 1} {
	# there is no printerdb
	puts "Error: cannot find $filtersrcdir/printerdb!"

	rhs_info_dialog	"Could not find printer database
required for printer setup. Make sure
that the gnulpr printfilters package is
properly installed."

        exit
    }
    
    set i 0
    while { 1 == 1}  {
	# get next entry, fill in display widgets
	set nextname [string trim [ FindNextEntry $fd ]]
	# if we find no more, exit
	if { $nextname == {} } {
	    set printerdb_count $i
	    return;
	}
	set printerdb_entry($i) $nextname
#	puts "Found printer entry called $nextname"
#	puts "Setting printerdb_entry($i) to $nextname"
	incr i

	# find all internal fields
	while { 1 == 1} {
	    set fieldlist [GetNextField $fd ]
#	    puts "fieldlist is $fieldlist"
	    if { [string compare $fieldlist "EndEntry" ] == 0 } {
#		puts "End of entry found for printer entry $nextname"
		break
	    } elseif { $fieldlist == {} } {
		break;
	    } 

	    # store into associative array of lists indexed by field name
	    set fieldname [lindex $fieldlist 0]
	    set field_array($fieldname)  [lrange $fieldlist 1 end]
#	    puts "Field $fieldname value is [lrange $fieldlist 1 end]"

	    # grab ones we really want and store now
	    if { [string compare $fieldname "Description" ] == 0 } {
		set printerdb_descr($nextname) [lindex $field_array(Description) 0]
		set printerdb_descr2entry($printerdb_descr($nextname)) $nextname
	    } elseif { [string compare $fieldname "About" ] == 0 } {
		set printerdb_about($nextname) [string trim [lindex $field_array(About) 0]]
	    } elseif { [string compare $fieldname "GSDriver" ] == 0 } {
		set printerdb_GSDriver($nextname) [lindex $field_array(GSDriver) 0]
	    } elseif { [string compare $fieldname "Resolution" ] == 0 } {
		lappend printerdb_res($nextname) $field_array(Resolution)
	    } elseif { [string compare $fieldname "BitsPerPixel" ] == 0 } {
		lappend printerdb_color($nextname) $field_array(BitsPerPixel)
	    }

	}
	# set printdb arrays from entries
    }
}






# Each printer type should export functions
#  TYPE_summaryline {i}  giving a summary for the main printer list
#  TYPE_name {}  giving a readable name for the type
#  TYPE_addpanel {w i} adds the configuration entries to the edit panel
#  TYPE_updateentry {w i} pulls configuration options out of the edit panel
#  TYPE_setup {i}  doing whatever extra type-specific setup is necessary
# printer_type_list is a list of printer types which should be presented
# to the user -- ie everything but UNKNOWN

set printer_type_list {LOCAL REMOTE SMB NCP DIRECT}

proc UNKNOWN_summaryline {i} { return "type unrecognized" }
proc UNKNOWN_name {} { return "unknown printer type--should not appear" }
proc UNKNOWN_addpanel {w i} {
    puts "UNKNOWN_addpanel called--shouldn't happen"
}
proc UNKNOWN_updateentry {w i} { 
    puts "UNKNOWN_updateentry called--shouldn't happen" 
}
proc UNKNOWN_setup {i} { puts "UNKNOWN_setup called--shouldn't happen" }


#
# sets up common stuff for queues which use auto_filter
#
proc FILTERED_updateentry {i filter} {
    global printer_info
    global auto_printer auto_resolution auto_paper_size auto_flag
    global auto_printerdb_entry auto_color auto_crlf_trans
    global suppress_headers
    global auto_eof auto_texteof auto_pseof auto_sendeof

    set is_auto [string trim [lindex [split $filter "-"] 0]]
    if {$filter != ""} {
	if {$is_auto != $auto_flag} {
	    set_printcap_field $i "if=" $filter
	} else {
	    set_printcap_field $i "if=" $auto_flag
	}
    } else {
	remove_printcap_field $i "if="
    }
    set_printcap_switch $i "sh" $suppress_headers
    if {$is_auto == $auto_flag} {
	set printer_info($i) [list $auto_printer $auto_resolution \
				  $auto_paper_size $auto_eof $auto_printerdb_entry $auto_color $auto_crlf_trans]
    } else {
	set printer_info($i) ""
    }
}


proc FILTERED_setup {i} {
    global auto_printer auto_resolution auto_paper_size auto_flag
    global auto_printerdb_entry auto_color auto_crlf_trans
    global auto_rev_pages
    global filtersrcdir
    global libexecdir
    global desired_print_format
    global printer_type printer_names
    global auto_ascps_trans
    global auto_eof auto_pseof auto_texteof auto_sendeof
    global auto_GSextra
    global auto_nup auto_rtlft_margin auto_topbot_margin
    global auto_ppdfile
    global xref
    global printerdb_descr
    global ppddir
    global locale

    # MLP
    # See if we have a matching PPD file

    if {[info exists auto_printerdb_entry] && ($auto_printerdb_entry != "")} {
	set auto_ppdfile ""
	set printer_description $printerdb_descr($auto_printerdb_entry)

	set pos [string last " (Postscript)" $printer_description]
	incr pos -1
	if {$pos >= 0} {
	    set printer_description [string range $printer_description 0 $pos]
	}

	if {[info exists xref($printer_description)]} {
	    set auto_ppdfile $xref($printer_description)

	    # MLP - Determine ppd directory including locale subdirectory
	    get_locale
	    if {[file exists "$ppddir$locale/$auto_ppdfile"]} {
		set ppdfile "$ppddir$locale/$auto_ppdfile"
	    } else {
		set ppdfile "$ppddir$auto_ppdfile"
	    }

	    set_printcap_field $i "ppdfile=" "$ppdfile"
	}

    } else {
	set auto_ppdfile ""
    }

    # End MLP

    set i_filter [printcap_field $i "if=" ""]
    if {$i_filter == $auto_flag} {
	set spool_dir [printcap_field $i "sd=" ""]
	set i_filter "$spool_dir/filter"
	set_printcap_field $i "if=" $i_filter
	catch {exec rm -f $i_filter}
#
#   first copy the master filter into place
#
#	puts "cp'ing $filtersrcdir/master-filter to $i_filter"
#	catch {exec cp "$filtersrcdir/master-filter" $i_filter}
#	catch {exec chmod 755 $i_filter}

#   first put a link to the master filter into place
#
        catch {exec ln -s "$libexecdir/master-filter" $i_filter}

#
#   create the appropriate queue specific fpi config files
#

#
#   see if we need postscript or not
#
	if {$auto_printer != "TEXT"} {
	    set desired_print_format "ps"
        } else {
            set desired_print_format "asc"
            set auto_ascps_trans "NO"
	}

	# MLP: add printcap fields for all the former
	# general.cfg variables:
	set_printcap_field_if_not_null $i "PAPERSIZE=" "$auto_paper_size"
	set_printcap_field_if_not_null $i "DESIRED_TO=" "$desired_print_format"
	set_printcap_field_if_not_null $i "PRINTER_TYPE=" "$printer_type($i)"
	set_printcap_field_if_not_null $i "ASCII_TO_PS=" "$auto_ascps_trans"
	#------------------------------------------------------

	#catch {exec sed -e "s/@@@desiredto@@@/$desired_print_format/g" \
	#	-e "s/@@@papersize@@@/$auto_paper_size/g" \
	#	-e "s/@@@printertype@@@/$printer_type($i)/g" \
	#	-e "s/@@@ascps_trans@@@/$auto_ascps_trans/g" \
	#	< $filtersrcdir/general.cfg.in > $spool_dir/general.cfg}
 	#catch {exec chmod 755 $spool_dir/general.cfg}

#
#   setup postscript/ghostscript filter
#

#
#   fixup color param
#
	if {$auto_color == "Default"} {
	    set tauto_color ""
        } elseif {$auto_printer == "uniprint"} {
	#  if uniprint, COLOR is set to filename of filename.upp
	    set tauto_color "$auto_color"
	} else {
	#  if color setting for traditional drivers
	    set tauto_color "-dBitsPerPixel=$auto_color"
	}

	# MLP: add printcap fields for all the former
	# postscript.cfg variables:
	set_printcap_field_if_not_null $i "GSDEVICE=" "$auto_printer"
	set_printcap_field_if_not_null $i "PAPERSIZE=" "$auto_paper_size"
	set_printcap_field_if_not_null $i "RESOLUTION=" "$auto_resolution"
	set_printcap_field_if_not_null $i "COLOR=" "$tauto_color"
	set_printcap_field_if_not_null $i "REVERSE_ORDER=" "$auto_rev_pages"
	set_printcap_field_if_not_null $i "EXTRA_GS_OPTIONS=" "$auto_GSextra"
	set_printcap_field_if_not_null $i "PS_SEND_EOF=" "$auto_pseof"
	set_printcap_field_if_not_null $i "NUP=" "$auto_nup"
	set_printcap_field_if_not_null $i "RTLFTMAR=" "$auto_rtlft_margin"
	set_printcap_field_if_not_null $i "TOPBOTMAR=" "$auto_topbot_margin"
	#------------------------------------------------------

	#if [catch {exec sed -e "s/@@@gsdevice@@@/$auto_printer/g" \
	#	-e "s/@@@papersize@@@/$auto_paper_size/g" \
	#	-e "s/@@@resolution@@@/$auto_resolution/g" \
	#	-e "s/@@@color@@@/$tauto_color/g" \
	#	-e "s/@@@reversepages@@@/$auto_rev_pages/g" \
	#	-e "s^@@@extragsoptions@@@^\"$auto_GSextra\"^g" \
	#	-e "s/@@@pssendeof@@@/$auto_pseof/g" \
	#	-e "s/@@@nup@@@/$auto_nup/g" \
	#	-e "s/@@@rtlftmar@@@/$auto_rtlft_margin/g" \
	#	-e "s/@@@topbotmar@@@/$auto_topbot_margin/g" \
	#	< $filtersrcdir/postscript.cfg.in \
	#	> $spool_dir/postscript.cfg} blah] {
	#    puts "postscript.cfg error: $blah"
	#}
        #catch {exec chmod 755 $spool_dir/postscript.cfg}
    }
#
#  do we need to setup text-only printing?
#
#  yes - we always write this, just in case they want to use
#        native text printing on their printer, and yet have
#        ghostscript print out other formats
#
        set textonly_opt ""

	# MLP: add printcap fields for all the former
	# textonly.cfg variables:
	set_printcap_field_if_not_null $i "TEXTONLYOPTIONS=" "$textonly_opt"
	set_printcap_field_if_not_null $i "CRLFTRANS=" "$auto_crlf_trans"
	set_printcap_field_if_not_null $i "TEXT_SEND_EOF=" "$auto_texteof"
	#------------------------------------------------------

	#catch {exec sed -e "s/@@@textonlyoptions@@@/$textonly_opt/g" \
	#	-e "s/@@@crlftrans@@@/$auto_crlf_trans/g" \
	#	-e "s/@@@textsendeof@@@/$auto_texteof/g" \
	#	< $filtersrcdir/textonly.cfg.in > $spool_dir/textonly.cfg}
	#catch {exec chmod 755 $spool_dir/textonly.cfg} 

# NickM
	#if {[file executable "/usr/sbin/checkpc"] == 1} {
	    #set queue [string trim [lindex [split $printer_names($i) "|"] 0]]
	    #catch {exec /usr/sbin/checkpc -fP $queue}
#
#
	#}
# end NickM
	if {[info exists ppdfile]} {
	    if {! [file exists "$ppdfile"]} {
	    rhs_info_dialog "NOTE: This printer uses a Postscript Printer\
Description\n(PPD) file called $auto_ppdfile,\navailable from the printer\
vendor. This file should be\ninstalled in the $ppddir directory."
            }
	}    
	
}

proc FILTERED_summaryline {i def} { 
    global printer_info
    global printerdb_descr

    set info $printer_info($i)
    if {[llength $info] > 4} {
	set idx [lindex $info 4]
	if { [info exists printerdb_descr($idx)] } {
	    set printer $printerdb_descr($idx)
	} else {
	    rhs_info_dialog \
"Note: The Printer \"$idx\" (specified in\n\
/etc/printap) wasn't found in the printer database.\n\
Check that you have selected the proper Input Filter."
	    set printer $def
	}
    } elseif {[llength $info] >= 1} {
	set printer [lindex $info 0]
    } else {
	set printer $def
    }
    return "$printer"
}



proc FILTERED_addpanel {w i} {
    global printer_names printer_type printer_info printer_entry
    global printer_comments printer_type_list
    global suppress_headers
    global auto_printer auto_resolution auto_paper_size auto_flag
    global printerdb_count printerdb_entry printerdb_GSDriver
    global auto_printerdb_entry
    global auto_color auto_crlf_trans auto_rev_pages
    global auto_eof auto_pseof auto_texteof

    frame $w.filter
    frame $w.filter.r
    frame $w.suppress

    label $w.l10 -text "Input Filter" -anchor w
    button $w.autofilter -text "Select" -command "LOCAL_select_filter $w.v10"

    entry $w.v10 -font fixed -relief sunken -borderwidth 2
    $w.v10 delete 0 end
    $w.v10 insert 0 [printcap_field $i "if=" ""]
    pack $w.autofilter -side left -anchor w  -in $w.filter.r
    pack $w.v10 -side right -anchor e -in $w.filter.r
    pack $w.l10 -side left -anchor w -in $w.filter
    pack $w.filter.r -side right -anchor e -in $w.filter

    checkbutton $w.v11 -text "Suppress Headers" -anchor w \
	-variable suppress_headers
    set suppress_headers [printcap_switch $i "sh"]

#    pack $w.fl9 -side top -fill x -in $w.f1
#    pack $w.v9 -pady 5 -side top -expand true -fill x -in $w.f2
#    pack $w.v10 -side top -expand true -fill x -in $w.f2

    pack $w.v11 -in $w.suppress
    pack $w.filter -fill x -side top -in $w.f1
    pack $w.suppress  -side top -in $w.f1

    # fill in defaults for select_filter panel
    set spool_dir [printcap_field $i "sd=" ""]
    set info [parse_auto_filter_params $i $printer_info($i) $spool_dir]
    set printer_info($i) $info

    if { $auto_printerdb_entry != "" } {
        $w.v10 delete 0 end
        $w.v10 insert 0 $auto_flag
	$w.v10 insert end " - $auto_printerdb_entry"
    }


}



proc LOCAL_summaryline {i} { 
    global printer_info
    global printerdb_descr
    
    set printer [FILTERED_summaryline $i "local printer"]
    set port [printcap_field $i {lp=} {unspecified device}]
    return "$printer on $port"
}

proc LOCAL_name {} { return "Local Printer" }

proc LOCAL_addpanel {w i} {
    global printer_names printer_type printer_info printer_entry
    global printer_comments printer_type_list
    global suppress_headers
    global auto_printer auto_resolution auto_paper_size auto_flag
    global printerdb_count printerdb_entry printerdb_GSDriver
    global auto_printerdb_entry
    global auto_color auto_crlf_trans auto_rev_pages
    global auto_eof auto_pseof auto_texteof


    frame $w.dev

    label $w.l8 -text "Printer Device" -anchor w
    entry $w.v8 -font fixed -relief sunken -borderwidth 2
    $w.v8 insert 0 [printcap_field $i "lp=" ""]

#
#   if no printer device specified, lets suggest one
#
    set lp0_stat "Not detected"
    set lp1_stat "Not detected"
    set lp2_stat "Not detected"
    set usb_lp0_stat "Not detected"
    set usb_lp1_stat "Not detected"
    set usb_lp2_stat "Not detected"

    if { [$w.v8 get] == "" } {
	if { [catch {open /dev/lp0 w} junkfp] == 0} {
	    set lp0_stat "Detected"
	    if { [$w.v8 get] == ""} {
		$w.v8 insert 0 "/dev/lp0"
	    }
	    close $junkfp
	}
	if { [catch {open /dev/lp1 w} junkfp] == 0} {
	    set lp1_stat "Detected"
	    if { [$w.v8 get] == ""} {
		$w.v8 insert 0 "/dev/lp1"
	    }
	    close $junkfp
	}
	if { [catch {open /dev/lp2 w} junkfp] == 0} {
	    set lp2_stat "Detected"
	    if { [$w.v8 get] == ""} {
		$w.v8 insert 0 "/dev/lp2"
	    }
	    close $junkfp
	}
	if { [catch {open /dev/usb/lp0 w} junkfp] == 0} {
	    set usb_lp0_stat "Detected"
	    if { [$w.v8 get] == ""} {
		$w.v8 insert 0 "/dev/usb/lp0"
	    }
	    close $junkfp
	}
	if { [catch {open /dev/usb/lp1 w} junkfp] == 0} {
	    set usb_lp1_stat "Detected"
	    if { [$w.v8 get] == ""} {
		$w.v8 insert 0 "/dev/usb/lp1"
	    }
	    close $junkfp
	}
	if { [catch {open /dev/usb/lp2 w} junkfp] == 0} {
	    set usb_lp2_stat "Detected"
	    if { [$w.v8 get] == ""} {
		$w.v8 insert 0 "/dev/usb/lp2"
	    }
	    close $junkfp
	}


	# Since this message can be confusing, I'm only
	# presenting it when NO lp devices (lp[0-2]) are
	# present.

	if {(($lp0_stat == "Not detected") && \
	     ($lp1_stat == "Not detected") && \
             ($lp2_stat == "Not detected")) \
	     && \
            (($usb_lp0_stat == "Not detected") && \
             ($usb_lp1_stat == "Not detected") && \
             ($usb_lp2_stat == "Not detected"))} {
	    rhs_info_dialog "Printtool found the following devices:

         /dev/lp0 :      $lp0_stat
         /dev/lp1 :      $lp1_stat
         /dev/lp2 :      $lp2_stat
         /dev/usb/lp0 :  $usb_lp0_stat
         /dev/usb/lp1 :  $usb_lp1_stat
         /dev/usb/lp2 :  $usb_lp2_stat

You may disregard this message 
if you are setting up a serial printer.

This port detection may not always work
on Sparc and Alpha architectures.

If no devices were found, this could
indicate a hardware problem that justifies
further investigation.

If you have a USB printer, but it was not
detected, make sure that the USB printer
module has been inserted and is working."
        }
    }   

    pack $w.l8 -side left -anchor w -in $w.dev
    pack $w.v8 -side right -anchor e -in $w.dev
    pack $w.dev -side top -fill x -in $w.f1

#    pack $w.l8 -pady 3 -side top -fill x -in $w.f1
#    pack $w.v8  -side top -expand true -fill x -in $w.f2

    FILTERED_addpanel $w $i

}

#
# handles picking a new printer type in print filter window.
#
# updates description, resolution, color depth, and paper size listboxes based
# on the printer db entry
#
# you pass it the index of printre type in printerdb_entry()
# and names of the various widgets
#
proc LOCAL_select_printer_type {index printer about res color paper asc2ps window_opened} {

    global printerdb_entry printerdb_descr
    global printerdb_about printerdb_GSDriver
    global printerdb_res   printerdb_color
    global printerdb_descr2entry
    global printerdb_count
    global paper_size_list
    global auto_paper_size
    global auto_color
    global auto_resolution
    global auto_nup
    global auto_ascps_trans
    global current_printer_type auto_sendeof
    global tmp_auto_sendeof
    global autodetecting trigger_2

#   set the printer type selection
    $printer selection clear 0 end
    $printer selection set $index
    $printer see $index

#    set pentry $printerdb_entry([$printer curselection])
    set pentry $printerdb_descr2entry([$printer get $index])

    # Any time the user selects a new Postscript printer
    # filter when setting up a LOCAL printer, we want
    # to default to "SEND EOF".  The $window_opened variable
    # prevents the initial setting of "SEND EOF" from 
    # getting reset when the window is first displayed.
    # The trigger_2 variable indicates whether OK or Cancel
    # has been pressed 
    if { ($current_printer_type == "LOCAL") && (($window_opened == 0) || ($autodetecting == 1) && ($trigger_2 == 0)) } {
	if { $printerdb_GSDriver($pentry) == "POSTSCRIPT" } {
	    set tmp_auto_sendeof "YES"
	} else {
	    set tmp_auto_sendeof "NO"
	} 
    }

#
#   handle nup printing config
#
#   if this is the PostScript device, have to do this.
#   Disable 'Fast printing...' cause it doesn't apply.
#
    if {$pentry == "PostScript"} {
	$asc2ps configure -state disabled
    } else {
	$asc2ps configure -state normal
    }	


#   update the 'about' info
    $about config -state normal
    $about delete 1.0 end
#    $about insert 1.0 $printerdb_about($pentry)

#
#   wish I knew better, but how else to break lines in text widget??
#
    set abstr $printerdb_about($pentry)
    while { 1 == 1 } {
	set nlindex [string first "\\n" $abstr]
	if { $nlindex == -1 } {
	    $about insert end $abstr
	    break
	}
	incr nlindex -1
	$about insert end [string range $abstr 0 $nlindex]
	$about insert end "\n"
	incr nlindex 3
	set abstr [string trimleft [string range $abstr $nlindex end]]
    }

    $about config -state disabled
#
#   rebuild resolution list
#
    $res delete 0 end
    if [llength [array names printerdb_res $pentry]] {
	foreach x $printerdb_res($pentry) {
	    set g [format "%4sx%-4s %-20s"  [lindex $x 0] [lindex $x 1] [lindex $x 2]]
	    $res insert end $g
	}
    } else {
	$res insert end [format "%4s%-4s %-20s"  Default {} {}]
    }
    
#   set the resolution to the first available
#    $res selection clear 0 end
#    $res selection set 0
#
#   set resolution to
#
    if { [string compare $auto_resolution "Default"] == 0 } {
	set rresentry 0
    } elseif { [array names printerdb_res $pentry] == {} } {
	set rresentry 0
    } else {	
	set rresentry 0
	set reslist [split $auto_resolution x]
	set resxdpi [lindex $reslist 0]
	set resydpi [lindex $reslist 1]
	for {set i 0} {$i < [llength $printerdb_res($pentry)]} {incr i} {
	    set curres [lindex $printerdb_res($pentry) $i]
	    if { [lindex $curres 0] == $resxdpi && [lindex $curres 1] == $resydpi } {
		set rresentry $i
		break
	    }
	}
    }
    $res selection clear 0 end
    $res selection set $rresentry
    $res see $rresentry
    
#
#   rebuild color list
#
    $color delete 0 end

#
# ugly - if no color info assume default
#
    if [llength [array names printerdb_color $pentry]] {
    	foreach x $printerdb_color($pentry) {
	    if { $printerdb_GSDriver($pentry) == "uniprint" } {
	        # it's a uniprint upp entry
		# check to make sure the upp file exists
	        set fname [ format "%s.upp" [lindex $x 0] ]

	    	if { ([ glob -nocomplain "/usr/share/ghostscript/*/$fname" ] != "") || ([ glob -nocomplain "/usr/lib/ghostscript/*/$fname" ] != "") } {
		    set g [format "%8s, %-30s"  [lindex $x 0] [lindex $x 1]]
		    $color insert end $g
		}
	    } else {
		    set g [format "%8s, %-30s"  [lindex $x 0] [lindex $x 1]]
		    $color insert end $g
	    }
	}
    } else {
	$color insert end [format "%4s %-30s"  Default {}]
    }


#   set the color depth to the first available
#    $color selection clear 0 end
#    $color selection set 0
#
#       if this is default, then set accordingly
#
    if { [string compare $auto_color "Default"] == 0 } {
	set rcolentry 0
    } elseif { [array names printerdb_color $pentry] == {} } {
	set rcolentry 0
    } else {	
	set rcolentry 0
	for {set i 0} {$i < [llength $printerdb_color($pentry)]} {incr i} {
	    set colentry [lindex $printerdb_color($pentry) $i]
	    if { [lindex $colentry 0] == $auto_color } {
		set rcolentry $i
		break
	    } 
	}
    }
    $color selection clear 0 end
    $color selection set $rcolentry
    $color see $rcolentry

#   set the paper to the first available
#    $paper selection clear 0 end
#    $paper selection set 0
# 
#  if there is no active selection in the paper size listbox
#  make the current paper size the selection
#
    set papselect [$paper curselection]
    if { $papselect != "" } {
	set papentry [$paper get $papselect]
	set papindex [lsearch $paper_size_list $papentry]
	if { $papindex == -1 } {
	    set papindex 0
	}
    } else {
	set papindex [lsearch $paper_size_list $auto_paper_size]
	if { $papindex == -1 } {
	    set papindex 0
	}
    }
    $paper selection clear 0 end
    $paper selection set $papindex
    $paper see $papindex
}

proc enable_widgets {args} {
    puts "enabling widgets"
    foreach w $args {
	$w configure -state normal
    }
}

proc disable_widgets {args} {
    puts "disabling widgets"
    foreach w $args {
	$w configure -state disabled
    }
}


proc LOCAL_select_filter {e} {
    global trigger_2
    global paper_size_list
    global auto_printer auto_resolution auto_paper_size auto_flag

#
# newer printerdb globals, some supercede some above
#
    global printerdb_entry printerdb_descr
    global printerdb_about printerdb_GSDriver
    global printerdb_res   printerdb_color
    global printerdb_count
    global printerdb_descr2entry
    global auto_printerdb_entry
    global auto_color
    global auto_crlf_trans tmp_auto_crlf_trans
    global gs_installed_drivers
    global auto_rev_pages tmp_auto_rev_pages
    global auto_ascps_trans tmp_auto_ascps_trans
    global auto_texteof tmp_auto_texteof
    global auto_pseof tmp_auto_pseof
    global auto_sendeof tmp_auto_sendeof
    global auto_nup tmp_auto_nup
    global auto_rtlft_margin
    global auto_topbot_margin
    global auto_GSextra

#    puts "On entry ..."
#    puts "Selected GSDriver      -> $auto_printer"
#    puts "Selected printer entry -> $auto_printerdb_entry"
#    puts "Selected resolution    -> $auto_resolution"
#    puts "Selected color         -> $auto_color"
#    puts "Selected paper size    -> $auto_paper_size"
#    puts "Selected eof setting   -> $auto_eof"
#    puts "Selected cr/lf trans   -> $auto_crlf_trans"
#    puts "Selected reverse pages -> $auto_rev_pages"


    toplevel .sf
    wm withdraw .sf
#    wm transient .sf .
    wm group .sf .
    wm title .sf "Configure Filter"

#
# create these in an order that makes stacking work out right
#
# .cf's are container frames
#
    frame .sf.cf1
    frame .sf.cf2
    frame .sf.cf3
    frame .sf.cf4

    frame .sf.f1
    frame .sf.f2
    frame .sf.f5
    frame .sf.f3
    frame .sf.f4
    frame .sf.f6
    frame .sf.f7
    frame .sf.f8
    frame .sf.f10

#
# trying something different - define vars with names of Tk widgets here
#  then use names later on. We can allocate names as needed

#
# First we'll handle the listbox containing the printer drivers available
#
    label .sf.f1.l1 -text "Printer Type" -anchor w -width 12
    listbox .sf.f1.list -yscrollcommand [list .sf.f1.sy set] \
                        -width 40 -height 20 -setgrid true -selectmode single \
			-exportselection false

    scrollbar .sf.f1.sy -orient vertical -command [list .sf.f1.list yview]

#
# we want to display a sorted list of descriptions, but lets not
# change the order of the description array
#
    set sorted_descr ""
    for {set i 0} {$i < $printerdb_count} {incr i} {
	lappend sorted_descr $printerdb_descr($printerdb_entry($i))
    }
    set sorted_descr [lsort $sorted_descr]
    set existing_descr ""
    for {set i 0} {$i < $printerdb_count} {incr i} {
	set this_descr [lindex $sorted_descr $i]
	set this_entry $printerdb_descr2entry($this_descr)
	set this_gsdriver $printerdb_GSDriver($this_entry)
	if { [lsearch $gs_installed_drivers $this_gsdriver] != -1 } {
	    .sf.f1.list insert end $this_descr
	    lappend existing_descr $this_descr
	} 

    }
    pack .sf.f1.sy -side right -fill y
    pack .sf.f1.l1 -side top -fill x
    pack .sf.f1.list -side left -fill both -expand true

#
# set binding for selection in printer type window
#
    bind .sf.f1.list <Button-1> "
    LOCAL_select_printer_type \[.sf.f1.list nearest %y\] .sf.f1.list .sf.f2.text .sf.f3.list .sf.f6.list .sf.f4.list .sf.c3 0"
#
# Now add a verbose description of the driver
#
# we disable the text widget so it is read-only
#
    label .sf.f2.l1 -text "Driver Description" -anchor w -width 20
    text .sf.f2.text -relief sunken -setgrid true -wrap word \
                     -width 35 -height 4 -yscrollcommand ".sf.f2.sy set" \
		     -font  -adobe-helvetica-medium-r-normal--12*
    scrollbar .sf.f2.sy -orient vert -command ".sf.f2.text yview"
    .sf.f2.text insert 1.0 "Should be initializing this elsewhere!!"
    .sf.f2.text config -state disabled
    pack .sf.f2.sy -side right -fill y -in .sf.f2
    pack .sf.f2.l1      -side top -fill x -in .sf.f2
    pack .sf.f2.text -side left -fill both -expand true -in .sf.f2

#
# Now add resolution/paper size/color depth selections
#
    label .sf.f3.l1 -text "Resolution" -anchor w -width 12
    listbox .sf.f3.list -yscrollcommand [list .sf.f3.sy set] \
                        -width 20 -height 3 -setgrid true -selectmode single \
			-exportselection false -font fixed

    scrollbar .sf.f3.sy -orient vertical -command [list .sf.f3.list yview]
#    foreach x $resolution_list {
#	.sf.f3.list insert end $x
#    }
    pack .sf.f3.sy -side right -fill y
    pack .sf.f3.l1 -side top -fill x
    pack .sf.f3.list -side left -fill both -expand true

    label .sf.f4.l1 -text "Paper Size" -anchor w -width 12
    listbox .sf.f4.list -yscrollcommand [list .sf.f4.sy set] \
                        -width 7 -height 3 -setgrid true -selectmode single \
			-exportselection false -font fixed
    scrollbar .sf.f4.sy -orient vertical -command [list .sf.f4.list yview]
    foreach x $paper_size_list {
	.sf.f4.list insert end $x
    }
#

#  make the current paper size the selection
#
#    set papindex [lsearch $paper_size_list $auto_paper_size]
#    if { $papindex == -1 } {
#	set papindex 0
#    }
#    .sf.f4.list selection clear 0 end
#    .sf.f4.list selection set $papindex

#fix paper to get initialized color too

    pack .sf.f4.sy -side right -fill y
    pack .sf.f4.l1 -side top -fill x
    pack .sf.f4.list -side left -fill both -expand true


    label .sf.f6.l1 -text "Color Depth / Uniprint Mode" -anchor w -width 12
    listbox .sf.f6.list -yscrollcommand [list .sf.f6.sy set] \
                        -width 60 -height 4 -setgrid true -selectmode single \
			-exportselection false -font fixed

    scrollbar .sf.f6.sy -orient vertical -command [list .sf.f6.list yview]
    set color_list {1 3 8 16 24 43}
    foreach x $color_list {
	.sf.f6.list insert end $x
    }
    pack .sf.f6.sy -side right -fill y
    pack .sf.f6.l1 -side top -fill x
    pack .sf.f6.list -side left -fill both -expand true
#
# put these into the container frame
#
    pack .sf.f3 -side left -expand yes -anchor w -ipadx 10 -in .sf.cf1 
    pack .sf.f4 -side left -expand yes -anchor e -ipadx 10 -in .sf.cf1 
    pack .sf.cf1 .sf.f6 -side top -expand yes -ipadx 10 -in .sf.cf4 

#
# checkbutton to control if EOF is added to output
#
    label .sf.l10 -text "Printing Options" -anchor nw \
	    -font -adobe-helvetica-bold-r-normal--12*
    set tmp_auto_crlf_trans $auto_crlf_trans
    checkbutton .sf.c2 -text "Fix stair-stepping text?" \
	-variable tmp_auto_crlf_trans -offvalue "" -onvalue 1 -anchor nw \
	    -font -adobe-helvetica-medium-r-normal--12*
    set tmp_auto_sendeof $auto_sendeof
    checkbutton .sf.c4 -text "Send EOF after job to eject page?" \
	-variable tmp_auto_sendeof -offvalue "NO" -onvalue "YES" -anchor nw \
	    -font -adobe-helvetica-medium-r-normal--12*

#
# number of input pages per output page
#
    set LabNup ".sf.nupl11"
    set RBNup1 ".sf.nuprb1"
    set RBNup2 ".sf.nuprb2"
    set RBNup4 ".sf.nuprb4"
    set RBNup8 ".sf.nuprb8"
    set FrNup  ".sf.f9"
    
    frame $FrNup
    set tmp_auto_nup $auto_nup
    label $LabNup -text "pages per output page."  -anchor w \
	    -font -adobe-helvetica-medium-r-normal--12*

    radiobutton $RBNup1 -variable tmp_auto_nup -text "1" -value 1 -anchor nw \
	    -font -adobe-helvetica-medium-r-normal--12*
    radiobutton $RBNup2 -variable tmp_auto_nup -text "2" -value 2 -anchor nw \
	    -font -adobe-helvetica-medium-r-normal--12*
    radiobutton $RBNup4 -variable tmp_auto_nup -text "4" -value 4 -anchor nw \
	    -font -adobe-helvetica-medium-r-normal--12*
    radiobutton $RBNup8 -variable tmp_auto_nup -text "8" -value 8 -anchor nw \
	    -font -adobe-helvetica-medium-r-normal--12*
    pack $LabNup $RBNup1 $RBNup2 $RBNup4 $RBNup8 -side right -in $FrNup \
	    -fill none -anchor w

#
# margins used by mpage if asc->ps is selected
#
    set MarLab        ".sf.marlab"
    set MarTopBotLab  ".sf.marentlabtb"
    set MarRtLftLab   ".sf.marentlabrl"
    set MarTopBot ".sf.marenttb"
    set MarRtLft  ".sf.marentrl"
    set MarRtLftEnt ".sf.marrtlftent"
    set MarTopBotEnt ".sf.martopbotent"

    set FrMar     ".sf.marfr"
    set FrMarLab  ".sf.marfrlab"
    set FrMarEnt1 ".sf.marfrent1"
    set FrMarEnt2 ".sf.marfrent2"
    set FrMarEnt  ".sf.marfrent"

    frame $FrMar
    frame $FrMarLab
    frame $FrMarEnt
    frame $FrMarEnt1
    frame $FrMarEnt2

    label $MarLab  -text "Margins (in pts=1/72 of inch)" -anchor nw \
	    -font -adobe-helvetica-bold-r-normal--12*

    label $MarTopBotLab -text "Top/Bottom:" -anchor nw \
	    -font -adobe-helvetica-medium-r-normal--10*

    label $MarRtLftLab -text "Right/Left:" -anchor nw \
	    -font -adobe-helvetica-medium-r-normal--10*
    
    set tmp_auto_rtlft_margin $auto_rtlft_margin
    set tmp_auto_topbot_margin $auto_topbot_margin

    entry $MarRtLftEnt -font fixed -relief sunken -borderwidth 2 -width 6
    $MarRtLftEnt insert 0 $auto_rtlft_margin

    entry $MarTopBotEnt -font fixed -relief sunken -borderwidth 2 -width 6
    $MarTopBotEnt insert 0 $auto_topbot_margin
    
#    pack $MarLab -in $FrMarLab -anchor nw
    pack $MarRtLftLab $MarRtLftEnt  -side left -in $FrMarEnt1 \
	    -anchor w -fill none
    pack $MarTopBotLab $MarTopBotEnt -side left -in $FrMarEnt2 \
	    -anchor w -fill none
    pack $FrMarEnt2 $FrMarEnt1 -in $FrMarEnt -side right -fill none

#    pack $FrMarLab $FrMarEnt -side bottom -in $FrMar
#
# converting ascii to ps? (essentially, use mpage?)
#
    set CBasc2ps ".sf.c3"
    set tmp_auto_ascps_trans $auto_ascps_trans
    checkbutton $CBasc2ps -text "Fast text printing (non-PS printers only)?" \
	-variable tmp_auto_ascps_trans \
	-offvalue "YES" -onvalue "NO" -anchor nw \
	-font -adobe-helvetica-medium-r-normal--12* 

#
# add those promised GS options
#
    set LabGSextra ".sf.gsextlab"
    set EntGSextra ".sf.gsextent"
    set FrGSextra  ".sf.gsextfr"

    frame $FrGSextra

    label $LabGSextra -text "Extra GS options:" -anchor w \
	    -font -adobe-helvetica-bold-r-normal--10*

    entry $EntGSextra -relief sunken -borderwidth 2 -width 35 \
	    -font -adobe-helvetica-bold-r-normal--10*

    $EntGSextra insert 0 $auto_GSextra
    pack $LabGSextra $EntGSextra  -side left -in $FrGSextra \
	    -anchor w -fill none
    
#    set tmp_auto_pseof $auto_pseof
#    checkbutton .sf.c1 -text "Send EOF (\\004) after job?" \
#	-variable tmp_auto_pseof -offvalue "NO" -onvalue "YES" \
#	    -font -adobe-helvetica-medium-r-normal--12*
#
# we will support reversing order in future versions
# requires using 'psorder' program which comes with netatalk pkg
#
#    set tmp_auto_rev_pages $auto_rev_pages
#    checkbutton .sf.c3 -text "non Text-only printers: Reverse page order" \
#	-variable tmp_auto_rev_order -offvalue "" -onvalue 1
#    pack .sf.c3 .sf.c2 .sf.c1 -side top -in .sf.f7
#    pack .sf.c3 .sf.c1 .sf.l11 .sf.c2 .sf.l10 -side bottom -in .sf.f7

    pack  $FrGSextra -in .sf.f7 -side bottom -anchor w
    pack  $FrMarEnt -in .sf.f7 -side bottom -anchor w
    pack  $MarLab -in .sf.f7 -fill x -side bottom -anchor w
    pack  $FrNup  -in .sf.f7 -fill none -anchor w -side bottom
    pack  .sf.c3 -side bottom -fill x -anchor w -in .sf.f7
    pack  .sf.c2 .sf.c4 .sf.l10  -side bottom -fill x -in .sf.f7

#
# add buttons to exit this dialog
#
    button .sf.b1 -text "OK" -width 10 -command "set trigger_2 1" -anchor s
    button .sf.b2 -text "Cancel" -width 10 -command "set trigger_2 0" -anchor s
    button .sf.b3 -text "HELP" -width 10 -anchor s -command { help_dialog "Help for selecting a print filter" "

Help for selecting a print filter
---------------------------------

First choose the type of printer you have:

  Choose 'Text-only' if you only want to print text (ASCII).

  Choose 'PostScript' if you have a PostScript capable printer.

  Otherwise, find a printer in the listing which is close to the 
  model you own. This will configure the print system to use
  ghostscript for that type of printer.

  Many printers are compatible with other brands;
  for example, many printers are compatible with HP printers.
  Try different drivers and then print a PostScript test page.
  Some experimentation may be required. It is advisable to be
  near the printer you are configuring, as if the wrong driver
  is chosen you will want to be nearby to turn off the printer
  if it starts printing garbage.

Second, choose the Paper Size for your printer:

  Note that not all printers support all the listed paper sizes.
  Also, some printers require adjustments before a different size
  paper can be used.

Third, choose the Resolution and Color Depth:

  The resolution controls the quality of the output of the printer.
  Some printers only support one resolution. Others, like 
  Epson dot-matrix printers, support a variety of resolutions.
  Read the description of the printer for extra information on
  resolution and color options.

Final notes:

The \"Printing Options\" -

  \"Send EOF after job to eject page?\" -
      Forces the printer to eject the page when a printer job is done. 
      Some printers will not print until this character is received. 
      If you find you get an extra blank page, try turning this 
      option off.

  \"Fix stair-stepping text?\" -
      Turn this on if you are getting stair-stepped text.

  These options DO NOT apply if your printer is configured 'Text-only'.

  \"Fast text printing (non-PS printers only)?\" -
      Use when setting up a non-PostScript printer.
      If this option is ON, then text (ASCII) files will be sent
      straight to the printer. This will normal result in much
      faster printing than if this option is set OFF. In that
      case, the text input file will be sent to the program 'mpage',
      converted into PostScript, then sent to ghostscript to be printed.

      NOTE - The disadvantage to turning this option on is that you 
      cannot print multiple input pages per output page. That requires
      all input go through the 'mpage' program.

  \"8/4/2/1 pages per output page.\" -
      This option allows you to print multiple input pages per output
      page. If you have selected the \"Fast Text Printing\" option,
      however, this option will not work. By selected the \"Fast Text...\"
      option, you have told the print filter to not use 'mpage', which
      is required to get mulitple input pages per output page.

  \"Margins\" -
      You can adjust the margins of the page if you have selected to
      print 8, 4, or 2 pages per output page. If you have chosen 1
      output page per input page, you cannot adjust the margins at
      this time. This is due to a limitation of 'mpage' in handling
      arbitrary PostScript input.

  \"Extra GS options\" -
      Some ghostscript drivers, like the Epson Stylus driver, can accept
      additional options to control the print quality generated
      by the driver. You can put those options here. For example, on
      the Epson Stylus Color driver, the options:

       -sDithering=fscmyk /usr/share/ghostscript/4.03/stcolor.ps

      can result in better looking color. This requires gs 4.03.
      

What does the print filter do?
------------------------------

  If you have selected a 'Text-only' printer type, the filter simply
sends whatever you print straight to the printer.

  Otherwise, the print system is configured to generate PostScript
output from whatever type of file you print, if possible. An example
of where this conversion occurs is when you print a TeX .dvi file.
If you have dvips installed on your system, the .dvi file you printed
will automatically be converted into the PostScript format, then
sent to the printer. If your printer requires ghostscript to print
PostScript, ghostscript will be run automatically.

  Depending on what software you have installed, the print system
will attempt to convert the files you print into a format your
printer can handle. If the print system is unable to print the file,
it will instead print a diagnostic message to this effect.

  Also, the print system rejects printing files which normally you
wouldn't mean to print, like an ELF executable. These will be rejected
and a diagnostic page will be printed instead.

  If you would like to setup a printer queue which prints whatever is
sent to it without the print system attempting a conversion, setup a
separate queue without a print filter. You may like to call it
\"raw\", and to print to it you would use the command

lpr -Praw <filename>

" }

    pack .sf.b1 .sf.b2 .sf.b3 -side left -expand true -fill both  -ipady 1 \
	    -in .sf.f8 -anchor s

#
# stack printer list box and ok/canel buttons
#
    pack .sf.f1 -side top -expand yes -fill y -in .sf.cf3
    pack .sf.f8 -side top -expand yes -fill none -in .sf.cf3 -anchor s
#
# put printer description to the side and on top of res/paper size/color
#
    pack .sf.f7 -side bottom -fill x -in .sf.cf2
    pack .sf.f2 .sf.cf4 -side top -fill x -in .sf.cf2
    pack .sf.cf3 .sf.cf2 -side left -fill x -in .sf
    

#
#   select entry from auto_printerdb_entry if available
#

if [catch {set curdescr $printerdb_descr($auto_printerdb_entry)}] {
    set curent 0
} else {
    set curent [lsearch $existing_descr $curdescr]
}

    LOCAL_select_printer_type $curent .sf.f1.list .sf.f2.text .sf.f3.list .sf.f6.list .sf.f4.list $CBasc2ps 1

    wm deiconify .sf
    grab set .sf
    wm protocol .sf WM_DELETE_WINDOW { set trigger_2 0 }
    update
    scan [wm geometry .sf] "%d%*c%d" xmin ymin
    wm minsize .sf $xmin $ymin
    wm maxsize .sf $xmin $ymin
    tkwait variable trigger_2
    if {$trigger_2 == 1} {
	set pentry $printerdb_descr2entry([.sf.f1.list get [.sf.f1.list curselection]])
	set auto_printer $printerdb_GSDriver($pentry)
	set auto_printerdb_entry $pentry
	set auto_paper_size [lindex $paper_size_list [.sf.f4.list curselection]]
	set resindex [.sf.f3.list curselection]
	set resentry [lindex $printerdb_res($pentry) $resindex]
	set auto_resolution [format "%sx%s" [lindex $resentry 0] [lindex $resentry 1]]
#
#       get the color selection
#
	set colindex [.sf.f6.list curselection]
#
#       if this is default, then set accordingly
#
	set colentry [string trim [.sf.f6.list get $colindex]]
	set colentry [lindex [split $colentry] 0]
	if { [string compare $colentry "Default"]  } {
	    set colentry [lindex $printerdb_color($pentry) $colindex]
	    set auto_color [lindex $colentry 0]
	} else {
	    set auto_color "Default"
	}

#
# We handle eof more intellgently, depending on what kind of printer
# they have chosen
#

	set auto_sendeof $tmp_auto_sendeof

	# MLP: Don't know why this was originally only set when
	# the box was checked. Seems to prevent an unchecked box
	# value from being saved.
#	if {$tmp_auto_sendeof == "YES"} {
	    if {$auto_printer == "POSTSCRIPT"} {
		set auto_pseof $tmp_auto_sendeof
		set auto_texteof "NO"
	    } else {
		set auto_texteof $tmp_auto_sendeof
		set auto_pseof "NO"
	    }
#	}
	set tmp_auto_pseof $auto_pseof
	set tmp_auto_texteof $auto_texteof

# stairstep control
	set auto_crlf_trans $tmp_auto_crlf_trans
# fast printing - is a PostScript printer then it has to be OFF
	set auto_ascps_trans $tmp_auto_ascps_trans
	if { $auto_printer == "POSTSCRIPT" }  {
	    if {$tmp_auto_ascps_trans == "NO"}  {
		set auto_ascps_trans "NO"
		set tmp_auto_texteof "YES"
		set auto_texteof "YES"
	    } else {
		set auto_ascps_trans "YES"
	    }
	}
# reverse page order
	set auto_rev_pages $tmp_auto_rev_pages
# extra GS options
	set auto_GSextra [$EntGSextra get]
# margins
	set auto_rtlft_margin [$MarRtLftEnt get]
	set auto_topbot_margin [$MarTopBotEnt get]
# number pages per output page
	set auto_nup $tmp_auto_nup

#	puts "Selected printer entry -> $pentry"
#	puts "Selected GSDriver      -> $auto_printer"
#	puts "Selected printer entry -> $auto_printerdb_entry"
#	puts "Selected resolution    -> $auto_resolution"
#	puts "Selected color         -> $auto_color"
#	puts "Selected paper size    -> $auto_paper_size"
#	puts "Selected eof setting   -> $auto_eof"
#	puts "Selected cr/lf trans   -> $auto_crlf_trans"
#	puts "Selected reverse pages -> $auto_rev_pages"
#
#      this is REAL ugly, but we do it this way cause its how it was
#      put stuff into the select filter entry widget
#
	$e delete 0 end
	$e insert 0 $auto_flag
	$e insert end " - $auto_printerdb_entry"
    }
    destroy .sf
    return $trigger_2
}


proc LOCAL_updateentry {w i} {
    global printer_info
    global auto_printer auto_resolution auto_paper_size auto_flag auto_eof
    global auto_printerdb_entry auto_color auto_crlf_trans
    global suppress_headers

    set_printcap_field $i "lp=" [$w.v8 get]
    set filter [$w.v10 get]
    FILTERED_updateentry $i $filter
}

proc LOCAL_setup {i} {
    global auto_printer auto_resolution auto_paper_size auto_flag auto_eof
    global auto_printerdb_entry auto_color auto_crlf_trans
    global auto_rev_pages
    global desired_print_format
#
# nothing specific to a local printer to do, so call generic function
#
    FILTERED_setup $i
}

proc REMOTE_summaryline {i} { 
    set queue [printcap_field $i "rp=" "lp"]
    set host [printcap_field $i "rm=" "(unspecified host)"]
    return "REMOTE lpd queue $queue on $host"
}

proc REMOTE_name {} { return "Remote Unix (lpd) Queue" }

proc REMOTE_addpanel {w i} {

    frame $w.rhost
    frame $w.rqueue

    label $w.l5 -text "Remote Host" -anchor w
    entry $w.v5 -font fixed -relief sunken -borderwidth 2
    $w.v5 insert 0 [printcap_field $i "rm=" ""]

    label $w.l6 -text "Remote Queue" -anchor w
    entry $w.v6 -font fixed -relief sunken -borderwidth 2
    $w.v6 insert 0 [printcap_field $i "rp=" ""]

#    pack $w.l5 -pady 4 -side top -fill x -in $w.f1
#    pack $w.l6 -pady 3 -side top -fill x -in $w.f1
#    pack $w.v5 $w.v6 -side top -expand true -fill x -in $w.f2

    pack $w.l5  -side left -anchor w -fill x -in $w.rhost
    pack $w.v5  -side right -anchor e -fill x -in $w.rhost
    pack $w.l6  -side left  -anchor w -fill x -in $w.rqueue
    pack $w.v6  -side right  -anchor e -fill x -in $w.rqueue

    pack $w.rhost -side top -fill x -in $w.f1
    pack $w.rqueue -side top -fill x -in $w.f1

    FILTERED_addpanel $w $i
}

proc REMOTE_updateentry {w i} {
    set_printcap_field $i "rm=" [$w.v5 get]
    set_printcap_field $i "rp=" [$w.v6 get]
    set_printcap_field $i "lpd_bounce=" "true"
    set filter [$w.v10 get]

    FILTERED_updateentry $i $filter
}

proc REMOTE_setup {i} {
    FILTERED_setup $i
}


#
# Direct to printer port support added by Joshua Buysse
# Adapted from Miquel de Icaza's SMB support
# 
proc DIRECT_config {i} {
    global auto_direct_printer_ip auto_direct_port

    set spool_dir [printcap_field $i "sd=" "none"]
    set config_file "$spool_dir/.config"

    if {[string compare spool_dir none] == 0} {
        return [list "" ""]
    }

    set tmp_ip [printcap_field $i "printer_ip=" ""]
    set tmp_port [printcap_field $i "port=" ""]

    if { ($tmp_ip != "") && ($tmp_port != "") } {
	set direct_printer_ip "$tmp_ip"
	set direct_port "$tmp_port"
        return [list $direct_printer_ip $direct_port]
    } elseif [file exists $config_file] {
        set fd [open $config_file]      
        if {![regexp "printer_ip=\(.*\)" [gets $fd] dummy direct_printer_ip]} {
            set direct_printer_ip ""
        }

        if {![regexp "port=\(.*\)" [gets $fd] dummy direct_port]} {
             set direct_port ""
        }
        return [list $direct_printer_ip $direct_port]
    }
#    return [list "" ""]
    # MLP
    return [list $auto_direct_printer_ip $auto_direct_port]
}

proc DIRECT_summaryline {i} {
    set printer [FILTERED_summaryline $i "printer"]
    set config [DIRECT_config $i]
    set printer_ip  [lindex $config 0]
    set port [lindex $config 1]
    return "DIRECT - $printer at $printer_ip:$port"
}

proc DIRECT_name {} { return "Direct to port printer" }

proc DIRECT_addpanel {w i} {
    set config [DIRECT_config $i]

    frame $w.printer_ip
    frame $w.port

    label $w.l5 -text "Hostname of Printer" -anchor w
    entry $w.v5 -font fixed -relief sunken -borderwidth 2
    $w.v5 insert 0 [lindex $config 0]

    label $w.l6 -text "Port number" -anchor w
    entry $w.v6 -font fixed -relief sunken -borderwidth 2
    $w.v6 insert 0 [lindex $config 1]
 
    pack $w.l5 -side left -fill x -anchor w -in $w.printer_ip
    pack $w.v5 -side right -fill x -anchor e -in $w.printer_ip
    pack $w.l6 -side left -fill x -anchor w -in $w.port
    pack $w.v6 -side right -fill x -anchor e -in $w.port

    pack $w.printer_ip $w.port -side top -fill x -in $w.f1

    FILTERED_addpanel $w $i
}

proc DIRECT_updateentry {w i} {
    global direct_printer_ip direct_port

    set direct_printer_ip    [$w.v5 get]
    set direct_port         [$w.v6 get]
#
#   get the auto_filter setup first
#
#   it will be the input filter, so it sets the printcap entry 'if='
#
    set filter [$w.v10 get]
    FILTERED_updateentry $i $filter
}

proc DIRECT_setup {i} {
    global direct_printer_ip direct_port
    global auto_flag
    global libexecdir

#
#   now do related setup
#
#
    set spool_dir [printcap_field $i "sd=" ""]
    catch {exec mkdir -p $spool_dir}
    catch {exec chown root.lp $spool_dir}
    catch {exec chmod 775 $spool_dir}

    set direct_config "$spool_dir/.config"
    set_printcap_field $i "af=" "$spool_dir/acct"
    set_printcap_field $i "lp=" /dev/null
    set_printcap_field $i "sh" ""

    set_printcap_field $i "printer_ip=" "$direct_printer_ip"
    set_printcap_field $i "port=" "$direct_port"

    #catch {exec rm -f $direct_config}
    #set ffd [open $direct_config w]
    #puts $ffd "printer_ip=$direct_printer_ip"
    #puts $ffd "port=$direct_port"
    #close $ffd

#
#   see if any filter was specified, and if not, default
#   to a filter that will just send data to the printer
#
    set i_filter [printcap_field $i "if=" ""]
    if {$i_filter != $auto_flag} {
        set_printcap_field $i "if=" $libexecdir/directprint
    } else {
#
#       now setup the auto_filter
#
        FILTERED_setup $i
    }
}


#
# SMB support by Miquel de Icaza, adopted to printtool 3.0 by Michael Fulbright
#
# Added support in 3.1 for '-I' option of samba, by "Teddy"
proc SMB_config {i} {
    set spool_dir [printcap_field $i "sd=" "none"]
    set config_file "$spool_dir/.config"

    if {[string compare spool_dir none] == 0} {
 	return [list "" "" "" ""]
    }
    if [file exists $config_file] {
        set fd [open $config_file]	
        if {![regexp "share='\(.*\)'" [gets $fd] dummy sharename]} {
	    set sharename ""
	}
        if {![regexp "hostip=\(.*\)" [gets $fd] dummy hostip]} {
	    set hostip ""
        }
        if {![regexp "user='\(.*\)'" [gets $fd] dummy user]} {
 	     set user ""
        }
        if {![regexp "password='\(.*\)'" [gets $fd] dummy password]} {
 	     set password ""
        }
        if {![regexp "workgroup='\(.*\)'" [gets $fd] dummy workgroup]} {
 	     set workgroup ""
        }

        set translate no
	if {![regexp {\\\\(.*)\\(.*)} $sharename dummy hostname printername] } {
	    set hostname ""
	    set printername ""
	}
        return [list $hostname $printername $user $password $workgroup $hostip]        
    }
    return [list "" "" "" "" "" ""]
}

proc SMB_summaryline {i} {
    set printer [FILTERED_summaryline $i "printer"]
    set config [SMB_config $i]
    set hostname  [lindex $config 0]
    set printername [lindex $config 1]
    return "SMB - $printer on \\\\$hostname\\$printername"
}

proc SMB_name {} { return "SMB/Windows 95/98/NT Printer" }

proc SMB_addpanel {w i} {
    global smb_crlf

    set smb_crlf 0
    set config [SMB_config $i]

    frame $w.smbhost
    frame $w.smbip
    frame $w.smbname
    frame $w.smbuser
    frame $w.smbpasswd
	frame $w.smbworkgroup

    label $w.l5 -text "Hostname of Printer Server" -anchor w
    entry $w.v5 -font fixed -relief sunken -borderwidth 2
    $w.v5 insert 0 [lindex $config 0]

    label $w.l_hostip -text "IP number of Server (optional)" -anchor w
    entry $w.v6_hostip -font fixed -relief sunken -borderwidth 2
    $w.v6_hostip insert 0 [lindex $config 5]
 
    label $w.l6 -text "Printer Name" -anchor w
    entry $w.v6 -font fixed -relief sunken -borderwidth 2
    $w.v6 insert 0 [lindex $config 1]

    label $w.l7 -text "User" -anchor w
    entry $w.v7 -font fixed -relief sunken -borderwidth 2
    $w.v7 insert 0 [lindex $config 2]

    label $w.l8 -text "Password" -anchor w
    entry $w.v8 -show "*" -font fixed -relief sunken -borderwidth 2
    $w.v8 insert 0 [lindex $config 3]
	
	label $w.l9 -text "Workgroup" -anchor w
	entry $w.v9 -font fixed -relief sunken -borderwidth 2
	$w.v9 insert 0 [lindex $config 4]


#
# we'll handle cr/lf translation through the print filter if necessary
#
    set smb_crlf 0

#    pack $w.l5 -pady 4 -side top -fill x -in $w.f1
#    pack $w.l_hostip -pady 4 -side top -fill x -in $w.f1
#    pack $w.l6 -pady 3 -side top -fill x -in $w.f1
#    pack $w.l7 -pady 4 -side top -fill x -in $w.f1
#    pack $w.l8 -pady 4 -side top -fill x -in $w.f1

##    pack $w.v5 $w.v6 -side top -expand true -fill x -in $w.f2
#    pack $w.v5 $w.v6_hostip $w.v6 $w.v7 $w.v8 -side top -expand true -fill x -in $w.f2

    pack $w.l5 -side left -fill x -anchor w -in $w.smbhost
    pack $w.v5 -side right -fill x -anchor e -in $w.smbhost
    pack $w.l_hostip -side left -fill x -anchor w -in $w.smbip
    pack $w.v6_hostip -side right -fill x -anchor e -in $w.smbip
    pack $w.l6 -side left -fill x -anchor w -in $w.smbname
    pack $w.v6 -side right -fill x -anchor e -in $w.smbname
    pack $w.l7 -side left -fill x -anchor w -in $w.smbuser
    pack $w.v7 -side right -fill x -anchor e -in $w.smbuser
    pack $w.l8 -side left -fill x -anchor w -in $w.smbpasswd
    pack $w.v8 -side right -fill x -anchor e -in $w.smbpasswd
	pack $w.l9 -side left -fill x -anchor w -in $w.smbworkgroup
	pack $w.v9 -side right -fill x -anchor e -in $w.smbworkgroup

    pack $w.smbhost $w.smbip $w.smbname $w.smbuser $w.smbpasswd $w.smbworkgroup -side top -fill x -in $w.f1

    FILTERED_addpanel $w $i
}

proc SMB_updateentry {w i} {
global smb_share_name smb_hostip smb_password smb_user smb_workgroup smb_crlf

    set smb_share_name "\\\\[$w.v5 get]\\[$w.v6 get]"
    set smb_hostip    [$w.v6_hostip get]
    set smb_user   [$w.v7 get]
    set smb_password   [$w.v8 get]
	set smb_workgroup [$w.v9 get]
#
#   get the auto_filter setup first
#
#   it will be the input filter, so it sets the printcap entry 'if='
#
    set filter [$w.v10 get]
    FILTERED_updateentry $i $filter

}

proc SMB_setup {i} {
    global smb_share_name smb_hostip smb_password smb_user smb_workgroup smb_crlf
    global auto_flag
    global libexecdir

#
#   now do smbprint related setup
#
#
    set spool_dir [printcap_field $i "sd=" ""]
    catch {exec mkdir -p $spool_dir}
    catch {exec chown root.lp $spool_dir}
    catch {exec chmod 775 $spool_dir}

    set smb_config "$spool_dir/.config"
    set_printcap_field $i "af=" "$spool_dir/acct"
    set_printcap_field $i "lp=" /dev/null
    set_printcap_field $i "sh" ""
    catch {exec rm -f $smb_config}

    set ffd [open $smb_config w]
    if {$smb_crlf} { set translate yes } else { set translate no }
    puts $ffd "share='$smb_share_name'"
    puts $ffd "hostip=$smb_hostip"
    puts $ffd "user='$smb_user'"
    puts $ffd "password='$smb_password'"
	puts $ffd "workgroup='$smb_workgroup'"
    close $ffd
    catch { exec chown root.lp $smb_config }
    catch { exec chmod 0640 $smb_config }

#
#   see if any filter was specified, and if not, default
#   to a filter that will just send data to remote SMB server
#
    set i_filter [printcap_field $i "if=" ""]
    if {$i_filter != $auto_flag} {
	set_printcap_field $i "if=" $libexecdir/smbprint
    } else {
#
#       now setup the auto_filter
#
	FILTERED_setup $i
    }
}


#
# NCP support by Fernando Lozano from BL Informtica Ltda. at Rio de
# Janeiro, Brazil, adopted to printtool by Bill Nottingham
#
proc NCP_config {i} {
    set spool_dir [printcap_field $i "sd=" "none"]
    set config_file "$spool_dir/.config"

    if {[string compare spool_dir none] == 0} {
 	return [list "" "" "" ""]
    }
    if [file exists $config_file] {
        set fd [open $config_file]	
        if {![regexp "server=\(.*\)" [gets $fd] dummy server]} {
	    set server ""
	}
        if {![regexp "queue=\(.*\)" [gets $fd] dummy queue]} {
	    set queue ""
        }
        if {![regexp "user=\(.*\)" [gets $fd] dummy user]} {
 	     set user ""
        }
        if {![regexp "password=\(.*\)" [gets $fd] dummy password]} {
 	     set password ""
        }

        set translate no
        return [list $server $queue $user $password]        
    }
    return [list "" "" "" ""]
}

proc NCP_summaryline {i} {
    set printer [FILTERED_summaryline $i "printer"]
    set config [NCP_config $i]
    set servername  [lindex $config 0]
    set queuename [lindex $config 1]
    return "NCP - $printer on server $servername, queue $queuename"
}

proc NCP_name {} { return "NetWare Printer (NCP)" }

proc NCP_addpanel {w i} {
    global ncp_crlf

    set ncp_crlf 0
    set config [NCP_config $i]

    frame $w.ncpserver
    frame $w.ncpqueue
    frame $w.ncpuser
    frame $w.ncppasswd

    label $w.l5 -text "Printer Server Name" -anchor w
    entry $w.v5 -font fixed -relief sunken -borderwidth 2
    $w.v5 insert 0 [lindex $config 0]

    label $w.l6 -text "Print Queue Name" -anchor w
    entry $w.v6 -font fixed -relief sunken -borderwidth 2
    $w.v6 insert 0 [lindex $config 1]

    label $w.l7 -text "User" -anchor w
    entry $w.v7 -font fixed -relief sunken -borderwidth 2
    $w.v7 insert 0 [lindex $config 2]

    label $w.l8 -text "Password" -anchor w
    entry $w.v8 -show "*" -font fixed -relief sunken -borderwidth 2
    $w.v8 insert 0 [lindex $config 3]


#
# we'll handle cr/lf translation through the print filter if necessary
#
    set ncp_crlf 0

#    pack $w.l5 -pady 4 -side top -fill x -in $w.f1
#    pack $w.l_hostip -pady 4 -side top -fill x -in $w.f1
#    pack $w.l6 -pady 3 -side top -fill x -in $w.f1
#    pack $w.l7 -pady 4 -side top -fill x -in $w.f1
#    pack $w.l8 -pady 4 -side top -fill x -in $w.f1

##    pack $w.v5 $w.v6 -side top -expand true -fill x -in $w.f2
#    pack $w.v5 $w.v6_hostip $w.v6 $w.v7 $w.v8 -side top -expand true -fill x -in $w.f2

    pack $w.l5 -side left -fill x -anchor w -in $w.ncpserver
    pack $w.v5 -side right -fill x -anchor e -in $w.ncpserver
    pack $w.l6 -side left -fill x -anchor w -in $w.ncpqueue
    pack $w.v6 -side right -fill x -anchor e -in $w.ncpqueue
    pack $w.l7 -side left -fill x -anchor w -in $w.ncpuser
    pack $w.v7 -side right -fill x -anchor e -in $w.ncpuser
    pack $w.l8 -side left -fill x -anchor w -in $w.ncppasswd
    pack $w.v8 -side right -fill x -anchor e -in $w.ncppasswd

    pack $w.ncpserver $w.ncpqueue $w.ncpuser $w.ncppasswd -side top -fill x -in $w.f1

    FILTERED_addpanel $w $i
}

proc NCP_updateentry {w i} {
global ncp_server ncp_queue ncp_password ncp_user ncp_crlf

    set ncp_server [$w.v5 get]
    set ncp_queue    [$w.v6 get]
    set ncp_user   [$w.v7 get]
    set ncp_password   [$w.v8 get]
#
#   get the auto_filter setup first
#
#   it will be the input filter, so it sets the printcap entry 'if='
#
    set filter [$w.v10 get]
    FILTERED_updateentry $i $filter

}

proc NCP_setup {i} {
    global ncp_server ncp_queue ncp_password ncp_user ncp_crlf
    global auto_flag
    global libexecdir

#
#   now do ncpprint related setup
#
#
    set spool_dir [printcap_field $i "sd=" ""]
    catch {exec mkdir -p $spool_dir}
    catch {exec chown root.lp $spool_dir}
    catch {exec chmod 775 $spool_dir}

    set ncp_config "$spool_dir/.config"
    set_printcap_field $i "af=" "$spool_dir/acct"
    set_printcap_field $i "lp=" /dev/null
    set_printcap_field $i "sh" ""
    catch {exec rm -f $ncp_config}

    set ffd [open $ncp_config w]
    if {$ncp_crlf} { set translate yes } else { set translate no }
    puts $ffd "server=$ncp_server"
    puts $ffd "queue=$ncp_queue"
    puts $ffd "user=$ncp_user"
    puts $ffd "password=$ncp_password"
    close $ffd

#
#   see if any filter was specified, and if not, default
#   to a filter that will just send data to remote NCP server
#
    set i_filter [printcap_field $i "if=" ""]
    if {$i_filter != $auto_flag} {
	set_printcap_field $i "if=" $libexecdir/ncpprint
    } else {
#
#       now setup the auto_filter
#
	FILTERED_setup $i
    }
}

proc write_printcap {} {
    global print_count printer_comments printer_entry
    global printer_info printer_type
    global delete_index

    set fd [open "/etc/printcap" w]

    for {set i 0} {$i < $print_count} {incr i} {
	if {$i == 0 || $i != $delete_index} {
	    puts -nonewline $fd $printer_comments($i)
	}
	if {$i != $delete_index} {
	    if {$printer_type($i) != "UNKNOWN"} {
		puts $fd "\#\#PRINTTOOL3\#\# $printer_type($i) $printer_info($i)"
	    }
	    # De-canonicalize entry
	    set entry $printer_entry($i)
	    regsub -all ":" $printer_entry($i) ":\\\n\t:" entry
	    regsub "\\\\\n\t:\$" $entry "\n" entry
	    puts -nonewline $fd $entry
	}
    }
    puts -nonewline $fd $printer_comments($i)

    close $fd
    set delete_index ""

    # NickM
    # run reload to make lpd snarf the printcap (necessary for lprng)


	#this is a pretty clumsy check to see if we're in lprng-land
	#if {[file executable "/usr/sbin/checkpc"] == 1} {
	    #if {[file executable "/etc/rc.d/init.d/lprng"] == 1} {
		## Imperfect check for user-installed lprng (rh6.x, etc).
		#catch {exec /etc/rc.d/init.d/lprng reload}
	    #} elseif {[file executable "/etc/init.d/lpr"] == 1} {
		## This is a check for debian or any other FHS-compliant system
		## running lprng.
		#catch {exec /etc/init.d/lpr reload}
	    #} else {
		# This is a check for standard lprng install (RH7.x, etc.)
		#catch {exec /etc/rc.d/init.d/lpd reload}
	    #}
	#}

    # -NickM
}

proc sync {} {
    write_printcap
    # If this is an LPRng system, run checkpc to
    # take care of spool directory issues.
    #if {([file executable "/usr/sbin/checkpc"] == 1)} {
	#catch {exec /usr/sbin/checkpc -f}
    #}

    reload
    redisplay
}

proc get_selected_index {} {
    return [.list curselection]
}

proc edit_entry {i} {
    global print_count printer_names printer_entry printer_type printer_type_list
    global trigger
    global current_printer_type

    set current_printer_type $printer_type($i)

    if {$printer_type($i) == "UNKNOWN"} {
	rhs_error_dialog \
"This printer was not created by the 
Print System Manager.  You can remove 
it using this tool but not edit it."
	return 0
    } elseif {$printer_type($i) == "NCP"} {
	rhs_info_dialog \
"               - WARNING -

    The use of a remote NetWare printer 
    may require a username and password for
    its server. These are stored unencrypted
    in a script locally, and must be passed on
    the command line to the nprint program.
            
    In other words, it is possible for 
    another person to learn of the username
    and password. It is therefore recommended
    that the username and password for the
    use of the printer to NOT BE the same as
    that for a user account on the local 
    Linux box. That way, if this information
    is compromised, the only possible damage
    is unauthorized use of the printer.

    If there are file shares from the server,
    it would be best if they required a 
    different password than that required for
    the use of the printer, for the same reason.
"
    } elseif {$printer_type($i) == "SMB"} {
	rhs_info_dialog \
"               - WARNING -

    The use of a remote SMB/Windows 95/NT printer 
    may require a username and password for
    its server. These are stored unencrypted
    in a script locally, and must be passed on
    the command line to the smbclient program.
            
    In other words, it is possible for 
    another person to learn of the username
    and password. It is therefore recommended
    that the username and password for the
    use of the printer to NOT BE the same as
    that for a user account on the local 
    Linux box. That way, if this information
    is compromised, the only possible damage
    is unauthorized use of the printer.

    If there are file shares from the server,
    it would be best if they required a 
    different password than that required for
    the use of the printer, for the same reason.
"
    }

    toplevel .e
    wm withdraw .e
    wm transient .e .
    wm group .e .
    wm title .e "Edit [$printer_type($i)_name] Entry"

    frame .e.f1
#    frame .e.f2
    frame .e.f3

    frame .e.name
    frame .e.spool
    frame .e.filelen

    set traversal_list ""

    label .e.l1 -text "Names (name1|name2|...)"
    entry .e.v1 -font fixed -relief sunken -borderwidth 2
    .e.v1 insert 0 $printer_names($i)
    lappend traversal_list .e.v1

#
# make it so first queue name is copied to tail of spool dir if
# a CR is pressed in the queue name field
#
    bind .e.v1 <Return> {
        set CR_queue_name [lindex [split [.e.v1 get] |] 0]
	set CR_spool_dir [.e.v2 get]
	if {$CR_spool_dir != ""} {
	    set CR_spool_dir [string range $CR_spool_dir 0 \
                                      [string last "/" $CR_spool_dir]]
	    append CR_spool_dir $CR_queue_name
	    .e.v2 delete 0 end
	    .e.v2 insert 0 $CR_spool_dir
	}
    }
#
# ugly, but do it like this for now
#
    bind .e.v1 <Tab> {
        set CR_queue_name [lindex [split [.e.v1 get] |] 0]
	set CR_spool_dir [.e.v2 get]
	if {$CR_spool_dir != ""} {
	    set CR_spool_dir [string range $CR_spool_dir 0 \
                                      [string last "/" $CR_spool_dir]]
	    append CR_spool_dir $CR_queue_name
	    .e.v2 delete 0 end
	    .e.v2 insert 0 $CR_spool_dir
	}
    }



    label .e.l2 -text "Spool Directory"
    entry .e.v2 -font fixed -relief sunken -borderwidth 2
    .e.v2 insert 0 [printcap_field $i "sd=" ""]
    lappend traversal_list .e.v2

    label .e.l3 -text "File Limit in Kb (0 = no limit)" -anchor w
    entry .e.v3 -font fixed -relief sunken -borderwidth 2
    .e.v3 insert 0 [printcap_field $i "mx#" "0"]
    lappend traversal_list .e.v3

#    pack .e.l1 -pady 3 -side top -fill x -in .e.f1
#    pack .e.l2 -pady 4 -side top -fill x -in .e.f1
#    pack .e.l3 -pady 3 -side top -fill x -in .e.f1
#    pack .e.v1 .e.v2 .e.v3 -pady 0 -side top -expand true -fill x -in .e.f2

    pack .e.l1 -side left -anchor w -in .e.name
    pack .e.v1 -side right -fill x -anchor e -in .e.name

    pack .e.l2 -side left -anchor w -in .e.spool
    pack .e.v2 -side right -fill x -anchor e -in .e.spool

    pack .e.l3 -side left -in .e.filelen
    pack .e.v3 -side right -fill x -in .e.filelen

    pack .e.name -fill x -side top -in .e.f1
    pack .e.spool -fill x -side top -in .e.f1
    pack .e.filelen -fill x -side top -in .e.f1


    $printer_type($i)_addpanel .e $i

    frame .e.buttons
    button .e.b1 -text "OK" -width 10 -command "set trigger 1"
    pack .e.b1 -side left -expand true -ipady 1 -in .e.buttons
    button .e.b2 -text "Cancel" -width 10 -command "set trigger 0"
    pack .e.b2 -side left -expand true -ipady 1 -in .e.buttons

    pack .e.buttons -side bottom -expand true -fill x -padx 4 -pady 4 -in .e
    pack .e.f3 -side bottom -expand true -fill x

#    pack .e.f1 -side left -fill both -padx 2 -pady 1 -in .e
#    pack .e.f2 -side left -expand true -fill both -padx 2 -pady 1 -in .e

    pack .e.f1 -side top -fill both -padx 2 -pady 1 -in .e

    center_dialog .e
    grab set .e
    update
    scan [wm geometry .e] "%d%*c%d" xmin ymin
    wm minsize .e $xmin $ymin
    wm maxsize .e 10000 $ymin

    set inp_err 1

    while {$inp_err == 1} {
	set inp_err 0
	tkwait variable trigger

	if {$trigger == 1} {
	    # MLP: Do some sanity checking. Force the user
	    # to enter both the remote host and remote queue, etc.
	    # The printcap entry woon't work otherwise.
	    if {[.e.v1 get] == ""} {
		rhs_error_dialog "The Names field requires an entry."
		set inp_err 1
	    } elseif {[.e.v2 get] == ""} {
		rhs_error_dialog "The Spool Directory field requires an entry."
		set inp_err 1
	    } elseif {[.e.v3 get] == ""} {
		rhs_error_dialog "The File Limit field requires an entry."
		set inp_err 1
	    } elseif {$printer_type($i) == "REMOTE"} {
		set fieldname ""
		if {[.e.v5 get] == ""} {
		    set fieldname "Remote Host"
		} elseif {[.e.v6 get] == ""} {
		    set fieldname "Remote Queue"
		}
		if {$fieldname != ""} {
		    rhs_error_dialog "The $fieldname field requires an entry."
		    set inp_err 1
		}
	    } elseif {$printer_type($i) == "DIRECT"} {
		set fieldname ""
		if {[.e.v5 get] == ""} {
		    set fieldname "Hostname"
		} elseif {[.e.v6 get] == ""} {
		    set fieldname "Port Number"
		}
		if {$fieldname != ""} {
		    rhs_error_dialog "The $fieldname field requires an entry."
		    set inp_err 1
		}
	    } elseif {$printer_type($i) == "LOCAL"} {
		if {[.e.v8 get] == ""} {
		    rhs_error_dialog "The Printer Device field requires an entry."
		    set inp_err 1
		}
	    }

	    if {$inp_err == 0} {
		set printer_names($i) [.e.v1 get]
		regsub {^[^:]*:} $printer_entry($i) "[.e.v1 get]:" printer_entry($i)
		regsub -all {[ ]} [.e.v2 get] "_" tmp  
		
		set_printcap_field $i "sd=" $tmp
		set_printcap_field $i "mx#" [.e.v3 get]

		$printer_type($i)_updateentry .e $i
	    }
	}
    }

    destroy .e
    return $trigger
}

proc edit_entry_aux {i} {
    global printer_type printer_names

    set res [edit_entry $i]

    if {$res == 1} {
	# make spool directory
	set spool_dir [printcap_field $i "sd=" ""]
	catch {exec mkdir -p $spool_dir}
	catch {exec chown root.lp $spool_dir}
	catch {exec chmod 775 $spool_dir}

	# do type-specific setup
	$printer_type($i)_setup $i

	sync

# NickM
	if {[file executable "/usr/sbin/checkpc"] == 1} {
	    set queue [string trim [lindex [split $printer_names($i) "|"] 0]]
	    catch {exec /usr/sbin/checkpc -fP $queue}
	    #catch {exec /usr/sbin/checkpc -f}
	    reload_lpd


	}
# end NickM
    }

    return $res
}

proc printer_type_dialog {} {
    global type_name trigger_3
    global printer_type_list
    
    # MJC
    # display list of printer types

    .list selection clear 0 end

    toplevel .pt
    wm withdraw .pt
    wm transient .pt .
    wm group .pt .
    wm title .pt "Add a Printer Entry"

    label .pt.l1 -text "Printer type:" -anchor w
    pack .pt.l1 -side top
    foreach t $printer_type_list {
	set readabletypeproc ${t}_name
	radiobutton .pt.t_$t -text [$readabletypeproc] -variable type_name \
	    -value $t -anchor w 
	pack .pt.t_$t -side top -fill x -padx 2
    }
    set type_name [lindex $printer_type_list 0]

    frame .pt.buttons
    button .pt.ok -text "OK" -width 10 -command "set trigger_3 1"
    pack .pt.ok -side left -expand true -ipady 1 -pady 3 -in .pt.buttons
    button .pt.cancel -text "Cancel" -width 10 -command "set trigger_3 0"
    pack .pt.cancel -side left -expand true -ipady 1 -pady 3 -in .pt.buttons

    pack .pt.buttons -side top

    center_dialog .pt
    grab set .pt
    update
    scan [wm geometry .pt] "%d%*c%d" xmin ymin
    wm minsize .pt $xmin $ymin
    wm maxsize .pt 10000 $ymin
    set trigger_3 0
    tkwait variable trigger_3

    destroy .pt

    if {$trigger_3 == 1} {
	return $type_name
    }

    return {}
}
   

## End of random functions
##############################################
## @@ User Interface callback functions

proc help_dialog { title msg } {
    global trigger_help


    toplevel .hf
    wm withdraw .hf
    wm group .hf .
    wm title .hf $title

    frame .hf.helpf
    text .hf.helpf.msg -relief sunken -setgrid true -wrap word \
	          -width 60 -height 24 -yscrollcommand ".hf.helpf.msg_sy set" \
                  -font -adobe-helvetica-medium-r-normal--14*
    scrollbar .hf.helpf.msg_sy -orient vert -command ".hf.helpf.msg yview"
    .hf.helpf.msg insert 1.0 $msg
    .hf.helpf.msg config -state disabled
    pack .hf.helpf.msg_sy -side right -fill y -in .hf.helpf
    pack .hf.helpf.msg    -side left -fill both -expand true -in .hf.helpf
    
    frame .hf.okframe
    button .hf.b1 -text "OK" -width 10 -command "set trigger_help 1"
    pack .hf.b1 -side left -expand true -ipady 1 -in .hf.okframe

    pack .hf.helpf   -side top -expand true -fill both -in .hf
    pack .hf.okframe -side bottom -in .hf

#    center_dialog .hf
    wm deiconify .hf
    grab set .hf
    wm protocol .hf WM_DELETE_WINDOW { set trigger_help 0 }
    update
    scan [wm geometry .hf] "%d%*c%d" xmin ymin
    wm minsize .hf $xmin $ymin

    tkwait variable trigger_help
    destroy .hf
}


proc menu_genhelp {} {

    help_dialog "General Help" "
General Help
------------

In order to create a new print queue, select 'ADD',
or, to have Printtool attempt to detect local or 
remote printers, select AUTODETECT.

Then it is necessary to select what type of printer is being added.

There are five types of printers which can be configured with printtool:  LOCAL, REMOTE, SMB, NCP, and DIRECT.


  LOCAL printers are attached to the printer port
  of the local machine. 

  REMOTE printers are attached to another machine 
  which has an lpd daemon running. Usually this 
  will be another Linux machine.

  SMB printers are attached to another machine 
  which is an SMB server. Use this to print to
  printers on a machine running Windows or the
  samba software.
  
  NCP printers are attached to another machine
  which is an NCP server. Use this to print to
  printers on a machine running NetWare or the
  mars_nwe software.

  DIRECT printers are attached directly to the 
  network, and leave a port (typically 9100)
  open. By sending data directly to this port
  you can print. Old HP JetDirect cards along
  with some old printer sharing devices use
  this protocol.


After choosing the printer type, the following information is required:

  Queue name: What the queue will be called. Multiple names
              can be specified with the '|' character
              separating entries.

  Spool Directory: This is the directory on the local machine
              where files are stored before printing occurs.
              Be careful to NOT have more than one printer queue
              use a given spool directory.

  File Limit: Maximum size print job accepted. A size of 0 
              indicates no limit should be imposed.

For LOCAL printers, the following are also required:

  Printer Device: Usually /dev/lp1, the name of the port which
                  the printer is attached to. Serial printers
                  are usually on /dev/ttyS? ports. You will need
                  to manually configure serial parameters.

  Input Filter: Filters convert printed files into a format
                the printer can handle. Choose the filter
                which best matches your printer.

For REMOTE printers, the following are also required:

   Remote Host: Hostname of the remote machine hosting the printer.
 
   Remote Queue: Printer queue name of printer on remote machine.

The remote machine must be configured to allow the local machine to print on the desired queue. Typically hosts.lpd controls this.

For SMB printers, the following are also required:

   Printer Server Name: Name of the machine to which the printer you want
                        to use is attached.

   IP number of Server: Using this parameter will force the client to assume
                        that the server is on the machine with the specified IP
                        number, instead of determining it automatically.

   Printer Name: Name of the printer on which you want to print.

   Printer User: Name of user you must login as to access the printer.
 
   Printer Password: Password (if required) to use the printer.
                     Someone should be able to tell you this if
                     you do not already know it.
  
For NCP printers, the following are also required:

   Printer Server Name: Name of the machine to which the printer you want
                        to use is attached.

   Printer Name: Name of the printer on which you want to print.

   Printer User: Name of user you must login as to access the printer.
 
   Printer Password: Password (if required) to use the printer.
                     Someone should be able to tell you this if
                     you do not already know it.

For DIRECT printers, the following are also required:

   Printer IP address: Hostname of the remote printer.
 
   Port: Port number on the printer (typically 9100).

"
}


proc menu_trouble {} {

    help_dialog "Troubleshooting" "
Troubleshooting
---------------

First try printing a test page from under the \"Tests\":

    Choosing \"Print ASCII directly to port\" sends text straight
    to the printer, without using lpr. This will only work if your
    printer is attached to a printer port on the local machine. It
    will not work if your printer is configured as a REMOTE or SMB
    printer. 

    You should at least see an indicator light on your printer blink
    if your printer port is configured correctly. If this doesn't 
    work, check your cabling and printer device specification. 
    When adding a new printer, printtool tries to detect the port
    the printer is attached to. In case this detection failed, try
    changing the printer device to /dev/lp0, /dev/lp1, and /dev/lp2.
    These are the most common ports on PC hardware.

More complex test pages are available as well, but these use lpr to print.

    If your printer is TEXT-ONLY, choose \"Print ASCII test page\".

    Otherwise try \"Print PostScript test page\".

If ASCII seems to print and PostScript does not, check that you have the correct filter configured for your printer. If your printer model does not appear in the possible choices, try picking a similar model printer. If this fails, see if your printer is compatible with another printer in the available choices. Many printers, for example, are compatible with HP printers. Your printer manual should contain this information.

The \"Suppress Headers\" option sometimes causes trouble if it is off. Try turning it on if you get undesired banner pages, or if your printer appears to accept data and then not print anything.

For text-only printers, if you get stair-stepped text, try turning on the \"LF->CR/LF\" option.

For all printers, if your printer appears to accept data (some printers have a light which blinks when its accepting data), and nothing prints, try enabling the \"Send EOF\" option.

If you get an extra page at the end of every job, try turning off the \"Send EOF\" option.

For JetDirect boxes, be sure to send non-text output to the 'raw' queue on the box.
"
}

proc menu_about {} {
    global version

    rhs_info_dialog "
Red Hat Software Printtool Version $version

This tool allows you to setup printers easily.

The default printer is the first one listed.  To change
this you will have to edit /etc/printcap and move the
appropriate entry to the head of the list.

Once the printer queue and print filter are setup, it
is possible to print a variety of file types to the
queue and the filter will automatically convert it to
the format required by the printer. At the moment all
file types are converted to PostScript, and then printed
either straight to a PostScript printer, or run through
ghostscript if the printer does not handle PostScript
natively.

The printer can also be configured as 'Text-only', in which
case no conversion is attempted. The input text is echoed
straight to the printer. It is possible to do LF->CR/LF
conversion to correct stair-stepping, and to send a form feed
to eject the page at the end of a job.

If you want to print PostScript and do not have a PostScript
capable printer, make sure ghostscript is installed.

(This version was modified by VA Linux Systems
to add printer autodetection.)
"
}

#-------------------------------------------------------
# MLP: Get the first available spooldir of pattern lp[0-9]+,
# where name does not conflict with any existing 
# $printer_names.
#-------------------------------------------------------
proc get_next_unused_qname {} {
    global printer_names

    set num 0
    set done 0
    while {$done == 0} {
	set done 1
	set lpx "lp$num"
	if { [file isdirectory "/var/spool/lpd/$lpx"] == 0 } {
	    foreach i [array names printer_names] {
		if {[regexp $lpx $printer_names($i)] == 1} {
		    set done 0
		    break
		}
	    }
	} else {
	    set done 0
	}
	incr num
    }
    return $lpx
}

proc button_add {} {
    global print_count printer_names printer_entry printer_comments
    global printer_info printer_type

    set type [printer_type_dialog]
    # user may have selected "Cancel"
    if {$type == ""} {
	return
    }

    # First find a candidate lpX
    # (Make first printer added "lp")
    set done 0
    set num 0
    if {$print_count == 0} {
	set lpx "lp"
	set done 1
    }

    # MLP:
    if {$done == 0} {
	set lpx [get_next_unused_qname]
    }

    set i $print_count
    incr print_count

    set printer_names($i) "$lpx"
    set printer_type($i) $type
    set printer_info($i) {}
    set printer_entry($i) "$lpx:sd=/var/spool/lpd/$lpx:mx#0:"
    set printer_comments($print_count) ""
#
#   default to suppress headers on
#
    set_printcap_switch $i "sh" 1
    
    if {[edit_entry_aux $i] != 1} {
	incr print_count -1
    }
}

proc button_delete {} {
    global printer_names spool_dir delete_index

    set i [.list curselection]
    if {$i == ""} {
	rhs_error_dialog "No Printer Selected"
	return
    }

    set res [rhs_continue_dialog "Delete $printer_names($i) from /etc/printcap?"]
    if {$res == 1} {
	return
    }

    set spool_dir [printcap_field $i "sd=" ""]
    if {$spool_dir != ""} {
	rhs_info_dialog "Please remember to remove the spool directory:\n\n$spool_dir"
    }

    set delete_index $i
    # sync writes /etc/printcap first, so this will do
    sync
}

proc button_edit {} {
    set i [get_selected_index]
    if {$i == ""} {
	return
    }

    edit_entry_aux $i
}

proc menu_restart_lpd {} {
    if {[file executable "/etc/rc.d/init.d/lprng"] == 1} {
	# Imperfect check for lprng.
	catch {exec /etc/rc.d/init.d/lprng restart}
    } elseif {[file executable "/etc/init.d/lpr"] == 1} {
	# This is a check for debian running lprng.
	catch {exec /etc/init.d/lpr stop}
	catch {exec /etc/init.d/lpr start}
    } else {
	# This is a check for standard lpr (RH6.x, etc.)
	catch {exec /etc/rc.d/init.d/lpd stop}
	catch {exec /etc/rc.d/init.d/lpd start}
    }
}

proc reload_lpd {} {
    if {[file executable "/etc/rc.d/init.d/lprng"] == 1} {
	# Imperfect check for lprng.
	catch {exec /etc/rc.d/init.d/lprng reload}
    } elseif {[file executable "/etc/init.d/lpr"] == 1} {
	# This is a check for debian running lprng.
	catch {exec /etc/init.d/lpr reload}
    } else {
	# This is a check for standard lpr (RH6.x, etc.)
	catch {exec /etc/rc.d/init.d/lpd reload}
    }
}

proc menu_quit {} {
    # We could sync here but we should always be synced
    exit 0
}

proc menu_reload {} {
    global selected_hint

    .list delete 0 end
    update idletasks

    # We don't need to (and should not) write out printcap
    # since we should always be synced, and the goal
    # is really to pick up any external changes

    reload
    redisplay
}

#
# print the ascii test page
#
proc print_ascii_testpage {} {
    global printer_names
    global filtersrcdir

    set i [get_selected_index]
    if {$i == ""} {
        rhs_error_dialog "Please highlight a printer first"
	return
    }

    set queue [string trim [lindex [split $printer_names($i) "|"] 0]]

    catch {exec lpr -P$queue $filtersrcdir/testpage.asc} result
    if { $result != "" } {
	rhs_error_dialog "Error printing test page to queue $queue

Error reason: $result"
	return
    } else {
	rhs_info_dialog "Test page printed to queue $queue"
    }
}

#
# print the ascii test page directly to printer port
#
proc print_ascii_direct_testpage {} {
    global printer_names
    global printer_type

    set i [get_selected_index]
    if {$i == ""} {
        rhs_error_dialog "Please highlight a printer first"
	return
    }

    if {$printer_type($i) != "LOCAL"} {
	rhs_error_dialog "Can only print directly to a LOCAL printer"
	return
    }

    set port [printcap_field $i {lp=} {unset}]

    if {$port == "unset"} {
	rhs_error_dialog "Please set the printer device for this printer first"
	return
    }

    catch {exec printf "This text should appear on the printer on $port\014" > $port} result
    if { $result != "" } {
	rhs_error_dialog "Error printing test page to queue $port

Error reason: $result"
	return
    } else {
	rhs_info_dialog "Test page printed to port $port"
    }
}

#
# print the PS test page
#
proc print_ps_testpage {} {
    global printer_names printer_type printer_info
    global filtersrcdir

    set i [get_selected_index]
    if {$i == ""} {
        rhs_error_dialog "Please highlight a printer first"
	return
    }

    set queue [string trim [lindex [split $printer_names($i) "|"] 0]]

#
#   make sure that we should be printing PS to this queue
#    
    set sumline [$printer_type($i)_summaryline $i]
    if { $sumline == "" } {
	puts "Something is really wrong in print_ps_testpage"
	return
    }
    set prn_type [string trim [lindex [split $sumline] 0]]
    set paper_type [string trim [lindex [split $printer_info($i)] 2]]

    if { $prn_type == "type unrecognized" || $prn_type == "Text-only" } {
	rhs_error_dialog "You have tried to print a PostScript document
to a printer which you have configured to
have no PostScript support. This will probably
not do what you want. Try printing an ASCII
test page instead..."
        return
    }

    if { $paper_type == "a4" } {
	catch {exec lpr -P$queue $filtersrcdir/testpage-a4.ps} result
    } else {
	catch {exec lpr -P$queue $filtersrcdir/testpage.ps} result
    }

    if { $result != "" } {
	rhs_error_dialog "Error printing test page to queue $queue

Error reason: $result"
	return
    } else {
	rhs_info_dialog "Test page printed to queue $queue"
    }

}

proc reload_printerdb2 {} {
#----------------------------------------------------
#----------------------------------------------------
    global printerdb_entry 
    global printerdb_descr
    global printerdb_about 
    global printerdb_GSDriver
    global printerdb_res   
    global printerdb_color

    global printerdb_count
    global printerdb_descr2entry
    global printerdb_curdbline
    global filtersrcdir

    set tsv_state "TSV_START"
#
#   counter to let us know where printerdb integrity errors occur
#
    set printerdb_curdbline 0
    set printerdb_debug 0
    set printerdb_count 0
    if {[catch {set fd [open "$filtersrcdir/printers.tsv" r]}] == 1} {
	# there is no printerdb
	puts "Error cannot find ./printers.tsv!"
	return
    }

    # Skip initial comments:
    while {[gets $fd s] != "-1"} {
	# strip spaces at the front
	set cp [string trimleft $s]

	# if not a comment line then break?
	if {[regexp "^\#" $cp] != 1} {
	    break
	}
    }

    set i 0
    while { $tsv_state != "TSV_END" && $s != "-1" }  {

	set pfields [split $s "\t"]
	regsub -all " " [lindex $pfields 1] "" nextname

#	puts "#$i $nextname [lindex $pfields 0]|[lindex $pfields 1] | [lindex $pfields 7]x[lindex $pfields 7]"

	set printerdb_entry($i) $nextname
	incr i

	set printerdb_descr($nextname) "[lindex $pfields 0] [lindex $pfields 1]"
	set printerdb_descr2entry($printerdb_descr($nextname)) $nextname

	if {[string toupper [lindex $pfields 5]] == "N/A" && 
	[string toupper [lindex $pfields 6]] == "POSTSCRIPT"} {
	    set printerdb_GSDriver($nextname) "POSTSCRIPT"
	} else {
	    set printerdb_GSDriver($nextname) [lindex $pfields 5]
	}

	set printerdb_about($nextname) "Driver for [lindex $pfields 0] [lindex $pfields 1].\nDriver Type: $printerdb_GSDriver($nextname)"

	if {[lindex $pfields 7] != ""} {
	    lappend printerdb_res($nextname) "[lindex $pfields 7]  [lindex $pfields 8] { }"
	}

	set s [gets $fd]

	# strip spaces at the front
	set cp [string trimleft $s]

	# if not a comment line then break?
	if {[regexp "^\#section:" $cp] == 1 || [string length $s] <= 2} {
	    set tsv_state "TSV_END"
	}
    }

    set printerdb_count $i
}

## End of user interface callback functions
#############################################
## @@ main program

if {[exec id -u] != 0} {
    rhs_error_dialog "You must be root to run the Printer Tool."
    exit 1
}

#catch {source /usr/lib/stelias/printtool-atalksmb}

#
# msf - silly stuff but have to use it for now
#
set printerdb_debug 0
set printerdb_curdbline 0

# We can't call sync right away or /etc/fstab will get clobbered

catch {exec /bin/cp -f /etc/printcap /etc/printcap.bak}
reload
reload_printerdb
load_ppdxref_file
find_gs
find_mpage
if { $warn_samba != 0 } {
 find_samba
}
if { $warn_ncpfs != 0 } {
 find_nprint
}
set fd [ open $env(CONTROL_PANEL_LIB_DIR)/printtool.warn w ]
puts $fd "set warn_samba $warn_samba"
puts $fd "set warn_ncpfs $warn_ncpfs"
close $fd
write_printcap
# If this is an LPRng system, run checkpc to
# take care of spool directory issues.
# 	commented out -NickM
#if {([file executable "/usr/sbin/checkpc"] == 1)} {
    #catch {exec /usr/sbin/checkpc -f}
#}
redisplay

#================================================================
# BEGIN AUTODETECT SECTION
#
# Added by Mark Pruett 3/10/2000
#================================================================
#############################################################################
# Visual Tcl v1.11p1 Project
#

#################################
# GLOBAL VARIABLES
#
global widget; 
global packed;

#################################
# USER DEFINED PROCEDURES
#
proc init {argc argv} {

}

init $argc $argv


proc load_aliases_file {} {
#----------------------------------------------------
# Load the aliases cross-reference file into an 
# associative array.
#
# $alias(printer-alias) = actual-printer-name
#----------------------------------------------------
    global filtersrcdir
    global alias

    if {[catch {set fd [open "$filtersrcdir/aliases.rdb" r]}] == 1} {
	# there is no aliases table
	puts "Error cannot find $filtersrcdir/aliases.rdb!"
	return
    }

    # Skip initial comments:
    while {[gets $fd s] != "-1"} {
	# strip spaces at the front
	set cp [string trimleft $s]

	# if not a comment line then break?
	if {[regexp "^\#" $cp] != 1} {
	    break
	}
    }

    set fnames $s

    # read field types
    gets $fd ftypes

    set i 0
    while {[gets $fd s] != "-1"}  {
	
	set pfields [split $s "\t"]

	set alias([lindex $pfields 0]) [lindex $pfields 1]

	incr i
    }
    set alias_count $i

    close $fd
}

proc is_lprng_spooler { } {
#----------------------------------------------------
#----------------------------------------------------
    set lprng_found 0

    # Check output of "rpm -q LPRng".
    if [catch {open "| rpm -q LPRng"} input] {
	return $lprng_found
    }

    # Look at output to look for string "not installed"
    if {[gets $input line] >= 0} {
	if {[string first "not installed" $line] == -1} {
	    set lprng_found 1
	}
    }
    catch {close $input}

    if {[file executable "/etc/rc.d/init.d/lprng"] == 1} {
	# Imperfect check for lprng.
	set lprng_found 1
    } elseif {[file executable "/etc/init.d/lpr"] == 1} {
	# This is a check for debian running lprng.
	set lprng_found 1
    } elseif {[file executable "/usr/sbin/checkpc"] == 1} {
	set lprng_found 1
    }

    return $lprng_found
}

proc button_autodetect_add {} {
#----------------------------------------------------
# Lifted from button_add (see above).
#----------------------------------------------------
    global print_count printer_names printer_entry printer_comments
    global printer_info printer_type 

    global printerdb_descr2entry
    global printerdb_GSDriver printerdb_res

    global auto_printer auto_resolution auto_paper_size auto_eof 
    global auto_printerdb_entry auto_color auto_crlf_trans

    global auto_ppdfile
    
    global auto_flag
    global detect_type

    global setmeth

    global detport
    global detip
    global detprn

    global auto_direct_printer_ip
    global auto_direct_port

    global xref
    global alias
    global autodetecting trigger_2

    # If detect_type is REMOTE, see if they really 
    # want DIRECT:

    if {$detect_type != "LOCAL"} {
	set detect_type $setmeth
    }

    # What printer was selected?

    set selected [.winFound.fra18.cpd22.01 curselection]
    if {$selected == ""} {
	# Tell user to select something
	rhs_info_dialog "No printer was selected."
	
	return
    }

    if {$detect_type == "LOCAL"} {
	set port $detport($selected)
    }

    set pname [.winFound.fra18.cpd22.01 get $selected]
    # Strip out the parenthetical port number or IP address
    # we prepended to the string:
    set pos [string first ") " $pname]
    incr pos 2
    if {$pos >= 0} {
	set pname [string range $pname $pos end]
    }

    # Some of the printer descriptions may have one of the
    # following appended to it in printerdb_descr2entry:
    # (Color)
    # (Postscript)
    # (Color Postscript)

    set found 0

    set searchname $pname
    set aname "$pname (Color)"
    if {[info exists printerdb_descr2entry($aname)] } {
	set searchname $aname
	set found 1
    }
    set aname "$pname (Postscript)"
    if {[info exists printerdb_descr2entry($aname)] } {
	set searchname $aname
	set found 1
    }
    set aname "$pname (Color Postscript)"
    if {[info exists printerdb_descr2entry($aname)] } {
	set searchname $aname
	set found 1
    }
    # If none of the earlier attempts matched, check alias table.
    if {$found == 0} {
	set test [string trim $pname]

	if {[info exists alias($test)]} {
	    set searchname $alias($test)
	}
    }

    # Once we've done our best to find a matching 
    # printer name, check if we've succeeded; set $entry.

    if {[info exists printerdb_descr2entry($searchname)] } {
	set entry $printerdb_descr2entry($searchname)
    } else {
	# No driver info found. 
	rhs_info_dialog "
No printer driver information\nwas found for Printer:

$searchname

Printtool's database of known printers could 
not find an exact match for this printer's
name. The printer may be in the database 
with different wording. If not, you may
still be able to set the printer up by 
selecting a printer that is compatible
with your printer.

Press the \"Select\" Input Filter button
on the next window to select from the list
of supported printers.

"
	# Get info from internet? Maybe later.
    }

    #------------------------------------------------
    set type $detect_type

    # First find a candidate lpX
    # (Make first printer added "lp")
    set done 0
    set num 0
    if {$print_count == 0} {
	set lpx "lp"
	set done 1
    }

    # MLP:
    if {$done == 0} {
	set lpx [get_next_unused_qname]
    }

    set i $print_count
    incr print_count

    set printer_names($i) "$lpx"
    set printer_type($i) $type
    set printer_info($i) {}
    set printer_entry($i) "$lpx:sd=/var/spool/lpd/$lpx:mx#0:"
    set printer_comments($print_count) ""
#
#   default to suppress headers on
#
    set_printcap_switch $i "sh" 1

    if {$detect_type == "LOCAL"} {
	set_printcap_field $i "lp=" "/dev/lp$port"
    } elseif {$detect_type == "REMOTE"} {
	set_printcap_field $i "rm=" $detip($selected)
	# Generate a queue name 
	set parts [split $detip($selected) "."]
	set remotequeue "q_[join $parts _]"

	# Apparently, the remotequeue name should be
	# simply "lp", so that's what we'll save it as.
	set_printcap_field $i "rp=" "lp"

	if { [is_lprng_spooler] == 1 } {
	    rhs_info_dialog \
		"The detected printer may not work
correctly with your print spooler (lprng).
If you encounter printing problems, 
consider setup as a DIRECT printer."	    
	}

    } elseif {$detect_type == "DIRECT"} {
	set auto_direct_printer_ip $detip($selected)
	set auto_direct_port 9100
    }

    if {[info exists entry]} {
	set_printcap_field $i "if=" "$auto_flag - $entry"

	set auto_printer $printerdb_GSDriver($entry)
	set auto_printerdb_entry $entry
	set auto_paper_size "letter"

	if [llength [array names printerdb_res $entry]] {
	    set x [lindex $printerdb_res($entry) 0]
	    set auto_resolution [format "%sx%s" [lindex $x 0] [lindex $x 1]]
	} else {
	    set auto_resolution "Default"
	}

	set printer_info($i) [list $auto_printer $auto_resolution \
		$auto_paper_size $auto_eof $auto_printerdb_entry \
		$auto_color $auto_crlf_trans]
    } 

    # See if we have a matching PPD file

#    set auto_ppdfile ""
#    if {[info exists xref($pname)]} {
#	set auto_ppdfile $xref($pname)
#    }



    # Show edit dialog and let user decide if they
    # want to add this printer.

    set autodetecting 1
    set trigger_2 0
    if {[edit_entry_aux $i] != 1} {
	incr print_count -1
    }
}

proc {detect_network} {} {
#----------------------------------------------------
# Detect network printers
#----------------------------------------------------
    # The packed list lets us track which dynamic
    # parts of the "detected printers" dialog
    # are currently packed.
    global packed
    global detect_type
    global setmeth

    global detip
    global detprn

    global ip_from
    global ip_to
    global ip_netmask
    global hostspec

    global sbindir

    set detect_type REMOTE

    #------------------------------------------------
    # validate dialog-box text variables:
    #------------------------------------------------

    if { $hostspec == "" } {
	rhs_info_dialog "You must enter a valid host identifier.\nSee Help for more information."
	return;
    }
    
    #------------------------------------------------
    # Run pconf_detect
    #------------------------------------------------

    if [catch {open "|$sbindir/pconf_detect -m NETWORK -i \"$hostspec\" "} input] {

	rhs_info_dialog	"Could not find pconf_detect command-line utility
required for auto-detection. Make sure
that the gnulpr printfilters package is
properly installed."

	return
    }

    # Step through output and create a list of detected
    # network printers.

    set num 0
    while {[gets $input line] >= 0} {
	#set lstPrinters [split $line "\t"]
	#puts "([lindex $lstPrinters 1])"

	# Output should be in the form:
	# printer=10.203.1.2;vendor=HP;model=LaserJet 4

	set parts [split $line ";"]
	set ipval [split [lindex $parts 0] "="]
	set modelval [split [lindex $parts 1] "="]
	set makeval [split [lindex $parts 2] "="]
	set lstPrinters [list [lindex $ipval 1] "[lindex $modelval 1] [lindex $makeval 1]"]

	if {[lindex $lstPrinters 1] != "Unknown device"} {

	    # build parallel arrays with ...?

	    set detip($num) [lindex $lstPrinters 0]
	    set detprn($num) [lindex $lstPrinters 1]

	    incr num
	}
    }
    catch {close $input}

    # FOR TESTING PURPOSES ONLY!!!!!===============================
    # MLP
    #set detip($num) "10.20.30.40"
    #set detprn($num) "HP DeskJet 600"
    #incr num
    #==============================================================

    if {$num == 0} {
	# no printers detected - bail out.
	rhs_info_dialog "No network printers were detected."
	return
    }

    #------------------------------------------------
    # display the detected printers list window:
    #------------------------------------------------

    Window show .winFound
    grab set .winFound
    
    list packed

    catch {
	label .winFound.fra23.lblInfo \
		-justify left \
		-text "Select a printer, then press \"Add Printer\""
    }
    pack .winFound.fra23.lblInfo \
	    -in .winFound.fra23 -anchor center -expand 1 -fill x -side top 
    lappend packed .winFound.fra23.lblInfo

    #------------------------------------------------
    # Set up the window with the right number
    # of setup-method radio buttons:
    #------------------------------------------------

    list packed 
    set i 1
    set methlist {"NONE" "REMOTE" "DIRECT"}
    set setmeth [lindex $methlist 1]

    catch {
	radiobutton .winFound.fra23.rad$i  \
		-justify left -text "Setup as REMOTE printer" \
		-variable setmeth -value [lindex $methlist $i]
    }
    pack .winFound.fra23.rad$i \
	    -in .winFound.fra23 -anchor center -expand 1 -fill x -side top 
    lappend packed .winFound.fra23.rad$i

    incr i
    catch {
	radiobutton .winFound.fra23.rad$i  \
		-justify left -text "Setup as DIRECT printer" \
		-variable setmeth -value [lindex $methlist $i]
    }
    pack .winFound.fra23.rad$i  \
	    -in .winFound.fra23 -anchor center -expand 1 -fill x -side top 
    lappend packed .winFound.fra23.rad$i

    #------------------------------------------------
    # fill the list box with detected printers
    #------------------------------------------------

    # Delete anything in the listbox.

    .winFound.fra18.cpd22.01 delete 0 end

    for {set i 0} {$i < $num} {incr i} {
	.winFound.fra18.cpd22.01 insert end "($detip($i)) $detprn($i)"
	
    }

}

proc {detect_pport} {} {
#----------------------------------------------------
# Detect parallel port printers
#----------------------------------------------------
    # The packed list lets us track which dynamic
    # parts of the "detected printers" dialog
    # are currently packed.
    global packed
    global detect_type

    global detport
    global detprn

    global sbindir

    set detect_type LOCAL

    #------------------------------------------------
    # Let's just sweep through ports 0 through 2
    #------------------------------------------------
    set ports "0,1,2"

    #------------------------------------------------
    # Run pconf_detect
    #------------------------------------------------

    if [catch {open "|$sbindir/pconf_detect -m PPORT -i $ports " } input] {

	rhs_info_dialog	"Could not find pconf_detect command-line utility
required for auto-detection. Make sure
that the gnulpr printfilters package is
properly installed."

	return
	
    }

    set ports_not_configured 0
    set ports_checked 0
    set num 0
    while {[gets $input line] >= 0} {

	# Output should be in the form:
	# port=0;model=HP DeskJet 500

	set pos [string first "port=" $line]
	if {$pos == -1} {
	    # Not a valid line, so skip.
	    continue
	}

	set parts [split $line ";"]
	set portval [split [lindex $parts 0] "="]
	set prnval [split [lindex $parts 1] "="]
	set lstPrinters [list [lindex $portval 1] [lindex $prnval 1]]

	incr ports_checked 1
	if {[lindex $lstPrinters 1] == "Port not configured"} {
	    incr ports_not_configured 1
	}

	if {([lindex $lstPrinters 1] != "Unknown device") &&
            ([lindex $lstPrinters 1] != "Port not configured")} {

	    # build parallel arrays with a port number and 
	    # printer ID.

	    set detport($num) [lindex $lstPrinters 0]
	    set detprn($num) [lindex $lstPrinters 1]

	    incr num
	}
    }
    catch {close $input}

    # FOR TESTING PURPOSES ONLY!!!!!===============================
    # MLP
    #set detport($num) 1
    #set detprn($num) "HP DeskJet 600"
    #incr num
    #==============================================================

    if {$ports_not_configured == $ports_checked} {
	# Couldn't find parport info.
	rhs_info_dialog "
No parallel ports detected. Make sure ports are
properly configured before running printtool.

You may not have parallel port support available
in your Linux kernel, or you may not be loading
the parallel port modules (usually called \"parport\")
needed for communicating with printers through
your computer's parallel ports.

"
	return
    }
	
    if {$num == 0} {
	# no printers detected - bail out.
	rhs_info_dialog "No parallel port printers were detected."
	return
    }

    #------------------------------------------------
    # display the detected printers list window:
    #------------------------------------------------

    Window show .winFound
    grab set .winFound
    
    list packed

    catch {
	label .winFound.fra23.lblInfo  \
		-justify left \
		-text "Select a printer, then press \"Add Printer\""
    }
    pack .winFound.fra23.lblInfo \
	    -in .winFound.fra23 -anchor center -expand 1 -fill x -side top 
    lappend packed .winFound.fra23.lblInfo

    #------------------------------------------------
    # fill the list box with detected printers
    #------------------------------------------------

    .winFound.fra18.cpd22.01 delete 0 end

    for {set i 0} {$i < $num} {incr i} {
	.winFound.fra18.cpd22.01 insert end "(Port $detport($i)) $detprn($i)"
	
    }

}

proc button_autodetect {} {
#----------------------------------------------------
# Display supported autodetect methods
#----------------------------------------------------

#    load_ppdxref_file
    load_aliases_file
    Window show .winDetMeth
    grab set .winDetMeth
}


proc {winDetMeth_hide} {} {
#----------------------------------------------------
# Pop-down the "autodetect methods" window
#----------------------------------------------------

    grab release .winDetMeth
    Window hide .winDetMeth
    
}

proc {winFound_hide} {} {
#----------------------------------------------------
# Pop-down the "Detected Printers" window
#----------------------------------------------------
    global packed
    global autodetecting

    set autodetecting 0

    set i 0
    while {$i < [llength $packed]} {
	pack forget [lindex $packed $i]
	incr i
    }

    grab release .winFound
    Window hide .winFound
}

proc {help_detect_pport} {} {
#----------------------------------------------------
# Popup help for "Detect parallel port printers".
#----------------------------------------------------
    help_dialog "Autodetecting Parallel Ports" "
Detecting Parallel Ports
------------------------

This option will attempt to check for
printers attached to parallel ports
on the local machine. Only printers
that repond to the IEEE 1284 protocol
are detected.
"

}

proc {help_detect_network} {} {
#----------------------------------------------------
# Popup help for "Detect parallel port printers".
#----------------------------------------------------
    help_dialog "Autodetecting Network Printers" "
Detecting Network Printers
--------------------------

This option probes your network seeking 
printers capable of identifying themselves.

The Host Specification can take one of
several forms:

Hostname: 
    myprinter.mydomain.com

IP address: 
    10.203.2.44

IP range: 
    10.203.2.5-23

IP/Netmask: 
    10.203.168.0.0/24 or 
    10.203.168.0.0/255.255.255.0

Note: This option may take several minutes
to execute, depending on the size of the
network you are scanning.

"

}

proc {main} {argc argv} {
#----------------------------------------------------
#----------------------------------------------------
}

proc {Window} {args} {
global vTcl
    set cmd [lindex $args 0]
    set name [lindex $args 1]
    set newname [lindex $args 2]
    set rest [lrange $args 3 end]
    if {$name == "" || $cmd == ""} {return}
    if {$newname == ""} {
        set newname $name
    }
    set exists [winfo exists $newname]
    switch $cmd {
        show {
            if {$exists == "1" && $name != "."} {wm deiconify $name; return}
            if {[info procs vTclWindow(pre)$name] != ""} {
                eval "vTclWindow(pre)$name $newname $rest"
            }
            if {[info procs vTclWindow$name] != ""} {
                eval "vTclWindow$name $newname $rest"
            }
            if {[info procs vTclWindow(post)$name] != ""} {
                eval "vTclWindow(post)$name $newname $rest"
            }
        }
        hide    { if $exists {wm withdraw $newname; return} }
        iconify { if $exists {wm iconify $newname; return} }
        destroy { if $exists {destroy $newname; return} }
    }
}

#################################
# VTCL GENERATED GUI PROCEDURES
#

proc vTclWindow. {base} {
    if {$base == ""} {
        set base .
    }
    ###################
    # CREATING WIDGETS
    ###################
    wm focusmodel $base passive
    wm geometry $base 1x1+0+0
    wm maxsize $base 1009 738
    wm minsize $base 1 1
    wm overrideredirect $base 0
    wm resizable $base 1 1
    wm withdraw $base
    wm title $base "vt.tcl"
    ###################
    # SETTING GEOMETRY
    ###################
}

proc vTclWindow.winDetMeth {base} {
    if {$base == ""} {
        set base .winDetMeth
    }
    if {[winfo exists $base]} {
        wm deiconify $base; return
    }
    ###################
    # CREATING WIDGETS
    ###################
    toplevel $base -class Toplevel
    wm focusmodel $base passive
# MLP: geometry with HP Multicast widgets enabled:
#    wm geometry $base 396x296+326+240
    wm geometry $base 396x226
    wm maxsize $base 1009 738
    wm minsize $base 1 1
    wm overrideredirect $base 0
    wm resizable $base 1 1
    wm deiconify $base
    wm title $base "AutoDetect Printers"
    frame $base.fra23 \
        -borderwidth 2 -height 152 -relief raised -width 125 
    label $base.fra23.lab26 \
        -borderwidth 1 -text {Detection Methods:} 
    frame $base.fra23.fra27 \
        -borderwidth 2 -height 75 -relief groove -width 125 
    label $base.fra23.fra27.lab28 \
        -borderwidth 1 -foreground #0000c0 -text {Parallel Port Printers} 
    button $base.fra23.fra27.but29 \
        -command detect_pport -padx 9 -pady 3 -text Detect... 
    button $base.fra23.fra27.butHelpPPort \
	-command help_detect_pport -padx 9 -pady 3 -text {Help}
    frame $base.fra23.fra30 \
        -borderwidth 2 -height 75 -relief groove -width 125 
    label $base.fra23.fra30.lab31 \
        -borderwidth 1 -foreground #0000c0 -text {Networked Printers} 
#    frame $base.fra23.fra30.fra32 \
#        -borderwidth 2 -height 75 -relief groove -width 125 
#    label $base.fra23.fra30.fra32.lab33 \
#        -borderwidth 1 -text {Foo} 
#    entry $base.fra23.fra30.fra32.ent34 -textvariable ip_from \
#        -width 30
#    label $base.fra23.fra30.fra32.lab35 \
#        -borderwidth 1 -text or 
    frame $base.fra23.fra30.fra37 \
	    -borderwidth 2 -height 75 -relief groove -width 125 
    label $base.fra23.fra30.fra37.lab38 \
	    -borderwidth 1 -text {Host Specification:} 
    entry $base.fra23.fra30.fra37.ent39 -textvariable hostspec \
	    -width 30
    frame $base.fra23.fra30.fra40 \
        -borderwidth 2 -height 75 -width 125 
    button $base.fra23.fra30.butHelpNetwork \
	-command help_detect_network -padx 9 -pady 3 -text {Help}
    button $base.fra23.fra30.fra40.but41 \
        -command detect_network -padx 9 -pady 3 -text Detect... 
    frame $base.fra23.fra42 \
        -borderwidth 2 -height 75 -relief groove -width 125 
    label $base.fra23.fra42.lab43 \
        -borderwidth 1 -foreground #0000c0 -text {HP Multicast} 
    button $base.fra23.fra42.but44 \
        -padx 9 -pady 3 -text Detect... 
    frame $base.fra24 \
        -borderwidth 2 -relief raised -width 125 
    button $base.fra24.but25 \
        -command winDetMeth_hide -padx 9 -pady 3 -text Cancel 
    ###################
    # SETTING GEOMETRY
    ###################
    pack $base.fra23 \
        -in .winDetMeth -anchor s -expand 1 -fill both -side top 
    pack $base.fra23.lab26 \
        -in .winDetMeth.fra23 -anchor sw -expand 0 -fill none -padx 3 \
        -side top 
    pack $base.fra23.fra27 \
        -in .winDetMeth.fra23 -anchor center -expand 0 -fill x -ipadx 3 \
        -padx 3 -pady 3 -side top 
    pack $base.fra23.fra27.lab28 \
        -in .winDetMeth.fra23.fra27 -anchor center -expand 0 -fill none -padx 3 \
        -side left 
    pack $base.fra23.fra27.but29 \
        -in .winDetMeth.fra23.fra27 -anchor center -expand 0 -fill none \
        -side right 
    pack $base.fra23.fra27.butHelpPPort \
        -in .winDetMeth.fra23.fra27 -anchor center -expand 0 -fill none \
        -side right 

    pack $base.fra23.fra30 \
        -in .winDetMeth.fra23 -anchor center -expand 0 -fill x -ipadx 3 \
        -ipady 3 -padx 3 -pady 3 -side top
    pack $base.fra23.fra30.lab31 \
        -in .winDetMeth.fra23.fra30 -anchor sw -expand 0 -fill none -padx 3 \
        -pady 3 -side top 
    pack $base.fra23.fra30.fra37 \
        -in .winDetMeth.fra23.fra30 -anchor center -expand 0 -fill x -ipadx 3 \
        -ipady 3 -padx 3 -pady 3 -side top 
    pack $base.fra23.fra30.fra37.lab38 \
        -in .winDetMeth.fra23.fra30.fra37 -anchor center -expand 0 -fill none \
        -side left 
    pack $base.fra23.fra30.fra37.ent39 \
        -in .winDetMeth.fra23.fra30.fra37 -anchor center -expand 0 -fill none \
        -side left 
    pack $base.fra23.fra30.fra40 \
        -in .winDetMeth.fra23.fra30 -anchor center -expand 0 -fill x -pady 3 \
        -side top 
    pack $base.fra23.fra30.fra40.but41 \
        -in .winDetMeth.fra23.fra30.fra40 -anchor center -expand 0 -fill none \
        -side right 
    pack $base.fra23.fra30.butHelpNetwork \
        -in .winDetMeth.fra23.fra30.fra40 -anchor center -expand 0 -fill none \
        -side right 

# MLP: These are related to HP Multicast, which is not
# yet implemented, but may be in the not-too-distant future.

#    pack $base.fra23.fra42 \
#        -in .winDetMeth.fra23 -anchor center -expand 0 -fill x -ipadx 3 \
#        -ipady 3 -padx 3 -pady 3 -side top 
#    pack $base.fra23.fra42.lab43 \
#        -in .winDetMeth.fra23.fra42 -anchor center -expand 0 -fill none \
#        -side left 
#    pack $base.fra23.fra42.but44 \
#        -in .winDetMeth.fra23.fra42 -anchor center -expand 0 -fill none \
#        -side right 
    pack $base.fra24 \
        -in .winDetMeth -anchor n -expand 0 -fill both -ipadx 5 -ipady 3 \
        -side bottom 
    pack $base.fra24.but25 \
        -in .winDetMeth.fra24 -anchor center -expand 0 -fill y -padx 3 \
        -pady 3 -side right 
}

proc vTclWindow.winFound {base} {
    if {$base == ""} {
        set base .winFound
    }
    if {[winfo exists $base]} {
        wm deiconify $base; return
    }
    ###################
    # CREATING WIDGETS
    ###################
    toplevel $base -class Toplevel
    wm focusmodel $base passive
    wm geometry $base 288x326
    wm maxsize $base 1009 738
    wm minsize $base 1 1
    wm overrideredirect $base 0
    wm resizable $base 1 1
    wm title $base "Detected Printers"
    frame $base.fra18 \
        -borderwidth 2 -height 200 -relief raised -width 125 
    frame $base.fra18.cpd22 \
        -borderwidth 1 -height 70 -width 30 
    listbox $base.fra18.cpd22.01 \
        -font -Adobe-Helvetica-Medium-R-Normal-*-*-120-*-*-*-*-*-* \
        -xscrollcommand {.winFound.fra18.cpd22.02 set} \
        -yscrollcommand {.winFound.fra18.cpd22.03 set} 
    scrollbar $base.fra18.cpd22.02 \
        -borderwidth 1 -command {.winFound.fra18.cpd22.01 xview} \
        -orient horiz -width 10 
    scrollbar $base.fra18.cpd22.03 \
        -borderwidth 1 -command {.winFound.fra18.cpd22.01 yview} -orient vert \
        -width 10 
    frame $base.fra19 \
        -borderwidth 2 -height 41 -relief raised -width 125 
    button $base.fra19.but20 \
        -command button_autodetect_add -padx 9 -pady 3 -text {Add Printer} 
    button $base.fra19.but21 \
        -command winFound_hide -padx 9 -pady 3 -text Cancel 
    frame $base.fra23 \
        -borderwidth 2 -height 126 -relief raised -width 125 
    ###################
    # SETTING GEOMETRY
    ###################
    pack $base.fra18 \
        -in .winFound -anchor center -expand 1 -fill both -side top 
    pack $base.fra18.cpd22 \
        -in .winFound.fra18 -anchor center -expand 1 -fill both -side top 
    grid columnconf $base.fra18.cpd22 0 -weight 1
    grid rowconf $base.fra18.cpd22 0 -weight 1
    grid $base.fra18.cpd22.01 \
        -in .winFound.fra18.cpd22 -column 0 -row 0 -columnspan 1 -rowspan 1 \
        -sticky nesw 
    grid $base.fra18.cpd22.02 \
        -in .winFound.fra18.cpd22 -column 0 -row 1 -columnspan 1 -rowspan 1 \
        -sticky ew 
    grid $base.fra18.cpd22.03 \
        -in .winFound.fra18.cpd22 -column 1 -row 0 -columnspan 1 -rowspan 1 \
        -sticky ns 
    pack $base.fra19 \
        -in .winFound -anchor center -expand 0 -fill x -ipadx 3 -ipady 3 \
        -side bottom 
    pack $base.fra19.but20 \
        -in .winFound.fra19 -anchor center -expand 0 -fill none -padx 3 \
        -side left 
    pack $base.fra19.but21 \
        -in .winFound.fra19 -anchor center -expand 0 -fill none -ipadx 5 \
        -padx 3 -side right 
    pack $base.fra23 \
        -in .winFound -anchor center -expand 0 -fill x -side top 
}

