#!/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

if {[catch set env(CONTROL_PANEL_LIB_DIR)] != 0} {
    set env(CONTROL_PANEL_LIB_DIR) /usr/lib/rhs/control-panel
}
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/lib/rhs/control-panel)"
    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/lib/rhs/control-panel)"
    exit 0
}

#########################################################
## @@ Random Data
set version "3.0r6"
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_crlf_trans ""
set tmp_auto_ascps_trans ""
set tmp_auto_rev_pages ""

set gs_installed_drivers ""

#
# 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"

#
# 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.
#
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_eof ""
set auto_crlf_trans ""
set auto_ascps_trans ""
set auto_rev_pages ""
set auto_color "Default"

#
# new filter related locations and filenames
#
set filtersrcdir "/usr/lib/rhs/printtool"

#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
pack .edit .add .delete -side left -expand true -ipady 1 -padx 1c -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 . "RHS 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 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 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 { 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

#
# 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"
	    }
	}
    } else {
	set auto_printer ""
	set auto_resolution ""
	set auto_paper_size ""
	set auto_eof ""
	set auto_pseof "NO"
	set auto_texteof "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
	if {$auto_ascps_trans != "YES" && $auto_ascps_trans != "NO"} {
	    set auto_ascps_trans "NO"
	}

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

	catch {exec sed -n -e "s/TEXT_SEND_EOF=//p" \
		< $spool_dir/textonly.cfg} auto_texteof
	if {$auto_texteof != "YES" && $auto_texteof != "NO"} {
	    set auto_texteof "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 set up 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]
#
# following two DO NOT depend upon ghostscript being installed
# please handle this more cleanly in future!
#
    lappend gs_installed_drivers {TEXT} {POSTSCRIPT}
}

#
# see if nenscript is installed
# 
proc find_nenscript {} {

    if [catch {open "|/usr/bin/nenscript -h"} input] {
	puts "open result was $input"
    }

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

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

#
# 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 ./printerdb!"
	return
    }

    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}

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

    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 desired_print_format
    global printer_type
    global auto_ascps_trans
    global auto_eof auto_pseof auto_texteof

    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}

#
#   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"
	}
	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 "-dBitsPerPixel=$auto_color"
        } else {
	    set tauto_color ""
	}

#
#   for now no support for user-supplied gs options
#
        set extragsoptions ""

	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@@@/$extragsoptions/g" \
	    -e "s/@@@pssendeof@@@/$auto_pseof/g" \
	    < $filtersrcdir/postscript.cfg.in > $spool_dir/postscript.cfg}
    }
	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 ""
	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} 

}

proc FILTERED_summaryline {i def} { 
    global printer_info
    global printerdb_descr

    set info $printer_info($i)
    if {[llength $info] > 4} {
	set printer $printerdb_descr([lindex $info 4])
    } 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.fl9
    label $w.l9 -text "Input Filter" -anchor w
    button $w.autofilter -text "Select" -command "LOCAL_select_filter $w.v9"
    pack $w.l9 -side left -in $w.fl9
    pack $w.autofilter -side right -in $w.fl9

    entry $w.v9 -font fixed -relief sunken -borderwidth 2
    $w.v9 delete 0 end
    $w.v9 insert 0 [printcap_field $i "if=" ""]

    checkbutton $w.v10 -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

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

    if { $auto_printerdb_entry != "" } {
        $w.v9 delete 0 end
        $w.v9 insert 0 $auto_flag
	$w.v9 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

    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"
    if { [$w.v8 get] == "" } {
	if { [catch {open /dev/lp0 w} junkfp] == 0} {
	    set lp0_stat "Detected"
	    $w.v8 insert 0 "/dev/lp0"
	    close $junkfp
	} elseif { [catch {open /dev/lp1 w} junkfp] == 0} {
	    set lp1_stat "Detected"
	    $w.v8 insert 0 "/dev/lp1"
	    close $junkfp
	} elseif { [catch {open /dev/lp2 w} junkfp] == 0} {
	    set lp2_stat "Detected"
	    $w.v8 insert 0 "/dev/lp2"
	    close $junkfp
	} 

	rhs_info_dialog "Auto-detection found the following:

         /dev/lp0 :      $lp0_stat
         /dev/lp1 :      $lp1_stat
         /dev/lp2 :      $lp2_stat

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

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

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

    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} {

    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


#   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])

#   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 "%4sx%-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) {
	    set g [format "%4s %-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 LOCAL_select_filter {e} {
    global trigger_2
    global paper_size_list
    global auto_printer auto_resolution auto_paper_size auto_flag
    global tmp_auto_eof tmp_auto_crlf_trans tmp_auto_rev_pages
    global auto_eof auto_pseof auto_texteof
#
# 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 auto_rev_pages
    global gs_installed_drivers
    global auto_ascps_trans tmp_auto_ascps_trans
    global auto_texteof tmp_auto_texteof
    global auto_pseof tmp_auto_pseof


#    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.f9
    frame .sf.f10

#
# 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"
#
# 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 8 -yscrollcommand ".sf.f2.sy set" \
		     -font  -adobe-helvetica-medium-r-normal--14*
    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" -anchor w -width 12
    listbox .sf.f6.list -yscrollcommand [list .sf.f6.sy set] \
                        -width 44 -height 3 -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 "Native Text Printing Options" -anchor nw
    set tmp_auto_crlf_trans $auto_crlf_trans
    checkbutton .sf.c2 -text "LF->CR/LF Translation?" \
	-variable tmp_auto_crlf_trans -offvalue "" -onvalue 1 -anchor nw
    set tmp_auto_texteof $auto_texteof
    checkbutton .sf.c4 -text "Send EOF (\\014) after job?" \
	-variable tmp_auto_texteof -offvalue "NO" -onvalue "YES" -anchor nw

    label .sf.l11 -text "Filtered Printing Options"
    set tmp_auto_ascps_trans $auto_ascps_trans
    checkbutton .sf.c3 -text "Convert ASCII to Postscript?" \
	-variable tmp_auto_ascps_trans -offvalue "" -onvalue "YES" \
	-offvalue "NO"
    set tmp_auto_pseof $auto_pseof
    checkbutton .sf.c1 -text "Send EOF (\\014) after job?" \
	-variable tmp_auto_pseof -offvalue "NO" -onvalue "YES"

#
# 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  .sf.c2 .sf.c4 .sf.l10 -side bottom -fill x -in .sf.f7
    pack  .sf.c3 .sf.c1 .sf.l11 -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"
    button .sf.b2 -text "Cancel" -width 10 -command "set trigger_2 0"
    button .sf.b3 -text "HELP" -width 10 -command " help_dialog {
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 \"Filtered Printing Options\" -

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

  \"Send EOF (\\014) after job?\" -
      Force 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.

  \"Convert ASCII to PostScript?\" -
      When setting up a PostScript printer, or a printer for which 
      you have selected a ghostscript driver, turning on this option
      causes a text file to be converted it to PostScript using the
      program 'nenscript'. This can produce nicer output, but can be
      slow on printers other than laser printers.

      If this option is turned off, the print system sends text input 
      straight to the printer. This is usually much faster on inkjet 
      and dot-matrix printers. Use the \"Native Text Printing Options\"
      to further control text output if this conversion is turned off.

The \"Native Text Printing Options\" -
  
  These options only apply if your printer is configured as 'Text-only',
  or you have turned off the conversion of text files to PostScript.
  In the later case, these options only apply when printing text files.
  When printing PostScript, or a file which gets converted to PostScript,
  these options are not used. Look at the \"Filtered Printing Options\"
  instead.

  \"Send EOF (\\014) after job?\" -
      Force 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. 

  \"LF->CR/LF Translation?\" -
      Turn this on if you are getting stair-stepped text.

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

#
# stack printer list box and ok/canel buttons
#
    pack .sf.f1 .sf.f8 -side top -expand yes -fill y -in .sf.cf3
#
# 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

    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"
	}
	set auto_pseof $tmp_auto_pseof
	set auto_texteof $tmp_auto_texteof
	set auto_crlf_trans $tmp_auto_crlf_trans
	set auto_ascps_trans $tmp_auto_ascps_trans
	set auto_rev_pages $tmp_auto_rev_pages
#	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.v9 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 filtersrcdir
    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 "lpd queue $queue on $host"
}

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

proc REMOTE_addpanel {w i} {

    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
}

proc REMOTE_updateentry {w i} {
    set_printcap_field $i "rm=" [$w.v5 get]
    set_printcap_field $i "rp=" [$w.v6 get]
}
proc REMOTE_setup {i} {}


#
# SMB support by Miquel de Icaza, adopted to printtool 3.0 by Michael Fulbright
#
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 "user=\(.*\)" [gets $fd] dummy user]} {
 	     set user ""
        }
        if {![regexp "password=\(.*\)" [gets $fd] dummy password]} {
 	     set password ""
        }

        set translate no
	if {![regexp {\\\\(.*)\\(.*)} $sharename dummy hostname printername] } {
	    set hostname ""
	    set printername ""
	}
        
        return [list $hostname $printername $user $password]
    }
    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 "Lan Manager Printer (SMB)" }

proc SMB_addpanel {w i} {
    global smb_crlf

    set smb_crlf 0
    set config [SMB_config $i]

    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.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.l7 -text "Automatic CR/LF translation" -anchor w
#    checkbutton $w.c7 -variable smb_crlf 
#    if {[string compare [lindex $config 2] yes] == 0} {
#	$w.c7 select
#    }

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

    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.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 $w.v7 $w.v8 -side top -expand true -fill x -in $w.f2

    FILTERED_addpanel $w $i
}

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

    set smb_share_name "\\\\[$w.v5 get]\\[$w.v6 get]"
    set smb_user   [$w.v7 get]
    set smb_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.v9 get]
    FILTERED_updateentry $i $filter

}

proc SMB_setup {i} {
    global smb_share_name smb_password smb_user smb_crlf
    global auto_flag

#
#   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 755 $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 "user=$smb_user"
    puts $ffd "password=$smb_password"
    close $ffd

#
#   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=" $filtersrcdir/smbprint
    } 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 ""
}

proc sync {} {
    write_printcap
    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

    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) == "SMB"} {
	rhs_info_dialog \
"               - WARNING -

    The use of a remote Lan Manager 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

    set traversal_list ""

    label .e.l1 -text "Names (name1|name2|...)" -anchor w
    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" -anchor w
    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

    $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

    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
    tkwait variable trigger

    if {$trigger == 1} {
	set printer_names($i) [.e.v1 get]
	regsub {^[^:]*:} $printer_entry($i) "[.e.v1 get]:" printer_entry($i)
	
	set_printcap_field $i "sd=" [.e.v2 get]
	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

    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 755 $spool_dir}

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

	sync
    }

    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 { msg } {
    global trigger_help


    toplevel .hf
    wm withdraw .hf
    wm group .hf .
    wm title .hf "General Help"

    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
------------

In order to create a new print queue, select 'ADD'.

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

There are 3 types of printers which can be configured with printtool:  LOCAL, REMOTE, and SMB

  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. Usuaully 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.
   
After choosing the printer type, the following information is required:

  Queue name: What the queue will be called. Multiple names
              can be specifed 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.

   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.
"
}


proc menu_trouble {} {

    help_dialog "
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 if 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 (\\014)\" option.

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

proc menu_about {} {
    global version

    rhs_info_dialog "
Red Hat Software Printtool Version $version

This tool allows you to set up printers easily.

The default printer is the first one listed.  To change
this you will have to edit /etc/printcap and move the
apropriate 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 echo'd
straight to the printer. It is possible to convert 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.
"
}

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
    }
    while {$done == 0} {
	set lpx "lp$num"
	set done 1
	for {set i 0} {$i < $print_count} {incr i} {
	    if {[regexp $lpx $printer_names($i)] == 1} {
		set done 0
		break
	    }
	}
	incr num
    }

    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 {} {
    catch {exec /etc/rc.d/init.d/lpd.init stop}
    catch {exec /etc/rc.d/init.d/lpd.init start}
}

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 filtersrcdir
    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
    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 sumline [string trim [lindex [split $sumline] 0]]
    if { $sumline == "type unrecognized" || $sumline == "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
    }
    
    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"
    }

}

## 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
find_gs
find_nenscript
write_printcap
redisplay
