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

###############################################################
# TkNet - Main Module
# Charlie KEMPSON - charlie@siren.demon.co.uk
# http://public.logica.com/~kempsonc/tknet.htm
# Version 1.1
###############################################################

###############################################################
#
#    Copyright (c) 1995-1996 Charlie Kempson
#
#    This program is free software; you can redistribute it 
#    and/or modify it under the terms of the GNU General 
#    Public License as published by the Free Software 
#    Foundation (version 2 of the License).
#
#    This program is distributed in the hope that it will 
#    be useful, but WITHOUT ANY WARRANTY; without even the 
#    implied warranty of MERCHANTABILITY or FITNESS FOR A 
#    PARTICULAR PURPOSE.  See the GNU General Public License 
#    for more details.
#
#    For a copy of the GNU General Public License, write to the 
#    Free Software Foundation, Inc., 675 Mass Ave, Cambridge, 
#    MA 02139, USA.
###############################################################

###############################################################
# Set include path and test variables (p: is my NT home)
if [string match "P:*" [set env(HOME)]] {
   # Flag indicating whether we're running a test under NT
   set test_nt 1
   set GLOBAL_INCLUDE "/usr/local/lib/TkNet"
} else {
   set test_nt 0
   set GLOBAL_INCLUDE "/usr/local/lib/TkNet"
}

###############################################################
# Set global variables

set TKNET_DEFAULT_USER  "root"
set TKNET_DEFAULT_USER_HOME  "/tmp"
set TKNET_HELP_FILE     "$GLOBAL_INCLUDE/tknet.help"

# Microhelp strings
set gs_help_connect     "Press to connect to the network"
set gs_help_disconnect  "Press to disconnect from the network"
set gs_help_quit        "Press to quit TkNet"
set gs_help_mail        "Press to invoke the mail window"
set gs_help_log         "Press to invoke the log window"
set gs_help_deliver     "Press to deliver/retrieve mail"
set gs_help_time        "Length of time connected for"
set gs_help_through     "Average link throughput"
set gs_help_status      "Connection status (click to toggle)"
set gs_help_mail        "Mail Status - click to view"
set gs_help_popup       "Click R-H button for menu"

# Flags
set gb_debug_on         0
set gi_clear_message    0

# Initialise Flags
set g_log_file          ""
set g_current_connect   ""
set g_network_interface "ppp0"

# Get the user's environment
if [catch {set env(HOME)} TKNET_USER_HOME] {
   set TKNET_USER_HOME $TKNET_DEFAULT_USER_HOME
}
if [catch {set env(USER)} TKNET_USER] {
   set TKNET_USER $TKNET_DEFAULT_USER
}

# Window position information
set TKNET_GEOMETRY      "+15+15"
set TKNET_NET_GEOMETRY "+408+51"

###############################################################
# Set a message into the main window message line
proc Debug {text} {

   # Globals
   global g_log_file gb_debug_on

   if {$gb_debug_on == 1} {

      # Log the message to file (if not help)
      set log_file_id [ open $g_log_file a+ ]

      # Write the message to file
      puts -nonewline $log_file_id "DEBUG : "
      puts $log_file_id $text
      flush $log_file_id

      # Close log file
      close $log_file_id
   }
}

###############################################################
# The procedure to read other tcl files stored in
# /usr/local/lib/TkNet/modules
proc Source_Other { filename } {

   # Globals
   global GLOBAL_INCLUDE

   # Look in the global area for files
   if [file exists "$GLOBAL_INCLUDE/modules/$filename"] {
      uplevel #0 source "$GLOBAL_INCLUDE/modules/$filename"
   } else {
      # FILE MISSING FROM /usr/local/lib/TkNet/modules
      Info_Dialog . "File $filename missing from \
$GLOBAL_INCLUDE/modules.\n\TkNet is not installed properly!"
      # FILE MISSING FROM /usr/local/lib/TkNet/modules
   }
}

###############################################################
# The procedure for creating the main popup menu
proc Create_Popup_Menu {} {
   Debug "Create_Popup_Menu"

   global FONT_NORMAL g_spool_file_size

   # Check for existence
   if [winfo exists .popup_menu] {destroy .popup_menu}

   # Create the popup menu
   menu .popup_menu
      Create_Connect_Menu_Items .popup_menu
      Create_Disconnect_Menu_Items .popup_menu
      .popup_menu add command -label "Deliver Mail" -command { \
         Deliver_Mail } -accelerator "Ctrl-M" -font \
         $FONT_NORMAL -underline 8
      .popup_menu add command -label "Biff (check mail)" \
         -command { set g_spool_file_size 0 ; Biff 1} \
         -accelerator "Ctrl-B" -font $FONT_NORMAL -underline 0
      .popup_menu add separator
      .popup_menu add command -label "View Log ..." -command \
         { Show_Log } -font $FONT_NORMAL -underline 0
      .popup_menu add command -label "Mail ..." -command \
         { Show_Mail } -font $FONT_NORMAL -underline 0
      .popup_menu add separator
      .popup_menu add command -label "Quit" \
         -accelerator "Ctrl-Q" -command { Quit } -font \
         $FONT_NORMAL -underline 0

   # Add a title for the tearoff
   wm title .popup_menu TkNet
}

###############################################################
# The procedure for creating the main menu
proc Create_File_Menu {} {
   Debug "Create_File_Menu"

   global FONT_NORMAL

   # Check for existence
   if [winfo exists .mbar.file.menu] {destroy .mbar.file.menu}

   menu .mbar.file.menu -tearoff 0
      Create_Connect_Menu_Items .mbar.file.menu
      Create_Disconnect_Menu_Items .mbar.file.menu
      .mbar.file.menu add command -label "Deliver Mail" -command { \
         Deliver_Mail } -font $FONT_NORMAL -underline 8
      .mbar.file.menu add command -label "Biff (check mail)" \
         -command { set g_spool_file_size 0 ; Biff 1} \
         -font $FONT_NORMAL -underline 0
      .mbar.file.menu add separator
      .mbar.file.menu add command -label "Quit" \
         -command { Quit } -font $FONT_NORMAL -underline 0
}
   
###############################################################
# The procedure for creating the connect items on both the
# fie menu and the popup menu
proc Create_Connect_Menu_Items { base } {
   Debug "Create_Connect_Manu_Items"

   global FONT_NORMAL g_number_sessions gi_current_session

   # Create the popup menu
   for {set count 1} {$count <= $g_number_sessions} {incr count} \
   {
      # Define globals
      global g_script_$count

      # Create the label and the accelerator
      set label [subst $[subst g_script_${count}(label)]]
      set accel "Ctrl-$count"

      # Create a row for the item
      $base add radiobutton -label $label -command " \
         set gi_current_session $count; Net_Connect" \
         -accelerator $accel -font $FONT_NORMAL -variable \
         gi_current_session -value $count
   }
   $base add separator
}

###############################################################
# The procedure for creating the disconnect items on both the
# file menu and the popup menu
proc Create_Disconnect_Menu_Items { base } {
   Debug "Create_Disconnect_Manu_Items"

   global FONT_NORMAL g_number_sessions gi_current_session

   # Create the submenu
   $base add cascade -label Disconnect -font $FONT_NORMAL \
      -underline 0 -menu $base.disconnect
   menu $base.disconnect -tearoff 0

   # Now for the items
   for {set count 1} {$count <= $g_number_sessions} {incr count} \
   {
      # Define globals
      global g_script_$count

      # Create the label and the accelerator
      set label [subst $[subst g_script_${count}(label)]]

      # Create a row for the item
      $base.disconnect add radiobutton -label $label \
         -command "set gi_current_session $count; Net_Disconnect" \
         -font $FONT_NORMAL -variable \
         gi_current_session -value $count
   }
   $base add separator
}

###############################################################
# Set a message into the main window message line
proc Set_Message { text colour } {
   # Debug "Set_Message"

   # Globals
   global HELP_COLOUR g_log_file gi_clear_message \
      g_message_display_time

   # Cancel current clear command
   after cancel $gi_clear_message

   # Set the text input into the message line
   .message_text configure -text $text -foreground \
      $colour

   if [string compare $colour $HELP_COLOUR] {

      # Log the message to file (if not help)
      set log_file_id [ open $g_log_file a+ ]

      # Write the message to file
      puts $log_file_id $text
      flush $log_file_id

      # Close log file
      close $log_file_id
   }

   # Set a timer to clear the message after 10 secs
   if {$text != "" && $g_message_display_time > 0} {
      set run_after [expr $g_message_display_time * 1000]
      set gi_clear_message [after $run_after \
          {Set_Message "" $HELP_COLOUR}]
   }
}

###############################################################
# Set a microhelp message into the main window message line
proc Set_Microhelp { text } {
   # Debug "Set_Microhelp"

   # Globals
   global gb_show_microhelp HELP_COLOUR
   
   # Test the global flag
   if {$gb_show_microhelp == 1} {
      Set_Message $text $HELP_COLOUR
   }
}

###############################################################
# Routine to show (or hide) the messages display
proc Show_Messages { show } {
   Debug "Show_Messages"

   global DEFAULT_PADDING

   if {$show == 1} {
      # Already created, just pack it
      pack .message_text -side top -padx $DEFAULT_PADDING \
         -pady $DEFAULT_PADDING -fill x
      wm geometry . ""
   } else {
      # Already managed, hide it!
      pack forget .message_text
      wm geometry . ""
   }
}

###############################################################
# Routine to show (or hide) the buttons
proc Show_Buttons { show } {
   Debug "Show_Buttons"

   if {$show == 1} {
      # Already created, just pack it
      pack .button_frame -side bottom -fill x
      wm geometry . ""
   } else {
      # Already managed, hide it!
      pack forget .button_frame
      wm geometry . ""
   }
}

###############################################################
# Routine to show (or hide) the status display
proc Show_Status { show } {
   Debug "Show_Status"

   # Globals
   global gb_display_compact DEFAULT_PADDING

   set frame .status_fr.connect_fr
   pack forget $frame $frame.status_label $frame.status

   if {$show == 1} {
      # If view is compact, place alongside other icons
      # Otherwise, place in separate row.
      if {$gb_display_compact == 1} {
         # Already created, just pack it
         pack $frame.status -side left -padx $DEFAULT_PADDING \
            -pady $DEFAULT_PADDING
         pack $frame -side left -anchor w
      } else {
         # Already created, just pack it
         pack $frame.status_label $frame.status -side left -padx \
            $DEFAULT_PADDING -pady $DEFAULT_PADDING
         pack $frame -side top -anchor w
      }
   }
   wm geometry . ""
}

###############################################################
# Routine to show (or hide) the status display
proc Show_Mail_Row { show } {
   Debug "Show_Mail_Row"

   # Globals
   global gb_display_compact DEFAULT_PADDING

   set frame .status_fr.mail_fr
   pack forget $frame $frame.mail_label $frame.mail

   if {$show == 1} {
      # If view is compact, place alongside other icons
      # Otherwise, place in separate row.
      if {$gb_display_compact == 1} {
         # Already created, just pack it
         pack $frame.mail -side left -padx $DEFAULT_PADDING \
            -pady $DEFAULT_PADDING
         pack $frame -side left -anchor w
      } else {
         # Already created, just pack it
         pack $frame.mail_label $frame.mail -side left -padx \
            $DEFAULT_PADDING -pady $DEFAULT_PADDING
         pack $frame -side top -anchor w
      }
   }
   wm geometry . ""
}

###############################################################
# Routine to show (or hide) the time display
proc Show_Time { show } {
   Debug "Show_Time"

   # Globals
   global gb_display_compact DEFAULT_PADDING

   set frame .status_fr.time_fr
   pack forget $frame $frame.time_label $frame.time

   if {$show == 1} {
      # If view is compact, place alongside other icons
      # Otherwise, place in separate row.
      if {$gb_display_compact == 1} {
         # Already created, just pack it
         pack $frame.time -side left -padx $DEFAULT_PADDING \
            -pady $DEFAULT_PADDING
         pack $frame -before .status_fr.connect_fr \
            -side left -anchor w
      } else {
         # Already created, just pack it
         pack $frame.time_label $frame.time -side left -padx \
            $DEFAULT_PADDING -pady $DEFAULT_PADDING
         pack $frame -after .status_fr.connect_fr \
            -side top -anchor w
      }
   }
   wm geometry . ""
}

###############################################################
# Routine to show (or hide) the buttons
proc Show_Stats { show } {
   Debug "Show_Stats"

   # Globals
   global gb_display_compact DEFAULT_PADDING

   set frame .status_fr.stats_frame
   pack forget $frame $frame.label $frame.stats

   if {$show == 1} {
      # If view is compact, place alongside other icons
      # Otherwise, place in separate row.
      if {$gb_display_compact == 1} {
         # Already created, just pack it
         pack $frame.stats -side left -padx $DEFAULT_PADDING \
            -pady $DEFAULT_PADDING
         pack $frame -after .status_fr.connect_fr \
            -side left -anchor w
      } else {
         # Already created, just pack it
         pack $frame.label $frame.stats -side left -padx \
            $DEFAULT_PADDING -pady $DEFAULT_PADDING
         pack $frame -after .status_fr.connect_fr \
            -side bottom -anchor w
      }
   }
   wm geometry . ""
}

###############################################################
# NEXT THE INTERFACE CODE, I.E. THE STUFF THAT BUILDS THE
# INTERFACE
###############################################################

# Source the modules
Source_Other global.tcl
Source_Other setup.tcl
Source_Other utils.tcl
Source_Other ppp.tcl
Source_Other command.tcl
Source_Other dialog.tcl
Source_Other help.tcl
Source_Other colour.tcl
Source_Other options.tcl
Source_Other scr_list.tcl
Source_Other launcher.tcl
Source_Other scripts.tcl
Source_Other connect.tcl
Source_Other log.tcl
Source_Other mail.tcl
Source_Other netstat.tcl
Source_Other time.tcl
Source_Other graph.tcl

# Source the setup files
Source_Setup 0
Source_Options 0
Source_Launcher_Setup 0
Source_Script_Setup 0
PPP_Source_Scripts 0


# And the help file
if [file exists $TKNET_HELP_FILE] {
   source $TKNET_HELP_FILE
}

# Delete the log file if one exists
if [file exists $g_log_file] {
   if {$test_nt != 1} {
      catch {exec rm -f $g_log_file} text 
   }
}

# Create a new file and modify permissions
if {$test_nt != 1} {
   catch {eval exec touch $g_log_file} text
   catch {eval exec chmod 777 $g_log_file} text
}

###############################################################
# Set application defaults

# Fonts
option add *font $FONT_NORMAL startupFile

# Highlight
option add *highlightThickness 0

# Colours
option add *background  $DEFAULT_COLOUR startupFile
option add *Text.background  $TEXT_COLOUR startupFile
option add *Entry.background  $TEXT_COLOUR startupFile

###############################################################
# Check for version
if {$tcl_version < 7.4 || $tk_version < 4.0} {
   # Out-of-date wish binaries...
   puts "VERSION ERROR! Found : Tcl $tcl_version, Tk $tk_version.
You must have at least Tcl 7.4 and Tk 4.0! TkNet will now exit."
   exit
}

###############################################################
# Create a dialogue
wm title . "TkNet - Network Manager"
wm iconname . "Net Down"
wm geometry . $TKNET_GEOMETRY
wm minsize . 40 5
#wm iconbitmap . @/usr/include/X11/pixmaps/netdn.xpm
#wm resizable . 0 0 
wm protocol . WM_DELETE_WINDOW Quit
if {$gb_startup_iconified == 1} {wm iconify .}

###########################################################################
# Create Menu Bar
frame .mbar -relief raised -bd 2
#frame .dummy -width 10c -height 5m
pack .mbar -side top -fill x

menubutton .mbar.file -text File -font $FONT_NORMAL -underline 0 \
   -menu .mbar.file.menu
menubutton .mbar.windows -text Windows -font $FONT_NORMAL \
   -underline 0 -menu .mbar.windows.menu
menubutton .mbar.options -text Options -font $FONT_NORMAL \
   -underline 0 -menu .mbar.options.menu
menubutton .mbar.launcher -text Launcher -font $FONT_NORMAL \
   -underline 0 -menu .mbar.launcher.menu
menubutton .mbar.help -text Help -font $FONT_NORMAL -underline 0 \
   -menu .mbar.help.menu
pack .mbar.file .mbar.windows .mbar.options .mbar.launcher \
   -side left
pack .mbar.help -side right

# Create the file menu from stored script items
Create_File_Menu

menu .mbar.windows.menu -tearoff 0
   .mbar.windows.menu add command -label "View Log ..." -command \
      { Show_Log } -font $FONT_NORMAL -underline 0
   .mbar.windows.menu add command -label "Mail ..." -command \
      { Show_Mail } -font $FONT_NORMAL -underline 0
   .mbar.windows.menu add command -label "Network ..." -command \
      { Show_Network } -font $FONT_NORMAL -underline 0

menu .mbar.options.menu -tearoff 0
   .mbar.options.menu add command -label "Environment ..." -command \
      { Show_Setup } -font $FONT_NORMAL -underline 0
   .mbar.options.menu add command -label "Display Settings..." -command \
      { Show_Display_Setup } -font $FONT_NORMAL -underline 0
   .mbar.options.menu add command -label "Startup Options..." -command \
      { Show_Startup_Setup } -font $FONT_NORMAL -underline 0
   .mbar.options.menu add command -label "Connect & Mail..." -command \
      { Show_Connect_Setup } -font $FONT_NORMAL -underline 0
   .mbar.options.menu add command -label "Script Setup ..." -command \
      { Show_Script_Setup } -font $FONT_NORMAL -underline 4
   .mbar.options.menu add separator
   .mbar.options.menu add checkbutton -label \
      "Confirm quit" -variable \
      gb_confirm_quit -font $FONT_NORMAL -underline 0
   .mbar.options.menu add checkbutton -label \
      "Save window positions" -variable \
      gb_save_positions -font $FONT_NORMAL -underline 5
   .mbar.options.menu add separator
   .mbar.options.menu add command -label "Reset options to defaults" \
      -command { Source_Options 0 } -font $FONT_NORMAL -underline 17
   .mbar.options.menu add command -label "Save options" -command \
      { Save_Options } -font $FONT_NORMAL -underline 5

menu .mbar.launcher.menu -tearoff 0
   .mbar.launcher.menu add command -label "Setup ..." -command \
      { Show_Launcher_Setup } -font $FONT_NORMAL -underline 0
   .mbar.launcher.menu add separator
   .mbar.launcher.menu add command -label \
      "Show toolbar" -command {Show_Launcher \
      $gb_launcher_window} -font $FONT_NORMAL -underline 6
   .mbar.launcher.menu add command -label \
      "Hide toolbar" -command {if [winfo exists .launcher_window] {
      destroy .launcher_window}} -font $FONT_NORMAL -underline 6
   .mbar.launcher.menu add separator
   .mbar.launcher.menu add checkbutton -label \
      "Show toolbar at startup" -variable \
      gb_show_launcher_at_startup -font $FONT_NORMAL \
      -underline 1
   .mbar.launcher.menu add checkbutton -label \
      "Toolbar uses separate window" -variable \
      gb_launcher_window -font $FONT_NORMAL -underline 22 \
      -command {Show_Launcher $gb_launcher_window}
   .mbar.launcher.menu add checkbutton -label \
      "Toolbar vertically oriented" -variable \
      gb_launcher_vertical -font $FONT_NORMAL -underline 8 \
      -command {Show_Launcher $gb_launcher_window}
   .mbar.launcher.menu add separator
   .mbar.launcher.menu add radiobutton -label \
      "Show Items as labels" -variable \
      gi_launcher_button_style -font $FONT_NORMAL \
      -underline 5 -value 1 -command {Show_Launcher \
      $gb_launcher_window}
   .mbar.launcher.menu add radiobutton -label \
      "Show Items as bitmaps" -variable \
      gi_launcher_button_style -font $FONT_NORMAL \
      -underline 5 -value 2 -command {Show_Launcher \
      $gb_launcher_window}
   .mbar.launcher.menu add radiobutton -label \
      "Show both labels and bitmaps" -variable \
      gi_launcher_button_style -font $FONT_NORMAL \
      -underline 5 -value 3 -command {Show_Launcher \
      $gb_launcher_window}

menu .mbar.help.menu -tearoff 0
   .mbar.help.menu add command -label "Contents" -command \
      {MH_MiniHelp Contents} \
      -font $FONT_NORMAL -underline 0
   .mbar.help.menu add command -label "Site-specific" -command \
      {MH_MiniHelp SiteSpecific} \
      -font $FONT_NORMAL -underline 0
   .mbar.help.menu add command -label "On Image Types" -command \
      {MH_MiniHelp Images} \
      -font $FONT_NORMAL -underline 3
   .mbar.help.menu add command -label "On Launcher" -command \
      {MH_MiniHelp Launcher} \
      -font $FONT_NORMAL -underline 3
   .mbar.help.menu add command -label "About" -command \
      {MH_MiniHelp About} \
      -font $FONT_NORMAL -underline 0
   .mbar.help.menu add separator
   .mbar.help.menu add checkbutton -label \
      "Show Microhelp" -variable gb_show_microhelp \
      -font $FONT_NORMAL -underline 0
   .mbar.help.menu add command -label "Edit..." -command {MH_EditHelp} \
      -underline 0

tk_menuBar .mbar .mbar.file .mbar.windows .mbar.options \
   .mbar.launcher .mbar.help

###############################################################
# Create the toolbar
if {$gb_show_launcher_at_startup} {
   Show_Launcher $gb_launcher_window
}

###############################################################
# Create the status panel
frame .status_fr -borderwidth $RIDGE_BORDER -relief groove
pack .status_fr -padx $DEFAULT_PADDING -pady $DEFAULT_PADDING \
   -side top -fill x

# Connected status row
set frame [frame .status_fr.connect_fr]
label $frame.status_label -text "Connection Status " \
   -justify center -width $LABEL_LENGTH
set g_connection_status [button .status_fr.connect_fr.status -text \
   "Disconnected" -highlightbackground $GREEN \
   -font $FONT_BOLD -justify center \
   -background $GREEN -command Toggle_Connection]
bind $g_connection_status <Enter> {Set_Microhelp $gs_help_status}

# Set the pixmap if one is specified
if {$g_disconnected_pixmap != ""} {
   set Image [Create_Image $g_disconnected_pixmap]
   if {$Image != -1} {
      $g_connection_status configure -image $Image
   }
}

# Mail row
set frame [frame .status_fr.mail_fr]
label $frame.mail_label -text "Mail Status" \
   -justify center -width $LABEL_LENGTH
set g_mail_status [button $frame.mail -text \
   "No Mail" -font $FONT_BOLD -justify center -command Run_Mail_Tool]
bind $g_mail_status <Enter> {Set_Microhelp $gs_help_mail}

# Set the pixmap if one is specified
if {$g_nomail_pixmap != ""} {
   set Image [Create_Image $g_nomail_pixmap]
   if {$Image != -1} {
      $g_mail_status configure -image $Image
   }
}


# For stats row
set frame [frame .status_fr.stats_frame]
label $frame.label -text "Link Throughput" -justify \
   center -width $LABEL_LENGTH
canvas $frame.stats
set g_throughput $frame.stats
Graph_Initialise $frame.stats
bind $frame.stats <Enter> {Set_Microhelp $gs_help_through}

# For time row
frame .status_fr.time_fr
label .status_fr.time_fr.time_label -text \
   "Time Connected" -justify center -width $LABEL_LENGTH
set g_connection_time [message .status_fr.time_fr.time -text \
   "00 sec" -font $FONT_BOLD -justify center \
   -aspect 400 -borderwidth $LOWER_BORDER]
bind $g_connection_time <Enter> {Set_Microhelp $gs_help_time}

# Pack the columns
Show_Status 1
Show_Mail_Row $gb_show_mail
Show_Time $gb_show_time
Show_Stats $gb_show_stats

###############################################################
# Create the message label
message .message_text -text \
   "Ok" -font $FONT_NORMAL -justify center \
   -aspect 1500 -foreground $GREEN -relief groove
Show_Messages $gb_show_messages
bind .message_text <Enter> {Set_Microhelp $gs_help_popup}

###############################################################
# Create the buttons below the frame
frame .button_frame -borderwidth $DEFAULT_PADDING
button .button_frame.connect -text Connect -font $FONT_NORMAL \
   -command {Net_Connect}
button .button_frame.disconnect -font $FONT_NORMAL \
    -text Disconnect -command {Net_Disconnect}
button .button_frame.deliver -font $FONT_NORMAL \
    -text Deliver -command {Deliver_Mail}
button .button_frame.quit -font $FONT_NORMAL \
    -text Quit -command {Quit}
button .button_frame.log -font $FONT_NORMAL -text "Log ..." \
    -command {Show_Log}
button .button_frame.mail -font $FONT_NORMAL -text "Mail ..." \
    -command {Show_Mail}
pack .button_frame.quit .button_frame.disconnect \
   .button_frame.connect .button_frame.deliver -side right
pack .button_frame.log .button_frame.mail -side left
Show_Buttons $gb_show_buttons

# Add bindings for microhelp 
bind .button_frame.connect <Enter> {Set_Microhelp $gs_help_connect} 
bind .button_frame.disconnect <Enter> {Set_Microhelp $gs_help_disconnect}
bind .button_frame.quit <Enter> {Set_Microhelp $gs_help_quit}
bind .button_frame.deliver <Enter> {Set_Microhelp $gs_help_deliver} 
bind .button_frame.mail <Enter> {Set_Microhelp $gs_help_mail} 
bind .button_frame.log <Enter> {Set_Microhelp $gs_help_log}

###############################################################
# Add binding for a popup menu (button 3)
Create_Popup_Menu
bind . <Button-3> {Popup_Menu . .popup_menu}
bind . <Button-2> {Popup_Menu . .popup_menu}

###############################################################
# Start background processing
if {$g_network_check_freq > 0} {
   set run_after [ expr $g_network_check_freq * 1000 ]
} else {set run_after 10000}
# Get the default port to check
if {$gi_current_session > 0} {
   # Read from script details
   set script [eval subst \$g_script_${gi_current_session}(connect1)]
   regsub -all {[\ ]+} $script {_} script
   if [info exists gs_ppp_interface_$script] {
      set g_network_interface [eval subst \$gs_ppp_interface_$script]
   } else {
      set g_network_interface [eval subst \$gs_cmd_interface_$script]
   }
} else {
   # Set a default
   set g_network_interface "ppp0"
}
# Start processing
after $run_after Check_For_Connection
after $run_after Biff 0

# Start internal clock mechanism (reduces system load)
# Submitted by George M. Sipe (gsipe@mindspring.com)
Get_Current_Time
after 1000 Time_Ticker

###############################################################
# Determine whether the user has EXPECT installed
if {[info commands "spawn"] == "" && $test_nt == 0} {
   # Info_Dialog . "Expect NOT installed"
   set gb_have_expect 0
} else {
   # Info_Dialog . "Expect installed"
   set gb_have_expect 1
}

###############################################################
# Set a welcome message for the user
Set_Message "$TKNET_USER - Welcome to TkNet 1.1" $RED
after 2000 {Set_Message "by Charlie Kempson" $RED}

###############################################################
# Start throughput calculations, checking first whether
# IP accounting has been enabled...
if [file exists "/proc/net/ip_acct"] {
   # The file exists, check whether rules have been
   # constructed by searching for the default text
   # in the file (IP Accounting Rules)
   set fp [open /proc/net/ip_acct r]
   set text [read $fp]
   if {[string first $text "Rules"] != -1} {
      # Info_Dialog . "IP Accouting Disabled, even though you 
have IP Accounting turned on in the kernel.

Please see the help on IP Accounting for information
about enabling reporting of network traffic in 
Bytes/sec rather than in Packets/sec."
   } else {
      # IP accounting is enabled in the kernel
      # and it appears that accounting rules
      # have been turned on.
      # Info_Dialog . "IP Accouting is ON"
      set g_have_ip_acct 1
      set g_graph_units "Bytes/sec"
   }
} else {
   # The file /proc/net/ip_acct does not exist, therefore
   # we know that IP accounting has not been compiled into
   # the kernel.
   # Info_Dialog . "IP Accouting is OFF"
   set g_graph_units "Pkts/sec"
}
# Now start the statistics gathering process
Update_Statistics

###############################################################
# If the connect-on-startup flag is set, start the connection
if {$gb_connect_on_startup == 1} {Net_Connect}
