#!/usr/X11/bin/wish -f
#
# tkppp - a graphical interface to control a PPP connection.
#
# Written by Eric Jeschke (jeschke@cs.indiana.edu)
# Comments, bugfixes, patches welcome...
#
# RCS Version Info:
# $Id: tkppp,v 1.1 1994/08/31 01:41:11 jeschke Exp $
# I have no idea on how to do RCS, so i'll just comment in the lines
# that have the changes in 'em...here they are:
# 1) i wrote a little script to get the IP address, incase they're connecting
# to a dynamic server.  i'll just keep that in the main window.
# 2) i also abolished the need for the scripts.   

#the following is TOTALLY optional. i use it 'cause it give it a
#Motif look.  that bisque is really phooey to me. :) comment it
#out if you don't have Tix.   

#BEGIN TIX STUFF
set TIX_LIBRARY /usr/local/lib/tix
lappend auto_path $TIX_LIBRARY

tixInit -libdir $TIX_LIBRARY -fontset 14Point -scheme Gray -binding Motif

#END TIX STUFF

#
# Set the following to appropriate values for your system.
#------------------------------------------------------------
# $pppon and $pppoff are the names of scripts to start and
# stop PPP, respectively.  They should return 0 on success.

set pppdir ~/bin 
set pppon   $pppdir/ppp-on
set pppoff  $pppdir/ppp-off

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

# How often to check the status of the PPP link (in seconds).
# I wouldn't set this lower than, say, 5.
#
set check_interval  7

#-------------- Don't edit below this line ------------------
#-- unless you're adding to the code. :)  jmm 9/2/94 ---

set linkstatus  "DOWN"

proc quit {} {
    destroy .
}

proc ppp-stat {w flag} {
#must put this catch, 'cause it's constantly checking for stuff that may
#return an error code, but not a critical one..

 catch {
    global linkstatus check_interval
    global pppup_bitmap pppdn_bitmap

		set result [exec ifconfig | grep ppp | sed s/0.*//] 
    set bitmap [lindex [$w.bitmap config -bitmap] 4]
    if { $result== "ppp"  } {
				set linkstatus UP
				set ipaddress [exec ifconfig | grep inet.*P-t-P | \
												sed s/inet.*r// | sed s/P-t-P.*//] 
        $w.bitmap config -bitmap $pppup_bitmap
        $w.caption config -text "PPP is UP"
				$w.caption2 config -text "address: $ipaddress"
    } else {
        $w.bitmap config -bitmap $pppdn_bitmap
        $w.caption config -text "PPP is DOWN"
				$w.caption2 config -text "localhost"
    }

    if {$flag != 0} {
        after [expr {$check_interval * 1000}] ppp-stat $w $flag
    }
 }
}

proc ppp-on {w} {
    global linkstatus pppon
    if {$linkstatus != "UP"} {
        $w.caption config -text "Starting PPP..."
        exec $pppon
        ppp-stat $w 0
    }
}

proc ppp-off {w} {
    global linkstatus pppdn_bitmap pppoff

    if {$linkstatus == "UP"} {
        $w.caption config -text "Terminating PPP..."
				$w.caption2 config -text ""
				set linkstatus "DOWN"	
        $w.bitmap config -bitmap $pppdn_bitmap
        exec $pppoff
				$w.caption2 config -text "at localhost."
        ppp-stat $w 0
    }
}

proc createwindow {} {
    global pppdn_bitmap

    frame .top
    frame .buttons  -relief sunken -border 1
    pack .top .buttons -side top -fill both -expand yes

    label .top.bitmap -bitmap $pppdn_bitmap -borderwidth 2 -relief sunken
    label .top.caption -text "PPP Status" 
    label .top.caption2 -text "not connected"
    pack  .top.bitmap .top.caption .top.caption2  -side top -pady 5

    button .buttons.pppon  -command "ppp-on  .top"  -text "PPP-On"
    button .buttons.pppoff -command "ppp-off .top"  -text "PPP-Off"
    button .buttons.quit   -command "quit"     -text "Quit"

    pack .buttons.pppon .buttons.pppoff .buttons.quit \
	    -side left -expand yes -pady 5 -padx 3

    pack .top .buttons -side top -fill both
    wm minsize . 10 10
		#so window managers will like
		wm title . "tkPPP"  
		wm iconname . tikeyPiPiPi
    bind . <q> {quit}
		bind . <o> {ppp-on  .top}
		bind . <f> {ppp-off .top }
    bind . <Control-c> {quit}
    ppp-stat .top 1
}

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

createwindow
