#!/usr/local/bin/wish -f
#########################################################################
#               tkAutoPPP (c) 1997 by Karsten Mueller                   #
#                 Questions go to: (Subject  tkPPP)                     #
#                     email: km@TheArtOfWeb.com                         #
#                WWW: http://TheArtOfWeb.com/~karsten                   #
#                   chat: telnet://www.vh1.de:1170                      #
#########################################################################

########################################################################
#                C o n f i g u r a b l e  S e c t i o n                #
########################################################################
#
#
#
# PPP_Path points to the root directory of tkPPP's installation
# (Usually /usr/local/bin/tkAutoPPP) 
# (I prefer to start this up via a button in fvwm(95) window-manager)

set PPP_Path /usr/local/bin/tkAutoPPP

# where to find your login script
set ppp_go $PPP_Path/connect

# where to find your PPP-termination-script
set ppp_x $PPP_Path/pppx

# we want to know what adress has been assigned to us by our provider
set getIP $PPP_Path/get_ip

# a simple script to find out our hostname
set getHost $PPP_Path/get_host

# we have to know the IP of our provider
# (The IP adress of our peer)
set provider 134.100.41.253

#
#
#
########################################################################
#        E n d  o f  C o n f i g u r a b l e  S e c t i o n            #
########################################################################



#################################
#                               #
# G L O B A L S                 #
#                               #
#################################

set ip_msg {Not yet available.}
set host_msg $ip_msg
set message {Not Connected.}
set msg {Not connected.}
set statusmsg {Not connected.}
set try 1
set connected 0
set connect_status 0
set dialing 0
set stattext {}
set seconds 20
set minutes 0
set hours   0
set stopped 1
set enddlg 0

source "~/.tkAutoPPP/phones"

set number [lindex $phones 0]

proc redraw {} {
   global connected dialing
   if { $connected == 0} {
      if {$dialing == 0} {
         .tkPPP_top.disconnect config -state disabled
         .tkPPP_top.connect config -state normal
         .tkPPP_top.lab2 config -text Status:
      } else {
         .tkPPP_top.disconnect config -state disabled
         .tkPPP_top.connect config -state disabled
         .tkPPP_top.lab2 config -text Status:
      }
   } else {
      .tkPPP_top.disconnect config -state normal
      .tkPPP_top.connect config -state disabled
      .tkPPP_top.lab2 config -text "Time elapsed:"
   }
   update
}

proc main {argc argv} {
   global phones number

   timer
   redraw
}

proc timer {} {
   global hours minutes seconds stopped
   if $stopped return
   after 1000 timer
   set seconds [expr $seconds+1]
   redraw
   if {$seconds >= 60 } {
      set seconds 0
      set minutes [expr $minutes + 1]
   }
   if {$minutes >= 60 } {
      set minutes 0
      set hours [expr $hours + 1]
   }
   if {$hours >= 24 } {
      set hours 0
   }

   setstatus [format "%02d:%02d:%02d" $hours $minutes $seconds]
}

proc ppp_connect {} {
global try connected connect_status stattext ppp_go ppp_x \
       getIP getHost provider stopped dialing PPP_Path \
       number enddlg
   set try 1
   set connect_status 0
   set ipaddr 0.0.0.0
   set hostname nohost.nodomain

   setstatus "Connecting..."

   while {$connect_status != 1 && $enddlg != 1} {
      set stattext [format "Attempt #%d in progress..." $try]
      setstatus $stattext
      update
      set dialing 1
      .tkPPP_top.quit config -text "Stop dialing"
      redraw
      catch {exec $ppp_go $number $provider } connect_status
      if {$connect_status == 1} {
         setstatus "Connected."
         catch {exec $getIP} ipaddr
         setipstatus $ipaddr
         catch {exec $getHost $ipaddr} hostname
         sethoststatus $hostname
         set connected 1
         set dialing 0
         set stopped 0
         timer
         redraw
         break
      } else {
         set stattext {Busy}
         setstatus $stattext
         set dialing 0
         redraw
         after 500
      }
      incr try
   } 
   if {$enddlg == 1} {
      set enddlg 0
      set dialing 0
      set connect_status 0
      setstatus Aborted.
      redraw
      return
   }
}

proc ppp_disconnect {} {
   global ppp_x connected stopped seconds minutes hours

# Query if the connection really should be shut down
   after idle {
      .dialog2.msg configure -wraplength 4i
   }
   after 100 {
      grab .dialog2
   }
   set i [tk_dialog .dialog2 "Confirm Disconnect" \
          {Do you really want to disconnect ?} \
          question 1 Disconnect {No, don't disconnect}]
        
   switch $i {
      0 {
         if { $connected == 1} {
            setstatus "Disconnecting..."
            catch {exec $ppp_x} connect_status
            set connected 0
            redraw
         }
         set stopped 1
#         setstatus [format "%02d:%02d:%02d" $hours $minutes $seconds]
#         after 3000
         .tkPPP_top.lab2 config -text "Status:"
         setstatus "Not connected."
         setipstatus "Online time:"
         sethoststatus [format "%02d:%02d:%02d" $hours $minutes $seconds]
         set connected 0
      }
   }
}

proc ppp_quit {} {
   global connected ppp_x seconds minutes hours dialing enddlg

   if { $dialing == 1 } {
      .tkPPP_top.quit config -text "Quit"
      set enddlg 1
      set dialing 0
   } else {
      after idle {
         .dialog2.msg configure -wraplength 4i
      }
      after 100 {
         grab .dialog2
      }
      set i [tk_dialog .dialog2 "Confirm Quit" \
             {Do you really want to Quit ?} \
             question 1 Quit {Don't Quit}]
           
      switch $i {
         0 {
            if { $connected == 1} {
               setstatus "Disconnecting..."
               catch {exec $ppp_x} connect_status
               set connected 0
               redraw
            }
            set stopped 1
            setstatus [format "%02d:%02d:%02d" $hours $minutes $seconds]
#            after 3000
            .tkPPP_top.lab2 config -text "Status:"
            destroy .
         }
      }
   }
}

proc setstatus {message} {
   .tkPPP_top.status.msg config -state normal
   .tkPPP_top.status.msg delete 0 1000
   .tkPPP_top.status.msg insert end $message
   .tkPPP_top.status.msg config -state disabled
   update
}

proc setipstatus {message} {
   .tkPPP_top.ip.ipaddress config -state normal
   .tkPPP_top.ip.ipaddress delete 0 1000
   .tkPPP_top.ip.ipaddress insert end $message
   .tkPPP_top.ip.ipaddress config -state disabled
   update
}

proc sethoststatus {message} {
   .tkPPP_top.ip.host config -state normal
   .tkPPP_top.ip.host delete 0 1000
   .tkPPP_top.ip.host insert end $message
   .tkPPP_top.ip.host config -state disabled
   update
}

proc Window {args} {
global vTcl
    set cmd [lindex $args 0]
    set name [lindex $args 1]
    set rest [lrange $args 2 end]
    if {$name == "" || $cmd == ""} {return}
    switch $cmd {
        create {
            if {[info procs vTclWindow(pre)$name] != ""} {
                vTclWindow(pre)$name $rest
            }
            if {[info procs vTclWindow$name] != ""} {
                vTclWindow$name $rest
            }
            if {[info procs vTclWindow(post)$name] != ""} {
                vTclWindow(post)$name $rest
            }
        }
        show {
            if {[info procs vTclWindow(pre)$name] != ""} {
                vTclWindow(pre)$name $rest
            }
            if {[info procs vTclWindow$name] != ""} {
                vTclWindow$name
            }
            if {[info procs vTclWindow(post)$name] != ""} {
                vTclWindow(post)$name $rest
            }
        }
        hide {
            if {[winfo exists $name] == 1} {
                wm withdraw $name; return
            }
        }
        iconify {
            if {[winfo exists $name] == 1} {
                wm iconify $name; return
            }
        }
        destroy {
            if {[winfo exists $name] == 1} {
                destroy $name; return
            }
        }
    }
}

#################################
# VTCL GENERATED GUI PROCEDURES
#

proc vTclWindow. {args} {
   set base .
   wm focusmodel . passive
   wm geometry . 0x0
   wm maxsize . 0 0
   wm minsize . 0 0
   wm overrideredirect . 1
   wm resizable . 0 0
   wm withdraw .
   wm title . "Auto-PPP"

}

proc vTclWindow.tkPPP_top {args} {
   global phones names

   set base .tkPPP_top
   if {[winfo exists .tkPPP_top]} {
       wm deiconify .tkPPP_top; return
   }
   toplevel .tkPPP_top -background grey80 
   wm geometry .tkPPP_top 194x225-80+0
   wm maxsize .tkPPP_top 194 225
   wm minsize .tkPPP_top 1 1
   wm overrideredirect .tkPPP_top 0
   wm resizable .tkPPP_top 0 0
   wm deiconify .tkPPP_top
   wm title .tkPPP_top "tkAutoPPP"

   menubutton .tkPPP_top.mb -text "[lindex $names 0]" -bg grey90 \
            -menu .tkPPP_top.mb.menu \
            -font -Adobe-Helvetica-Medium-R-Normal-*-*-120-*-*-*-*-*-*
   menu .tkPPP_top.mb.menu -activebackground grey94 -bg grey80


   for {set i 0} {$i < [llength $phones]} {incr i} {
      set comspec ".tkPPP_top.mb config -text {[lindex $names $i]} ; set number [lindex $phones $i]"
      .tkPPP_top.mb.menu add command -label "[lindex $names $i]" \
         -font -Adobe-Helvetica-Medium-R-Normal-*-*-120-*-*-*-*-*-* \
         -command $comspec
   }
   place .tkPPP_top.mb -x 5 -relx 0 -y 5 -rely 0 -width 185 -height 25 \
         -relheight {} -anchor nw -bordermode ignore

   button .tkPPP_top.connect \
       -background grey80 -command ppp_connect \
       -font -Adobe-Helvetica-Medium-R-Normal-*-*-120-*-*-*-*-*-* -padx 9 \
       -pady 3 -state normal -text {Connect !} 
   place .tkPPP_top.connect \
       -x 5 -relx 0 -y 35 -rely 0 -width 90 -relwidth {} -height 28 \
       -relheight {} -anchor nw -bordermode ignore 
   button .tkPPP_top.disconnect \
       -background grey80 -command ppp_disconnect \
       -font -Adobe-Helvetica-Medium-R-Normal-*-*-120-*-*-*-*-*-* -padx 9 \
       -pady 3 -state disabled -text Disconnect 
   place .tkPPP_top.disconnect \
       -x 100 -relx 0 -y 35 -rely 0 -width 90 -relwidth {} -height 28 \
       -relheight {} -anchor nw -bordermode ignore 

   button .tkPPP_top.quit \
       -background grey80 -command ppp_quit \
       -font -Adobe-Helvetica-Medium-R-Normal-*-*-120-*-*-*-*-*-* -padx 9 \
       -pady 3 -text Quit
   place .tkPPP_top.quit \
       -x 10 -relx 0 -y 195 -rely 0 -width 175 -relwidth {} -height 28 \
       -relheight {} -anchor nw -bordermode ignore
   label .tkPPP_top.lab2 \
       -background grey80 -borderwidth 0 \
       -font -Adobe-Helvetica-bold-R-Normal-*-*-160-*-*-*-*-*-* \
       -justify left -relief groove -text Status: 
   place .tkPPP_top.lab2 \
       -x 10 -relx 0 -y 65 -rely 0 -width 165 -relwidth {} -height 20 \
       -relheight {} -bordermode ignore -anchor nw
   frame .tkPPP_top.status \
       -background grey80 -borderwidth 1 -height 30 -relief sunken \
       -width 30 
   place .tkPPP_top.status \
       -x 15 -relx 0 -y 85 -rely 0 -width 165 -relwidth {} -height 30 \
       -relheight {} -anchor nw -bordermode ignore 
   entry .tkPPP_top.status.msg \
       -background grey80 \
       -font -Adobe-Helvetica-Medium-R-Normal-*-*-120-*-*-*-*-*-* \
       -highlightthickness 0 -relief flat -selectborderwidth 0 \
       -state disabled -justify center -textvariable msg 
   place .tkPPP_top.status.msg \
       -x 5 -relx 0 -y 35 -rely 0 -width 156 -relwidth {} -height 20 \
       -relheight {} -anchor nw -bordermode ignore 
   pack .tkPPP_top.status.msg \
      -in .tkPPP_top.status -anchor center -expand 1 -fill x -ipadx 0 \
      -ipady 0 -padx 4 -pady 0 -side top 
   label .tkPPP_top.ip_hostlabel \
      -background grey80 -borderwidth 0 \
      -font -Adobe-Helvetica-bold-R-Normal-*-*-160-*-*-*-*-*-* \
      -justify left -relief groove -text {IP / Hostname:} 
   place .tkPPP_top.ip_hostlabel \
       -x 15 -relx 0 -y 120 -rely 0 -width 165 -relwidth {} -height 20 \
       -relheight {} -anchor nw -bordermode ignore 

   frame .tkPPP_top.ip \
       -background grey80 -borderwidth 1 -height 30 -relief sunken \
       -width 30 
   place .tkPPP_top.ip \
       -x 15 -relx 0 -y 140 -rely 0 -width 165 -relwidth {} -height 50 \
       -relheight {} -anchor nw -bordermode ignore 
   entry .tkPPP_top.ip.ipaddress \
       -background grey80 \
       -font -Adobe-Helvetica-Medium-R-Normal-*-*-100-*-*-*-*-*-* \
       -highlightthickness 0 -relief flat -selectborderwidth 0 \
       -state disabled -justify center -textvariable ip_msg 
   place .tkPPP_top.ip.ipaddress \
       -x 5 -relx 0 -y 5 -rely 0 -width 156 -relwidth {} -height 20 \
       -relheight {} -anchor nw -bordermode ignore 
   entry .tkPPP_top.ip.host \
       -background grey80 \
       -font -Adobe-Helvetica-Medium-R-Normal-*-*-100-*-*-*-*-*-* \
       -highlightthickness 0 -relief flat -selectborderwidth 0 \
       -state disabled -justify center -textvariable host_msg 
   place .tkPPP_top.ip.host \
       -x 5 -relx 0 -y 25 -rely 0 -width 156 -relwidth {} -height 20 \
       -relheight {} -anchor nw -bordermode ignore 

   bind q ppp_quit
}


Window
Window show .
Window show .tkPPP_top
bind all q {
   ppp_quit
}
bind all c {
   ppp_connect
}
bind all d {
   ppp_disconnect
}

main $argc $argv
