#!/usr/X11/bin/wish -f
#
# tkppp - a graphical interface to control a PPP connection.
#
# Copyright 1995  Eric Jeschke (jeschke@cs.indiana.edu)
#
# All rights reserved.  Permission to use, copy, modify, and distribute
# this software and its documentation for any non commercial purposes and
# without fee is hereby granted, provided that the above copyright
# notice appear in all copies. No warranty is provided or implied.
# 
# I disclaim all warranties with regard to this software, including all
# implied warranties of merchantability and fitness. In no event shall I
# be liable for any special, indirect or consequential damages or any
# damages whatsoever resulting from loss of use, data or profits,
# whether in an action of contract, negligence or other tortuous action,
# arising out of or in connection with the use or performance of this
# software.
#
# Comments, bugfixes, patches welcome...
#
# RCS Version Info:
set rcsid "\$Id$"

#
# CHANGES CREDIT
#   - Show IP address used (jmmadiso@iupui.edu)
#

##############################################################
#                      System variables                      #
##############################################################
#
# These are the bitmaps that show the status of the PPP link.
#
#set bitmaps       @/usr/include/X11/bitmaps
set bitmaps       @.
set pppup_bitmap  $bitmaps/arrup
set pppdn_bitmap  $bitmaps/arrdown

# See tkppprc for a description of these settings...
set ppp_on           ppp-on
set ppp_off          ppp-off
set wait_interval    60
set check_interval   10
set auto_minimize    0
set auto_maximize    0
set auto_ping        0
set ping_interval    60
set ping_command     {ping -c 1 $ipaddr >& /dev/null}
set show_hostname    0

##############################################################
#                      Widget Resources                      #
##############################################################
#
# Common resources.
# --- Class ---
option add *Button*cursor:		hand2		widgetDefault
option add *Entry.font			9x15		widgetDefault
option add *Label.font			8x13		widgetDefault
option add *Menu.font		-Adobe-Helvetica-Medium-R-Normal--*-120-* \
							widgetDefault
option add *Menubutton.font	-Adobe-Helvetica-Bold-R-Normal--*-120-* \
							widgetDefault
option add *Text.font			8x13		widgetDefault
option add *ComboBox.font		9x15		userDefault

# --- Specific ---
option add *ipaddr.font			9x15
option add *caption.font	-Adobe-Helvetica-Medium-R-Normal--*-120-* \
							widgetDefault
option add *msg.font		-Adobe-Helvetica-Bold-R-Normal--*-140-* \
							widgetDefault

# Color resources
if {[tk colormodel .] == "color"} {
# --- Class ---
  option add *background		navajowhite3	widgetDefault
  option add *Frame.background		navajowhite3	widgetDefault
  option add *Entry.background		navajowhite2	widgetDefault
  option add *Label.background		navajowhite3	widgetDefault
  option add *Menu.background		navajowhite2	widgetDefault
  option add *Menubutton.background	navajoWhite3	widgetDefault
  option add *Scrollbar.background	gray70		widgetDefault
  option add *Scrollbar.foreground	gray		widgetDefault
  option add *Text.background		navajowhite2	widgetDefault

# --- Specific ---
  option add *ipaddr.background		navajowhite2	widgetDefault
  option add *bitmap.background		navajowhite2	widgetDefault
  option add *msg.background		navajowhite3	widgetDefault
}

#---------- You shouldn't need to edit below this line ------------

set aboutmsg {
tkppp v1.3
Copyright 1995
Eric Jeschke

Please send bug reports (details!), enhancements, patches,
comments, etc. to: jeschke@cs.indiana.edu

Thanks for using tkppp!
}

set linkstatus    -1
set wait_flag      0
set statusmsg     {Initializing...}

proc quit {} {
    destroy .
}

proc status {msg} {
   .status.msg delete 0 1000
    .status.msg insert end $msg
    update
}

proc about {} {
    global aboutmsg
    tk_dialog .about "tkppp" $aboutmsg info 0 OK
}

proc setstatus {} {
    global linkstatus statusmsg ipaddr show_hostname
    if {$linkstatus} {
        set statusmsg "PPP is DOWN"
    } else {
        # Get IP address used.
        catch {exec ifconfig | grep inet.\*P-t-P | \
                   sed s/inet.\*r// | sed s/P-t-P.\*//} ipaddr
        set ipaddr [string trim $ipaddr " "]
        .top.addr.ipaddr config -text $ipaddr

        if {!$show_hostname} {
            set statusmsg "PPP is UP"
        } else {
            # Get IP name used.
            if [catch {exec nslookup $ipaddr | grep Name | sed s/Name://} \
                            ipname] {
                set statusmsg "\[nslookup error\]"
            } else {
                set ipname [string trim $ipname " "]
                set statusmsg "\[ $ipname \]"
            }
        }
    }
    status $statusmsg
}

proc ppp-stat {flag} {
    global linkstatus check_interval auto_minimize auto_maximize \
            pppup_bitmap pppdn_bitmap ipaddr show_hostname wait_flag

    # Check status of PPP link: 0=up, 1=down 
#    puts stdout "Checking status ..."
    set oldstatus $linkstatus
#    set linkstatus [catch {exec ps augx | grep pppd | grep -vq grep}]
    set linkstatus [catch {exec ifconfig | grep ppp0} result]
    if {$oldstatus != $linkstatus} {
        set wait_flag 0
        if {!$linkstatus} {
            .top.bitmap config -bitmap $pppup_bitmap
            wm iconbitmap . $pppup_bitmap

            setstatus
            if {$auto_minimize} {wm iconify .}
        } else {
            .top.bitmap config -bitmap $pppdn_bitmap
            wm iconbitmap . $pppdn_bitmap
            setstatus
            if {$auto_maximize} {wm deiconify .}
        }
    }
    if {$flag} {
        after [expr {$check_interval * 1000}] ppp-stat $flag
        return
    }
    if {$wait_flag} {after 2000 ppp-stat 0}
}

proc ping {} {
    global linkstatus ipaddr statusmsg auto_ping ping_command ping_interval
    if {!$linkstatus && $auto_ping} {
        status "Pinging ..."
#        puts stdout "Pinging ..."
        catch {eval "exec $ping_command"}
        status $statusmsg
    }
    after [expr {$ping_interval * 1000}] ping
}

proc wait-status {flag} {
    global wait_flag wait_interval
    set wait_flag $flag
    if {$flag} {
        after [expr {$wait_interval * 1000}] {wait-status 0}
        ppp-stat 0
    } else {
        setstatus
    }
}

proc ppp-on {} {
    global ppp_on
    status "Starting PPP..."
    if [catch {exec $ppp_on &} errtxt] {
        error $errtxt
    }
    wait-status 1
}

proc ppp-off {} {
    global ppp_off
    status "Terminating PPP..."
    if [catch {exec $ppp_off &} errtxt] {
        error $errtxt
    }
    wait-status 1
}

proc ppp-toggle {} {
    global linkstatus
    if {$linkstatus} ppp-on else ppp-off
}

proc addmenu {hdr lbl cmd} {
    set w .menu.$hdr
    $w.m add command -label $lbl -command $cmd
}

proc addscript {lbl cmd} {
    addmenu pgms $lbl $cmd
}

proc createwindow {} {
    global pppdn_bitmap auto_minimize auto_maximize show_hostname \
           auto_ping

    . config -bg [option get . background Toplevel]

    frame .menu -relief raised -borderwidth 1
    pack .menu -side top -fill x

    set w .menu.ppp
    menubutton $w -text "PPP" -menu $w.m
    menu $w.m
    $w.m add command -label "On"  -command ppp-on
    $w.m add command -label "Off" -command ppp-off
    $w.m add sep
    $w.m add command -label "Exit" -command quit

    set w .menu.options
    menubutton $w -text "Options" -menu .menu.options.m
    menu $w.m
    $w.m add check -label "Show Hostname" -variable show_hostname \
                   -command setstatus
    $w.m add check -label "Auto Minimize" -variable auto_minimize
    $w.m add check -label "Auto Maximize" -variable auto_maximize
    $w.m add check -label "Auto Ping"     -variable auto_ping

    set w .menu.pgms
    menubutton $w -text "Run" -menu .menu.pgms.m
    menu $w.m

    set w .menu.help
    menubutton $w -text "Help" -menu .menu.help.m
    menu $w.m
    $w.m add sep
    $w.m add command -label "About" -command about

    set w .menu
    pack $w.ppp $w.options $w.pgms -side left
    pack $w.help -side right
    tk_menuBar $w $w.ppp $w.options $w.pgms $w.help

    set w .top
    frame $w
    button $w.bitmap -bitmap $pppdn_bitmap -cursor hand2 \
                     -command ppp-toggle

    set w .top.addr
    frame $w
    label $w.caption -text "IP Address:"
    label $w.ipaddr  -relief ridge -border 2 -width 20
    pack  $w.caption $w.ipaddr -side top -pady 0 -fill y -expand no

    pack .top.bitmap -side left  -pady 6 -padx 8 -expand no
    pack .top.addr   -side right -fill x -pady 2 -padx 8 -expand no
    pack .top -side top -fill x -expand no

    frame .status
    entry .status.msg -text "PPP Status" -relief flat
    pack .status.msg -side top -padx 4 -fill x -expand no
    pack .status -side top -fill x -expand yes

    wm minsize . 10 10
    wm title . tkPPP  
    wm iconname . tkPPP
    wm iconbitmap . $pppdn_bitmap
    bind . <Q> quit
    bind . <Control-c> quit
    bind . <o> ppp-on
    bind . <f> ppp-off
    bind . <i> {wm iconify .}
    bind . <s> setstatus
    tk_bindForTraversal .
}

##############################################################
#                        Main program                        #
##############################################################

createwindow
catch {source $env(HOME)/.tkppprc}
status $statusmsg
update
ppp-stat 1
ping
