#!/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.2 1994/09/03 21:25:31 jeschke Exp $
#
# CHANGES CREDIT
#   - Show IP address used (jmmadiso@iupui.edu)
#   - Cool xpm icon (jmmadiso@iupui.edu)
#

#
# 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  /usr/lib/ppp
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 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  10

# 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 (jmmadiso@iupui.edu).   

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

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

set linkstatus  "1"

proc quit {} {
    destroy .
}

proc ppp-stat {w flag} {
    global linkstatus check_interval
    global pppup_bitmap pppdn_bitmap

    set linkstatus [catch {exec ps augx | grep pppd | grep -vq grep}]
    if {$linkstatus == "0"} {
        $w.bitmap config -bitmap $pppup_bitmap
        $w.caption config -text "PPP is UP"

        # Get IP address used.
        catch {exec ifconfig | grep ppp | sed s/0.*//} result 
        if { $result== "ppp"  } {
            catch {exec ifconfig | grep inet.*P-t-P | \
		      sed s/inet.*r// | sed s/P-t-P.*//} ipaddress
            $w.caption2 config -text "IP Address: $ipaddress"
        } else {
            $w.caption2 config -text "localhost"
        }
    } else {
        $w.bitmap config -bitmap $pppdn_bitmap
        $w.caption config -text "PPP is DOWN"
        $w.caption2 config -text ""
    }
    update

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

proc ppp-on {w} {
    global linkstatus pppon
    if {$linkstatus != "0"} {
        . config -cursor watch
        $w.caption config -text "Starting PPP..."
        update
        set status [catch {exec $pppon} errtxt]
        . config -cursor arrow
        if {$status} {
            error $errtxt
        }
        ppp-stat $w 0
    }
}

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

    if {$linkstatus == "0"} {
        . config -cursor watch
        $w.caption config -text "Terminating PPP..."
        update
        set status [catch {exec $pppoff} errtxt]
        . config -cursor arrow
        if {$status} {
            error $errtxt
        }
        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 3

    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 . tkPPP
    bind . <q> {quit}
		bind . <o> {ppp-on  .top}
		bind . <f> {ppp-off .top }
    bind . <Control-c> {quit}
    ppp-stat .top 1
}

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

createwindow
