#!/opt/Tcl-Tk/bin/wish -f
# installation: path of Tk-Lpc distribution
# installation: you must set this or the important procedures cannot be found!
# set path /opt/tk-lpc
set path /home/Alceste/stef/PLP/plp-4.1.2/contrib/tk-lpc
# installation: set the path here of your executable lpd program
set lpd_program "/usr/sbin/lpd"
source [join "$path statusfunctions.tcl" /]
source [join "$path menufunctions.tcl" /]
source [join "$path widgetfunctions.tcl" /]
source [join "$path help.tcl" /]
source [join "$path buttons.tcl" /]

# set configuration variables
#set fonts
set fontmain -adobe-courier-bold-r-normal--14-100-100-100-m-90-iso8859-1
set fonthelptext -adobe-courier-medium-r-normal--14-100-100-100-m-90-iso8859-1
set fontbighelptext -adobe-times-bold-r-normal--14-140-75-75-p-*
set fontdialog -Adobe-times-bold-R-Normal--18-180-* 
#define colours
set backgr gray78
set backgr2 gray92
set backgr3 gray72
set backgr4 gray98
set butcol  wheat
set activebg peachpuff2
# set pressedbg cornsilk
# set sunkenbg cornsilk
set pressedbg tomato1
set sunkenbg  tomato1
set pressedfg snow1
set sunkenfg  snow1
set normalcol black
#height for listbox in lines
set listboxheight 10
set selectedprs all
# define refreshrate in milliseconds 
set refreshrate 15000
# set refresh status "normal" or "disabled"
set refresh normal
set remote 0
# installation:
# define prefered language "english" or "german" in the NEXT line;
# the line number here shows the beginning of the corresponding help text
set language german
set helpchapterlines(german) {{"Allg. Hilfe" 1} {"Drucker Modus" 11} \
	{"Job Modus" 29} {"QueueOperations Men" 54} {"Job Operations Men" 88} \
        {"Das LPD Men" 122} {"Das Refresh Men" 136} {"Buttons" 149} \
        }
set helpchapterlines(english) {{"General Information" 1} {"Printer Mode" 13} \
        {"Job Mode" 30} {"QueueOperations" 53} {"Job Operations" 85} \
        {"LPD Menu" 116} {"The Refresh Menu" 130} {"Buttons"  144} \
        }

set helptitle(german) Hilfe
set helptitle(english) Help
set helpfile(german) hilfe.txt
set helpfile(english) help.txt

# choose title and icon for top-level window
set machine [exec uname -n] 
wm title . "Tk-Lpc 1.3 for PLP 4.1.2 (Host: $machine)"
# installation: set in the next TWO lines the path to the Tk-Lpc icon e.g.
#  wm iconbitmap . @/opt/tk-lpc/printer.xbm 
# wm iconbitmap . @/opt/tk-lpc/printer.xbm
# wm iconmask . @/opt/tk-lpc/printer.xbm
wm iconbitmap . @/home/Alceste/stef/PLP/plp-4.1.2/contrib/tk-lpc/printer.xbm
wm iconmask . @/home/Alceste/stef/PLP/plp-4.1.2/contrib/tk-lpc/printer.xbm
wm iconname . "Tk-Lpc"

#create menubar
frame .menuframe -height 23 -relief raised -bd 2 -bg $backgr 
pack .menuframe -side top -anchor w -fill x 
menubutton .menuframe.pr -text QueueOperations -underline 0\
  -menu .menuframe.pr.menu -padx 4 -bg $backgr
menubutton .menuframe.job  -text JobOperations -underline 0\
  -menu .menuframe.job.menu -padx 4 -state disabled -bg $backgr
menubutton .menuframe.daemon -text LPD -underline 0 -bg $backgr\
  -menu .menuframe.daemon.menu -padx 4 -state disabled
menubutton .menuframe.refr -text Refresh -underline 0\
  -menu .menuframe.refr.menu -padx 4 -bg $backgr
menubutton .menuframe.help -text Help -underline 0\
  -menu .menuframe.help.menu -padx 4 -bg $backgr
button .menuframe.exit -text Exit -underline 0 -padx 5 -relief flat \
  -borderwidth 0 -command \
           {set erg [dialog .exit Exit {Exit now ?} {} 1 Yes No]
            switch $erg {
            0 {destroy .}
            1 {}
            }
           } -bg $backgr -borderwidth 0

pack .menuframe.pr .menuframe.job .menuframe.daemon -side left
pack .menuframe.exit .menuframe.help .menuframe.refr -side right

#create menu for printer operations
set prmenu [menu .menuframe.pr.menu -tearoff 0 -activebackground $activebg]
$prmenu add command -label "Up" -command {prcmds up} -underline 0
$prmenu add cascade -label "Down" \
  -menu $prmenu.down -underline 0 
$prmenu add separator
$prmenu add cascade -label "History" -menu $prmenu.history -underline 0
$prmenu add separator
$prmenu add command -label "Start" -command {prcmds start} -underline 0
$prmenu add command -label "Restart" -command {prcmds restart} -underline 0
$prmenu add cascade -label "Stop" \
  -menu $prmenu.stop -underline 2
$prmenu add command -label "Abort" -command {prcmds abort} -underline 0
$prmenu add command -label "Kill" -command {prcmds kill} -underline 0

menu $prmenu.history -tearoff 0
$prmenu.history add command -label "All printers" -underline 4\
	-command {historyall}
#  -command {selectallproc} -command {prcmds "history all all"}
$prmenu.history add command -label " All users" -underline 1\
   -command {prcmds "history all"}
$prmenu.history add command -label "    User  " -underline 4\
-command { set text "Type in username: " 
           set erg [dialog  .history History $text entry\
		0 Accept Cancel]
           switch $erg {
	       0 {prcmds "history $message"}
	       1 {}
	   }
         }

menu $prmenu.down -tearoff 0
$prmenu.down add command -label "No Message" -underline 0\
  -command {prcmds down}
$prmenu.down add command -label "   Message" -underline 3\
  -command { set text "Type in Message: "
             set erg [dialog .down Down $text entry\
              0 Accept Cancel]
             switch $erg {
              0 {prcmds down #$message}
              1 {}
             }
           }
   
menu $prmenu.stop -tearoff 0
$prmenu.stop add command -label "No Message"\
  -command {prcmds stop}
$prmenu.stop add command -label "   Message"\
  -command { set text "Type in Message: "
             set erg [dialog .stop Stop $text entry\
              0 Accept Cancel]
             switch $erg {
              0 {prcmds stop #$message}
              1 {}
             }
           }

$prmenu add separator

$prmenu add command -label "Enable" -command {prcmds enable} -underline 0

$prmenu add cascade -label "Disable" \
  -menu $prmenu.disable -underline 1

$prmenu add separator
$prmenu add command -label "Attach" -command { while 1 {
            if {[attach]==0} {return }}} -underline 1

$prmenu add command -label "Unattach" -command { unattach} -underline 1

$prmenu add separator

$prmenu add command -label "Clean" -underline 0 \
  -command {set text "Do you really want to clean the queue(s) ?"
            set erg [dialog .clean Clean $text {} 1 Yes No]
            switch $erg {
            0 {prcmds clean}
            1 {}
            }
           }

menu $prmenu.disable -tearoff 0
$prmenu.disable add command -label "No Message"\
  -command {prcmds disable}
$prmenu.disable add command -label "   Message"\
  -command {set text "Type in Message: "
            set erg [dialog .disable Disable $text entry\
             0 Accept Cancel]
            switch $erg {
             0 {prcmds disable #$message}
             1 {}
            }
           }
                 
#create menu for job operations
set jobmenu [menu .menuframe.job.menu -tearoff 0 -activebackground $activebg]
$jobmenu add command -label "Chprio" -underline 0 -command {
      set text "Type in new Priority: (A-Z) "
      while 1 {
        set erg [dialog .chprio Chprio $text shortentry\
          0 Accept Cancel]
        if {$erg == 1} break 
        if [regexp -nocase {[a-z]} $prioentry] { 
          set priority [string toupper $prioentry]
          jobcmd chprio $priority
          set priority {}
          break 
         } else {
        }
       }
     }
$jobmenu add command -label "Downq" -command {jobcmd downq} \
  -underline 0
$jobmenu add command -label "Topq" -command {jobcmd topq} \
  -underline 0
$jobmenu add separator
$jobmenu add command  -label "Copy" -command { jobcmd2 copy } \
  -underline 1
$jobmenu add command  -label "Move" -command { jobcmd2 move } \
  -underline 0
$jobmenu add separator
$jobmenu add command -label "Hold" -command {jobcmd1 hold} \
  -underline 0
$jobmenu add command -label "One Print" -command {jobcmd1 one} \
  -underline 0
$jobmenu add command -label "Release" -command {jobcmd1 release} \
  -underline 0
$jobmenu add command -label "Requeue" -command {jobcmd1 requeue} \
  -underline 2
$jobmenu add separator
$jobmenu add command -label "Delete" -command {jobcmd lprm} \
  -underline 1

# create menu for operations on lpd process
set daemonmenu [menu .menuframe.daemon.menu -tearoff 0 \
    -activebackground $activebg]
$daemonmenu add command  -label "Kill LPD" -underline 0 \
    -command { killserver }
$daemonmenu add command -label "Start LPD" -underline 0 \
    -command { restartserver }
$daemonmenu add command -label "HUP LPD" -underline 0 \
    -command { hupserver }
                 
# create menu for refreshing listbox with status information 
set refrmenu [menu .menuframe.refr.menu -tearoff 0 -activebackground $activebg]
$refrmenu add checkbutton -label " Refresh" -variable refresh \
 -onvalue normal -offvalue disabled  -command \
 {   $refrmenu entryconfigure 2 -state $refresh
     $refrmenu entryconfigure 3 -state $refresh
     $refrmenu entryconfigure 4 -state $refresh
     $refrmenu entryconfigure 5 -state $refresh 
 }

$refrmenu add separator
$refrmenu add radiobutton -label " 5  sec" -variable refreshrate -value 5000 \
  -state $refresh
$refrmenu add radiobutton -label " 10 sec" -variable refreshrate -value 10000 \
  -state $refresh
$refrmenu add radiobutton -label " 15 sec" -variable refreshrate -value 15000 \
  -state $refresh 
$refrmenu add radiobutton -label " 20 sec" -variable refreshrate -value 20000 \
  -state $refresh

# create help menu 
set helpmenu [menu .menuframe.help.menu -tearoff 0 -activebackground $activebg]

#the procedure "dialhelp" is defined in file "help.tcl"
#the german helptext is located in "hilfe.txt"
#the english one can be found in "help.txt"
$helpmenu add radiobutton -label "German  Help" -variable language -value german \
  -command {
     set index 3
     foreach element $helpchapterlines(german) { 
       $helpmenu entryconfigure $index -label [lindex $element 0] \
        -command {
          set entrynr [$helpmenu index active]
          set indexnr [expr $entrynr-3]
          dialhelp .help "Tk-Lpc $helptitle(german)" $helpfile(german) \
           [lindex [lindex $helpchapterlines(german) $indexnr] 1]
        }  
       incr index
     }
  }
$helpmenu add radiobutton -label "English Help" -variable language -value english \
  -command {
     set index 3
     foreach element $helpchapterlines(english) { 
       $helpmenu entryconfigure $index -label [lindex $element 0] \
         -command {
           set entrynr  [$helpmenu index active]
           set indexnr [expr $entrynr-3]
           dialhelp .help "Tk-Lpc $helptitle(english)" $helpfile(english) \
            [ lindex [lindex $helpchapterlines(english) $indexnr]  1] 
         }
      incr index
     }
  }
$helpmenu add separator
 
foreach index $helpchapterlines($language) {
  $helpmenu add command -label [lindex $index 0] \
    -command {
           set entrynr  [$helpmenu index active]
           set indexnr [expr $entrynr-3]
           dialhelp .help "Tk-Lpc $helptitle($language)" $helpfile($language) \
            [ lindex [lindex $helpchapterlines($language) $indexnr]  1] 
         }
}


#create buttons
#the procedures associated to the button option "-command" 
# can be found in file buttons.tcl
frame .buttonframe -height 2 -borderwidth 1 -relief groove -bg $backgr3
pack .buttonframe -side top -anchor w -fill x
button .buttonframe.viewall -text ViewAll -relief raised -state normal\
   -bg $butcol -highlightbackground $backgr3 -activebackground $activebg \
   -bg $butcol -command { viewallproc} 

button .buttonframe.selectpr -text GetSelect -relief raised -pady 3\
   -bg $butcol -highlightbackground $backgr3 -activebackground $activebg \
   -command { selectproc }
                       
button .buttonframe.selectall -text SelectAll -relief raised \
       -highlightbackground $backgr3 -activebackground $activebg \
       -bg $butcol -command {selectallproc}

button .buttonframe.remote -text Local -relief raised \
   -highlightbackground $backgr3 -activebackground $activebg \
    -bg $butcol -command { remoteproc }

pack .buttonframe.viewall .buttonframe.selectpr\
  .buttonframe.selectall  .buttonframe.remote -side left 

label .buttonframe.daemon -text Daemon: -relief flat \
  -background $backgr3 

set lpd [exec lpc lpd]

if {[ string compare "LPD is not active." $lpd] == 0} { 
   label .buttonframe.daemonpid -textvariable lpd  -relief sunken \
	   -padx 5 -bg red -foreground white
} else {
   label .buttonframe.daemonpid -textvariable lpd  -relief sunken \
	   -padx 5 -bg cornsilk -foreground forestgreen 
} 
frame .buttonframe.rightborder -width 5
pack .buttonframe.rightborder .buttonframe.daemonpid .buttonframe.daemon \
  -side right -padx 5 -ipady 0 

#create frame to hold the listbox and the text widgets
set w [frame .rahmen -relief flat -bd 3 -bg $backgr]
pack $w -side top -fill both -expand 1

set lpcheader "Printername          Loc  Jobs Queuing Printing \
  PID  Pri  Job Host            "
set lpqheader1 "  Rank Owner    Pr  Job Host        Files                 \
%  Form  Size Time"
set lpqheader2 "Queue   Dev   Status    Job Files            \
        User          PP %    Blks  Cp Rnk" 

#create first listbox with scrollbar
frame $w.main  -borderwidth 3 -relief groove 
pack $w.main  -side top  -fill both -anchor w -expand true  

#this label is only displayed in jobmode and holds the printename
label $w.main.title1 -anchor w -height 1 -pady 2\
   -width 80 -padx 5 -font $fontmain -bg $backgr4 -relief raised\
   -borderwidth 1

label $w.main.header -textvariable lpcheader -anchor w -height 1\
   -width 80 -padx 5 -pady 2 -font $fontmain -bg $backgr \
   -relief groove
pack $w.main.header -side top -fill x -anchor w

#create listbox with associated scrollbar to hold queue and job information
listbox $w.main.lpcstatlistbox -relief flat -borderwidth 1 \
	-yscrollcommand "$w.main.scroll set" -font $fontmain\
        -selectmode multiple -bg $backgr2  -selectbackground $activebg\
        -highlightthickness 2 -height $listboxheight
scrollbar $w.main.scroll -command "$w.main.lpcstatlistbox yview" -width 10\
        -bg $backgr -highlightbackground $backgr2 
pack $w.main.lpcstatlistbox -side left -fill both -expand true
pack $w.main.scroll -side right -fill y


#Create second listbox with scrollbar. This one is to hold the 
#contents of the remote printer queue in the jobmode 
frame $w.main2  -borderwidth 3 -relief groove 
#this label is only displayed in the jobmode and holds the printename
label $w.main2.title2 -anchor w -height 1 -pady 2\
   -width 80 -padx 5 -font $fontmain -bg $backgr4 -relief raised \
   -borderwidth 1
label $w.main2.header -textvariable lpqheader -anchor w -height 1\
   -width 80 -padx 5 -pady 2 -font $fontmain -bg $backgr \
   -relief groove
listbox $w.main2.lpcstatlistbox -relief flat -borderwidth 1 \
	-yscrollcommand "$w.main2.scroll set" -font $fontmain\
        -selectmode multiple -bg $backgr2 -selectbackground $activebg \
        -highlightthickness 2 -height 4
scrollbar $w.main2.scroll -command "$w.main2.lpcstatlistbox yview" -width 10\
        -bg $backgr -highlightbackground $backgr2 

#create label and text widget for aditional information
label $w.messages -text "Messages: " -anchor w -height 1 -padx 10\
  -bg $backgr -font $fontmain
pack $w.messages  -side top  -anchor w -fill x 

frame $w.info -borderwidth 2 -relief groove 
pack $w.info -side bottom -fill both  -anchor w -expand false
text $w.info.lpcinftextbox -relief flat -borderwidth 1 -state normal \
   -yscrollcommand "$w.info.scroll set" -height 4 -font $fontmain \
   -bg $backgr2 -highlightthickness 2
scrollbar $w.info.scroll -command "$w.info.lpcinftextbox yview" -width 10\
    -bg $backgr  -highlightthickness 1
pack $w.info.lpcinftextbox -side left -fill both   -expand true
pack $w.info.scroll -side right -fill y -before $w.info.lpcinftextbox

#create bindings
#the bindings to switch between jobmode and printermode
bind $w.main.lpcstatlistbox <Double-1> {double-left %y}
bind $w.main.lpcstatlistbox <Double-3> {double-right }
bind $w.main2.lpcstatlistbox <Double-3> {double-right }

#the bindings for the refresh function
bind . <FocusIn> {
  set loop 0 }
bind . <FocusOut> {
     set type %d
     if {($refresh == "normal") && ( $type != "NotifyNonlinear")} {
      while {1} {
       set cancelid [after $refreshrate { 
         if {$mode == "prmode"} {
           lpc-stat $selectedprs
           set lpd [exec lpc lpd]
         } else {
           lpc-lpq $globalprname
           set lpd [exec lpc lpd]
	 }
	 if {[ string compare "LPD is not active." $lpd] == 0} { 
	     .buttonframe.daemonpid configure -background red -foreground white
	 } else {
	     .buttonframe.daemonpid configure -background cornsilk \
		 -foreground forestgreen
	 }
         set loop 1
       }]
       tkwait variable loop
       if {$loop != 1} {
	   after cancel $cancelid
	   return
       }
      }
     }   
}

# these bindings set the right remote status in jobmode 
# depending on a mouse-click in the upper or lower listbox  
bind $w.main.lpcstatlistbox <Button-1> {
    if {$mode == "jobmode"} {set remote 0}
}
bind $w.main2.lpcstatlistbox <Button-1> {
  set remote 1
} 

# test for root to open Daemon menu
regexp {[0-9]} [exec id] userid
if {$userid==0} {
 .menuframe.daemon configure -state normal
}

#initialise listbox and text widget
set mode prmode
lpc-stat all
selection clear .


