#
# Startppp
# 
# Copyright (C) 1995  Matthias Ott 
#  (msott@cip.informatik.uni-erlangen.de)
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
# 
# 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
# Library General Public License for more details.
# 
# You should have received a copy of the GNU Library General Public
# License along with this program; if not, write to the Free
# Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

set progname [file tail $argv0]
set commandoption [lindex $argv 0]
set version "v0.80"
set instdir "/usr/local/lib/startppp"
set moneyfile "$env(HOME)/.${progname}_money"
set configfile "$env(HOME)/.${progname}_config"
set expectfile "$env(HOME)/.${progname}_expect"
set onlinetime "00:00:00"; 		# how long are you online
set lastonlinetime "00:00:00";		# time to next unit
set modemdevice /dev/modem;		# modem to use
set dialtry 0; 				# how often did you dial already
set Iconify 0; 				# iconify program after connection
set showterminal 1;			# show terminal while dialing
set startdelay 4; 			# how long does it take to connect
set showmoneyinicon 1; 			# display time in iconname
set flagwarning 0; 			# time-warning yes/no
set warningtime 60; 			# warningperiod (min)
set refreshtime 1; 			# refresh-delay of onlinetime (sec)
set button 1; 				# how many seconds per unit, default
set zonedaytime 1; 			# daytime or nighttime
set realunits 60;			# display realunits
set money "0.00"; 			# how much money does it cost 
set linestatus "LINE down"; 		# status of connection
set ppp_up "/etc/ppp/ppp-up"; 		# dialing-program
set ppp_down "/etc/ppp/ppp-down"; 	# canceling-program
set nightstart 18; 			# start of cheapzone
set nightstop 8; 			# end of cheapzone
set modeminit ATZ;			# Initstring for modem
set dialtype T;				# T: tone, P: pulse
set modemnumber 858111;			# Number to dial
set repeatdelay 1;			# delay between dialing
set modemspeed "38400";			# Speed of line
set modemparity "CS8";			# Parity
set modemstopbits "|";			# How many stopbits
set modemtimeout 60;			# timeout for expect
set zoneday(0) 1; 			# Sunday
set zoneday(1) 0; 			# Monday
set zoneday(2) 0; 			# Tuesday
set zoneday(3) 0; 			# Wednesday
set zoneday(4) 0; 			# Thursday
set zoneday(5) 0; 			# Friday
set zoneday(6) 1; 			# Saturday

set zone(1) 360; 			# Length (sec)
set zone(2) 60; 			# Length (sec)
set zone(3) 21; 			# Length (sec)
set zonemoney 0.23; 			# How much is one unit
set currency DM; 			# Currency
set button 1; 				# default of zones
set pid 0;				# child pid

# config-files

# common config

if {[catch {open $configfile r} Datei] == 1} {
  tk_dialog .config "Config" "No file $configfile" error 0 ok
} else {
  gets $Datei instdir
  gets $Datei zone(1)
  gets $Datei zone(2)
  gets $Datei zone(3)
  gets $Datei zonemoney
  gets $Datei currency
  gets $Datei showmoneyinicon
  gets $Datei Iconify
  gets $Datei ppp_up
  gets $Datei ppp_down
  gets $Datei alterrefresh
  gets $Datei button
  gets $Datei alternightstart
  gets $Datei alternightstop
  gets $Datei flagwarning
  gets $Datei warningtime
  gets $Datei zoneday(0)
  gets $Datei zoneday(1)
  gets $Datei zoneday(2)
  gets $Datei zoneday(3)
  gets $Datei zoneday(4)
  gets $Datei zoneday(5)
  gets $Datei zoneday(6)
  gets $Datei startdelay
  gets $Datei modeminit
  gets $Datei modemnumber
  gets $Datei dialtype
  gets $Datei modemdevice
  gets $Datei repeatdelay
  gets $Datei modemspeed
  gets $Datei modemparity
  gets $Datei modemstopbits
  gets $Datei modemtimeout
  gets $Datei showterminal
  close $Datei
}

if {$button!="1" && $button!="2" && $button!="3"} {
  set button 1
} 

set modemdevicetail [file tail $modemdevice]
set zonelength $zone($button)
if {[catch {expr int($alterrefresh)}] == 0} {
  set refreshtime $alterrefresh
} else {
  set alterrefresh $refreshtime
}

if {[catch {expr int($alternightstart)}] == 0} {
  set nightstart $alternightstart
} else {
  set alternightstart $nightstart
}
if {[catch {expr int($alternightstop)}] == 0} {
  set nightstop $alternightstop
} else {
  set alternightstop $nightstop
}

# Ressource
set Ressource $instdir/Startppp

if [catch {option readfile $Ressource startup} err] {
  tk_dialog .config "Config" "$err: change it, but use it!!!" error 0 ok
}


# Pictures

set mypicup [image create photo -file "$instdir/eye11.ppm"]
set mypicup2 [image create photo -file "$instdir/eye12.ppm"]
set mypicdown [image create photo -file "$instdir/eye24.ppm"]
set mypicdown2 [image create photo -file "$instdir/eye25.ppm"]
set mypicexit [image create photo -file "$instdir/cancel.ppm"]
set mypicpppup [image create photo -file "$instdir/pppup.ppm"]
set mypicpppdown [image create photo -file "$instdir/pppdown.ppm"]


# hints for the window-manager

tk appname $progname
wm title . "$progname $version"
wm iconbitmap . "@$instdir/pppdownicon"
wm iconname . $progname
wm protocol . WM_DELETE_WINDOW bell

#
# Something like this does not work because of a bug
# in the Iconbox of fvwm
# maybe I can use this later on
#
# toplevel .icon -cursor man
# label .icon.image -image $mypicdown
# pack .icon.image
# wm iconwindow . .icon
#

# menubar

frame .mbar -relief raised -bd 2

# help-button

menubutton .mbar.help -cursor sb_down_arrow -text Help -underline 0 -menu .mbar.help.menu
menu .mbar.help.menu
.mbar.help.menu add command -label "About $progname $version     " -underline 0 -accelerator Ctrl+a -command {about $version}
.mbar.help.menu add command -label "Help" -underline 0 -accelerator Ctrl+h -command {Help}

# options-button

menubutton .mbar.options -cursor sb_down_arrow -text Options -underline 0 -menu .mbar.options.menu
menu .mbar.options.menu
.mbar.options.menu add checkbutton -label "Iconify" -variable Iconify
.mbar.options.menu add checkbutton -label "Onlinetime in Icon" -variable showmoneyinicon
.mbar.options.menu add checkbutton -label "Show terminal" -variable showterminal

menubutton .mbar.config -cursor sb_down_arrow -text Configs -underline 0 -menu .mbar.config.menu
menu .mbar.config.menu
.mbar.config.menu add command -label "Show money...     " -underline 0 -accelerator Ctrl+s -command {wholemoney $moneyfile $currency} 
.mbar.config.menu add separator
.mbar.config.menu add command -label "Alter options...     " -underline 6 -accelerator Ctrl+o -command alterunits
.mbar.config.menu add command -label "Alter modem...     " -underline 6 -accelerator Ctrl+m -command altermodem
.mbar.config.menu add separator
.mbar.config.menu add command -label "Change state...     " -underline 0 -accelerator Ctrl+c -command changestate


focus .mbar

# eventbinding for the menubar

bind . <Control-a> {about $version}
bind . <Control-h> {Help}
bind . <Control-s> {wholemoney $moneyfile $currency}
bind . <Control-o> {alterunits}
bind . <Control-m> {altermodem}
bind . <Control-x> {Exit $moneyfile}
bind . <Control-c> {changestate}


# buttons for the functions

frame .button
button .button.up -cursor target -image $mypicpppup -command {up}
button .button.down -cursor target -image $mypicpppdown -command {down}
button .button.exit -cursor target -image $mypicexit -command {Exit $moneyfile}

# display information

frame .info -cursor cross -relief ridge -borderwidth 4 

# connection-data, from <ip>

frame .info.status -relief sunken -borderwidth 4   
frame .info.status.unitlength
label .info.status.unitlength.label -text "Length of one unit (sec):"
label .info.status.unitlength.output -textvariable realunits -width 8 -anchor w
frame .info.status.ifname
label .info.status.ifname.label -text "Interface-name:"
label .info.status.ifname.output -textvariable Ifname -width 10 -anchor w
frame .info.status.ttyname
label .info.status.ttyname.label -text "Device:"
label .info.status.ttyname.output -textvariable Ttydev
frame .info.status.pppspeed
label .info.status.pppspeed.label -text "Speed:"
label .info.status.pppspeed.output -textvariable Pppspeed
frame .info.status.localip
label .info.status.localip.label -text "Local IP:"
label .info.status.localip.output -textvariable Localip
frame .info.status.remoteip
label .info.status.remoteip.label -text "Remote IP:"
label .info.status.remoteip.output -textvariable Remoteip
frame .info.status.message -relief ridge -bd 2
label .info.status.message.label -text "          "
label .info.status.message.output -textvariable message

# display status

frame .info.action -relief groove -borderwidth 4  

frame .info.action.ppp -relief ridge -borderwidth 3
label .info.action.ppp.info -image $mypicdown

frame .info.action.time
label .info.action.time.label -text "Time:	"
label .info.action.time.time -textvariable Time
frame .info.action.onlinetime
label .info.action.onlinetime.label -text "Online:	"
label .info.action.onlinetime.time -textvariable onlinetime -width 10 -anchor w
frame .info.action.lastonlinetime
label .info.action.lastonlinetime.label -text "Next:	"
label .info.action.lastonlinetime.time -textvariable lastonlinetime -width 10 -anchor w
frame .info.action.money
label .info.action.money.label -text "Money:	"
label .info.action.money.money -textvariable money
frame .info.action.dialtry
label .info.action.dialtry.label -text "Dialing:	"
label .info.action.dialtry.dialtry -textvariable dialtry

bind .info.action.ppp.info <Enter> {
  goodieenter
}
bind .info.action.ppp.info <Leave> {
  goodieleave
}

. configure -bg [option get .mbar.help background Background]

# the whole packing

pack .mbar -side top -fill x
pack .mbar.help -side right
pack .mbar.options -side left
pack .mbar.config -side left

pack .button -side bottom -fill x
pack .button.up -side left -padx 20 -pady 10
pack .button.down -side left -padx 10 -pady 10

pack .button.exit -side right -padx 10 -pady 10

pack .info -side top -fill x -fill y -pady 5 -padx 5
pack .info.status -side left -fill y -anchor w
pack .info.action -side right -anchor e
pack .info.status.unitlength -side top -anchor w -pady 0
pack .info.status.unitlength.label -side left
pack .info.status.unitlength.output -side left
pack .info.status.ifname -side top -anchor w -pady 0
pack .info.status.ifname.label -side left
pack .info.status.ifname.output -side left
pack .info.status.ttyname -side top -anchor w -pady 0
pack .info.status.ttyname.label -side left
pack .info.status.ttyname.output -side left
pack .info.status.pppspeed -side top -anchor w -pady 0
pack .info.status.pppspeed.label -side left
pack .info.status.pppspeed.output -side left
pack .info.status.localip -side top -anchor w -pady 0
pack .info.status.localip.label -side left
pack .info.status.localip.output -side left
pack .info.status.remoteip -side top -anchor w -pady 0
pack .info.status.remoteip.label -side left
pack .info.status.remoteip.output -side left
pack .info.status.message -side top -fill x -anchor w -pady 0
pack .info.status.message.label -side left
pack .info.status.message.output -side left -fill x

pack .info.action.ppp -side top -anchor w
pack .info.action.ppp.info 
pack .info.action.time -side top -anchor w
pack .info.action.time.label -side left 
pack .info.action.time.time -side left 
pack .info.action.onlinetime -side top -anchor w
pack .info.action.onlinetime.label -side left 
pack .info.action.onlinetime.time -side left
pack .info.action.lastonlinetime -side top -anchor w
pack .info.action.lastonlinetime.label -side left 
pack .info.action.lastonlinetime.time -side left
pack .info.action.money -side top -anchor w
pack .info.action.money.label -side left 
pack .info.action.money.money -side left 
pack .info.action.dialtry -side top -anchor w
pack .info.action.dialtry.label -side left 
pack .info.action.dialtry.dialtry -side left 


# the functions

# dial ppp 

proc up {} {
  global linestatus message showterminal pid modemdevicetail modemdevice
  if {$linestatus == "LINE up"} {
    set message "line already active"
    return
  } else {
    set modemdevicetail [file tail $modemdevice]
    if {$showterminal == 1} showterminal
    set pid [dialproc]
    set message $pid
  }
}


proc up2 {} {
  global ppp_up mypicup instdir
  exec $ppp_up &
  .info.action.ppp.info configure -image $mypicup
  wm iconbitmap . "@$instdir/pppupicon"
}


# cancel ppp 

proc down {} {
  global message pid ppp_down dialtry
  global linestatus instdir mypicup mypicdown startwarn 
  global modemdevice Ifname Ttydev Pppspeed Localip Remoteip
  set dialtry 0
  if {$pid != 0} {
    catch {killpid} message
  }
  if [catch {exec $ppp_down &} result] {
    error $result
  }

  set message [modemclose [file tail $modemdevice]]

  set pid 0
  set info_linestatus [catch {exec netstat -nr | grep ppp0} result]
  if {$info_linestatus == 1} {
    set linestatus "LINE down"
    .info.action.ppp.info configure -image $mypicdown
    wm iconbitmap . "@$instdir/pppdownicon"
    set Ifname ""
    set Ttydev ""
    set Pppspeed ""
    set Localip ""
    set Remoteip ""
    startonlinetime
  }
}

 
# is there already a ppp-connection?

proc pppinfo {} {
  global linestatus instdir mypicup mypicdown startwarn 
  global Ifname Ttydev Pppspeed Localip Remoteip
  set info_linestatus [catch {exec netstat -nr | grep ppp0}]
  if {$info_linestatus == 1} {
    set linestatus "LINE down"
    .info.action.ppp.info configure -image $mypicdown
    wm iconbitmap . "@$instdir/pppdownicon"
    set Ifname ""
    set Ttydev ""
    set Pppspeed ""
    set Localip ""
    set Remoteip ""
  } else {
    set linestatus "LINE up"
    .info.action.ppp.info configure -image $mypicup
    wm iconbitmap . "@$instdir/pppupicon"
  }
  startonlinetime
}   

# starting the online-time

proc startonlinetime {} {
  global zonemoney moneyfile money linestatus lastonlinetime onlinetime onlinemoneytime
  global startwarn startdelay
  if {$linestatus == "LINE down"} {
    set onlinetime "00:00:00"
    set lastonlinetime "00:00:00"
    savemoney $moneyfile $money 
    set money "0.00"
    set onlinemoneytime 0
   } else {
    set money $zonemoney
    set onlinemoneytime 0
    set startwarn [expr [gettime] -$startdelay]
    after [expr 60000 - (($startdelay - 1) * 1000)] warningguard
    onlinetime [expr [gettime] - $startdelay]
  }
}   

# Save the configfile

proc savemoney {moneyfile money} {
  if {$money != "0.00"} {
    lappend temp $money
    lappend temp [exec date]
    set Datei [open $moneyfile a+]
    puts $Datei "$temp" 
    close $Datei
  }
}
  
# display of the connection-data, from <send> at <ip>

proc showppp {letter ifname ttydev pppspeed localip remoteip} {
  global Iconify linestatus Ifname Ttydev Pppspeed Localip Remoteip
  global startwarn mypicup mypicdown instdir
  if {$letter == "p"} {
    set Ifname $ifname
    set Ttydev $ttydev
    set Pppspeed $pppspeed
    set Localip $localip
    set Remoteip $remoteip
  } else {
    set linestatus "LINE down"
    .info.action.ppp.info configure -image $mypicdown
    wm iconbitmap . "@$instdir/pppdownicon"
    set Ifname ""
    set Ttydev ""
    set Pppspeed ""
    set Localip ""
    set Remoteip ""
    startonlinetime
  }
  if {$Iconify == 1} {
    after 5000 {wm iconify .}
  }
}

# clock

proc Time {} {
  global realunits Time refreshtime nightstart nightstop zonelength zonedaytime 
  upvar #0 zoneday localzoneday
  set Time [string range [getshowtime] 0 4]
  set realrefresh ""
  getpartzone zonedaytime $nightstart $nightstop $localzoneday(0) $localzoneday(1) $localzoneday(2) $localzoneday(3) $localzoneday(4) $localzoneday(5) $localzoneday(6) 
  set realunits  [expr $zonelength * $zonedaytime]
  after 60000 {Time}
}

# warning after some time

proc warningguard {} {
  global linestatus startwarn warningtime flagwarning
  if {($flagwarning == 1) && ($linestatus == "LINE up")} {
    if {[expr [gettime] - $startwarn] > [expr $warningtime * 60]} {
      incr startwarn [expr $warningtime * 60] 
      bell
      after 1000 bell
      after 2000 bell
      after 3000 bell
      after 4000 bell
    }
  } 
  if {$linestatus == "LINE up"} {
    after 60000 {warningguard}
  }
}

# display of online-time

proc onlinetime {starttime} {
  global zonemoney money zonelength zonedaytime onlinetime onlinemoneytime linestatus
  global lastonlinetime progname refreshtime showmoneyinicon
  if {$linestatus == "LINE down"} {
    wm iconname . $progname
    return
  }
  set temp [expr ([onlineshowtime raw $starttime] - $onlinemoneytime) - ($zonelength * $zonedaytime)]
  
  if {$temp > 0} {
    incr onlinemoneytime [expr $zonelength * $zonedaytime]
    set money [format "%3.2f" [expr $money + $zonemoney]]
    set temp [expr $temp - ($zonelength * $zonedaytime)] 
  }

  if {$refreshtime < 59} {
    set onlinetime [onlineshowtime showsec $starttime]
    set lastonlinetime [showdifftime showsec $temp]
  } else {
    set onlinetime [onlineshowtime showmin $starttime]
    set lastonlinetime [showdifftime showmin $temp]
  }

# still needs too much time !!!

  if {$showmoneyinicon == 1} {
    wm iconname . $onlinetime
   } else {
    if {$showmoneyinicon == 0} {
       wm iconname . $progname
       set showmoneyinicon 2
    }
  }

  after [expr $refreshtime * 1000] onlinetime $starttime
} 

# show the about-text (copyright)

proc about {version} {
  global progname
  toplevel .top3 -cursor man
  wm transient .top3 .
  wm title .top3 "about"
  wm geometry .top3 +300+300
  button .top3.exit -text "OK" -command {destroy .top3}
  message .top3.help1 -relief raised -justify left -width 13c -bd 2 -font \
          -b&h-lucida-bold-r-normal-*-25-*-*-*-*-*-*-* \
          -text "$progname $version"
  message .top3.help2 -relief raised -justify left -width 13c -bd 2 -font \
          -Adobe-Helvetica-Medium-R-Normal--*-180-* \
          -text "                               by\n
 Matthias Ott \n Gebrder-Grimm-Weg 9\n 91522 Ansbach \n Germany \n
 E-Mail: msott@cip.informatik.uni-erlangen.de \n\n(c) 1995"
  pack .top3.help1 .top3.help2 -side top
  pack .top3.exit -ipadx 1c -pady 1m -side bottom
}

# helptext

proc Help {} {
  global instdir 
  set text ""
  toplevel .top4 -cursor man
  wm transient .top4 .
  wm title .top4 "help"
  wm geometry .top4 +400+200
  button .top4.exit -text "OK" -command {destroy .top4}
  message .top4.help -relief raised -justify left -bd 2 -font \
          -*-courier-bold-r-normal-*-12-*-*-*-*-*-*-* 
  if {[catch {open $instdir/startppp-help r} Datei] == 1} {
    tk_dialog .help "Help" "No file $instdir/startppp-help" error 0 ok
    return
  } else {
    while {[gets $Datei line] >=0} {
      set text "$text\n$line"
    }
    close $Datei
  } 
  .top4.help configure -text $text
  pack .top4.help -side top
  pack .top4.exit -ipadx 1c -pady 1m -side bottom

}

# show the money already spent

proc wholemoney {moneyfile currency} {
  global zonemoney
  set wholemoney 0
  if {[catch {open $moneyfile r} Datei] == 1} {
    tk_dialog .money "Money" "No file $moneyfile" error 0 ok
    return
  } else {
    while {[gets $Datei number] >=0} {
      set wholemoney [expr $wholemoney + [lindex $number 0]]
    }
    close $Datei
    set result [expr $wholemoney / $zonemoney]
    tk_dialog .money "Money" "Money spent: [format "%5.2f" $wholemoney] $currency\n\n          Units: [expr int($result)]" info 0 ok
  }
}

# Exit startppp

proc Exit {moneyfile} {
  global pid message mypicup mypicdown mypicpppup mypicpppdown mypicexit
  global money 
  if {[tk_dialog .exit "Exit?" "Really quit?" questhead 1 yes no] == 0} {
    if {$pid != 0} {
      catch {killpid} message
      set pid 0
    }
    savemoney $moneyfile $money     
    image delete $mypicup
    image delete $mypicdown 
    image delete $mypicpppup
    image delete $mypicpppdown 
    image delete $mypicexit 
    exit
  }
}

proc altermodem {} {

  toplevel .am 
  wm transient .am .
  wm title .am "config modem"
  wm geometry .am +300+100
  grab .am
  
  frame .am.text 
  label .am.text.label -text "Alter the modemconfigs:"

  frame .am.buttons
  frame .am.buttons.z1  
  entry .am.buttons.z1.value -width 20 -textvariable modeminit
  label .am.buttons.z1.entry -cursor question_arrow -text " Initstring  "

  frame .am.buttons.z2  
  entry .am.buttons.z2.value -width 20 -textvariable modemnumber
  label .am.buttons.z2.entry -cursor question_arrow -text " Number"

  frame .am.buttons.z3  
  radiobutton .am.buttons.z3.button -cursor question_arrow -text "Tone" -variable dialtype -value T

  frame .am.buttons.z4  
  radiobutton .am.buttons.z4.button -cursor question_arrow -text "Pulse" -variable dialtype -value P

  frame .am.buttons.z5  
  entry .am.buttons.z5.value -width 20 -textvariable modemdevice
  label .am.buttons.z5.entry -cursor question_arrow -text " Modem"

  frame .am.buttons.z6  
  scale .am.buttons.z6.scale -length 9c -relief ridge -label " Delay of dialrepeat (sec)" -from 0 -to 60 -orient horizontal -variable repeatdelay

  frame .am.buttons.z7
  label .am.buttons.z7.entry -cursor question_arrow -text " Linespeed"
  radiobutton .am.buttons.z7.button1 -cursor question_arrow -text "38400" -variable modemspeed -value 38400
  radiobutton .am.buttons.z7.button2 -cursor question_arrow -text "19200" -variable modemspeed -value 19200
  radiobutton .am.buttons.z7.button3 -cursor question_arrow -text "9600" -variable modemspeed -value 9600
  radiobutton .am.buttons.z7.button4 -cursor question_arrow -text "1200" -variable modemspeed -value 1200

  frame .am.buttons.z8
  label .am.buttons.z8.entry -cursor question_arrow -text " Parity"
  radiobutton .am.buttons.z8.button1 -cursor question_arrow -text "None" -variable modemparity -value CS8
  radiobutton .am.buttons.z8.button2 -cursor question_arrow -text "Even" -variable modemparity -value CS7|PARENB
  radiobutton .am.buttons.z8.button3 -cursor question_arrow -text "Odd" -variable modemparity -value CS7|PARENB|PARODD

  frame .am.buttons.z9
  label .am.buttons.z9.entry -cursor question_arrow -text " Stop bits"
  radiobutton .am.buttons.z9.button1 -cursor question_arrow -text "One" -variable modemstopbits -value |
  radiobutton .am.buttons.z9.button2 -cursor question_arrow -text "Two" -variable modemstopbits -value CSTOPB

  frame .am.buttons.z10  
  scale .am.buttons.z10.scale -length 9c -relief ridge -label " timeout for expect (sec)" -from 0 -to 200 -orient horizontal -variable modemtimeout

  frame .am.options 
  button .am.options.ok -cursor target -text Ok -command {destroy .am}
  button .am.options.save -cursor target -text Save -command {saveconfigfile}


  pack .am.text -side top
  pack .am.text.label -side left
  pack .am.buttons -pady 5
  pack .am.buttons.z5 .am.buttons.z1 .am.buttons.z2 .am.buttons.z3 .am.buttons.z4 .am.buttons.z6 -side top -fill x
  pack .am.buttons.z7 .am.buttons.z8 .am.buttons.z9 .am.buttons.z10 -side top -fill x
  pack .am.buttons.z1.entry -side left
  pack .am.buttons.z1.value -side right -padx 10
  pack .am.buttons.z2.entry -side left
  pack .am.buttons.z2.value -side right -padx 10
  pack .am.buttons.z3.button -side left 
  pack .am.buttons.z4.button -side left 
  pack .am.buttons.z5.entry -side left
  pack .am.buttons.z5.value -side right -padx 10
  pack .am.buttons.z6.scale -side left
  pack .am.buttons.z7.entry -side top
  pack .am.buttons.z7.button1 -side left
  pack .am.buttons.z7.button2 -side left
  pack .am.buttons.z7.button3 -side left
  pack .am.buttons.z7.button4 -side left
  pack .am.buttons.z8.entry -side top
  pack .am.buttons.z8.button1 -side left
  pack .am.buttons.z8.button2 -side left
  pack .am.buttons.z8.button3 -side left
  pack .am.buttons.z9.entry -side top
  pack .am.buttons.z9.button1 -side left
  pack .am.buttons.z9.button2 -side left
  pack .am.buttons.z10.scale -side left

  pack .am.options -side top -fill x -pady 5
  pack .am.options.ok -side right -padx 5
  pack .am.options.save -side left -padx 5

  tkwait window .am

}




# change the configfile

proc alterunits {} {
  global realunits flagwarning nightstart nightstop zonelength refreshtime zonedaytime button

  toplevel .au 
  wm transient .au .
  wm title .au "config"
  wm geometry .au +300+100
  grab .au
  
  frame .au.text 
  label .au.text.label -text "Alter the configs:"

  frame .au.buttons

  frame .au.buttons.z1
  entry .au.buttons.z1.value -width 10 -textvariable zone(1)
  radiobutton .au.buttons.z1.button -cursor question_arrow -text "Zone 1" -variable button -value 1

  frame .au.buttons.z2
  entry .au.buttons.z2.value -width 10 -textvariable zone(2)
  radiobutton .au.buttons.z2.button -cursor question_arrow -text "Zone 2" -variable button -value 2

  frame .au.buttons.z3 
  entry .au.buttons.z3.value -width 10 -textvariable zone(3)
  radiobutton .au.buttons.z3.button -cursor question_arrow -text "Zone 3" -variable button -value 3

  frame .au.buttons.z4  
  entry .au.buttons.z4.value -width 10 -textvariable zonemoney
  label .au.buttons.z4.entry -cursor question_arrow -text " Length of one unit"

  frame .au.buttons.z5 
  entry .au.buttons.z5.value -width 10 -textvariable currency
  label .au.buttons.z5.entry -cursor question_arrow -text " Your currency"

  frame .au.buttons.z6 
  checkbutton .au.buttons.z6.button -cursor question_arrow -text "Iconify after connect" -variable Iconify

  frame .au.buttons.z16 
  checkbutton .au.buttons.z16.button -cursor question_arrow -text "Show terminal" -variable showterminal

  frame .au.buttons.z7 
  checkbutton .au.buttons.z7.button -cursor question_arrow -text "Show Onlinetime in Icon" -variable showmoneyinicon

  frame .au.buttons.z8 
  entry .au.buttons.z8.value -width 10 -textvariable ppp_up
  label .au.buttons.z8.entry -cursor question_arrow -text " PPP-Up"

  frame .au.buttons.z9 
  entry .au.buttons.z9.value -width 10 -textvariable ppp_down
  label .au.buttons.z9.entry -cursor question_arrow -text " PPP-Down"

  frame .au.buttons.z17 
  entry .au.buttons.z17.value -width 10 -textvariable startdelay
  label .au.buttons.z17.entry -cursor question_arrow -text " Delay after CONNECT (sec)"

  frame .au.buttons.z10  
  scale .au.buttons.z10.scale -length 13c -relief ridge -label " Refresh of time" -from 1 -to 60 -orient horizontal -variable refreshtime

  frame .au.buttons.z11  
  scale .au.buttons.z11.scale -length 13c -relief ridge -label " Begin of nightzone" -from 1 -to 24 -orient horizontal -variable nightstart 

  frame .au.buttons.z12 
  scale .au.buttons.z12.scale -length 13c -relief ridge -label " End of nightzone" -from 1 -to 24 -orient horizontal -variable nightstop 

  frame .au.buttons.z13 
  checkbutton .au.buttons.z13.button -cursor question_arrow -text "Warning (in minutes)" -variable flagwarning -command {configwarning $flagwarning}
  scale .au.buttons.z13.scale -length 13c -relief ridge -from 1 -to 120 -orient horizontal -variable warningtime
  
  frame .au.buttons.z14 
  checkbutton .au.buttons.z14.button0 -cursor question_arrow -text "Son" -variable zoneday(0)
  checkbutton .au.buttons.z14.button1 -cursor question_arrow -text "Mon" -variable zoneday(1)
  checkbutton .au.buttons.z14.button2 -cursor question_arrow -text "Tue" -variable zoneday(2)
  checkbutton .au.buttons.z14.button3 -cursor question_arrow -text "Wed" -variable zoneday(3)
  checkbutton .au.buttons.z14.button4 -cursor question_arrow -text "Thu" -variable zoneday(4)
  checkbutton .au.buttons.z14.button5 -cursor question_arrow -text "Fri" -variable zoneday(5)
  checkbutton .au.buttons.z14.button6 -cursor question_arrow -text "Sat" -variable zoneday(6)

  label .au.buttons.z15 -cursor question_arrow -text " Days with wholeday-nightzone"

  frame .au.options 
  button .au.options.ok -cursor target -text Ok -command {set zonelength $zone($button)
    set realunits  [expr $zonelength * $zonedaytime]
    destroy .au
  }
  button .au.options.save -cursor target -text Save -command {saveconfigfile}



  pack .au.text -side top
  pack .au.text.label -side left
  pack .au.buttons -pady 5
  pack .au.buttons.z8 .au.buttons.z9 .au.buttons.z17 .au.buttons.z1 .au.buttons.z2 .au.buttons.z3 .au.buttons.z4 -side top -fill x
  pack .au.buttons.z5 .au.buttons.z10 .au.buttons.z11 .au.buttons.z12 -fill x -side top
  pack .au.buttons.z15 -side top -anchor w
  pack .au.buttons.z14 .au.buttons.z13 -fill x -side top
  pack .au.buttons.z6 .au.buttons.z16 .au.buttons.z7 -fill x -side top
  pack .au.buttons.z1.button -side left 
  pack .au.buttons.z1.value -side right -padx 10
  pack .au.buttons.z2.button -side left
  pack .au.buttons.z2.value -side right -padx 10
  pack .au.buttons.z3.button -side left
  pack .au.buttons.z3.value -side right -padx 10
  pack .au.buttons.z4.entry -side left
  pack .au.buttons.z4.value -side right -padx 10
  pack .au.buttons.z5.entry -side left
  pack .au.buttons.z5.value -side right -padx 10
  pack .au.buttons.z6.button -side left
  pack .au.buttons.z7.button -side left
  pack .au.buttons.z8.entry -side left 
  pack .au.buttons.z8.value -side right -padx 10
  pack .au.buttons.z9.entry -side left 
  pack .au.buttons.z9.value -side right -padx 10
  pack .au.buttons.z17.entry -side left 
  pack .au.buttons.z17.value -side right -padx 10
  pack .au.buttons.z10.scale -pady 3 -side left 
  pack .au.buttons.z11.scale -pady 3 -side left
  pack .au.buttons.z12.scale -pady 3 -side left
  pack .au.buttons.z13.button -side top -anchor w
  pack .au.buttons.z13.scale -side top -side left
  pack .au.buttons.z14.button0 -side left
  pack .au.buttons.z14.button1 -side left
  pack .au.buttons.z14.button2 -side left
  pack .au.buttons.z14.button3 -side left
  pack .au.buttons.z14.button4 -side left
  pack .au.buttons.z14.button5 -side left
  pack .au.buttons.z14.button6 -side left
  pack .au.buttons.z16.button -side left
  
  pack .au.options -side top -fill x -pady 5
  pack .au.options.ok -side right -padx 5
  pack .au.options.save -side left -padx 5

  configwarning $flagwarning
  tkwait window .au

}

# dis/enable warningscale

proc configwarning flagwarning {
  if {$flagwarning == 0} {
    .au.buttons.z13.scale configure -state disabled -troughcolor grey
  } else {
    .au.buttons.z13.scale configure -state normal -troughcolor [option get .au.buttons.z10.scale activeBackground Foreground]
  }
} 

# show the realunits

proc realunits {} {
  global nightstart nightstop zonelength zonedaytime 
  upvar #0 zoneday localzoneday
  getpartzone zonedaytime $nightstart $nightstop $localzoneday(0) $localzoneday(1) $localzoneday(2) $localzoneday(3) $localzoneday(4) $localzoneday(5) $localzoneday(6) 
  tk_dialog .realunits "The real units" "Zonelength: [expr $zonelength * $zonedaytime]" info 0 ok
}
   
# save the configfile (I/O)

proc saveconfigfile {} {
  global configfile
  global button refreshtime ppp_up ppp_down instdir zonemoney currency showmoneyinicon Iconify
  global modeminit dialtype modemnumber startdelay nightstart nightstop flagwarning warningtime
  global showterminal modemtimeout modemspeed modemparity modemstopbits modemdevice repeatdelay
  upvar #0 zone localzone
  upvar #0 zoneday localzoneday
  set Datei [open $configfile w+]
  puts $Datei $instdir
  puts $Datei $localzone(1)
  puts $Datei $localzone(2)
  puts $Datei $localzone(3)
  puts $Datei $zonemoney
  puts $Datei $currency
  puts $Datei $showmoneyinicon
  puts $Datei $Iconify
  puts $Datei $ppp_up
  puts $Datei $ppp_down
  puts $Datei $refreshtime
  puts $Datei $button
  puts $Datei $nightstart
  puts $Datei $nightstop
  puts $Datei $flagwarning
  puts $Datei $warningtime
  puts $Datei $localzoneday(0)
  puts $Datei $localzoneday(1)
  puts $Datei $localzoneday(2)
  puts $Datei $localzoneday(3)
  puts $Datei $localzoneday(4)
  puts $Datei $localzoneday(5)
  puts $Datei $localzoneday(6)
  puts $Datei $startdelay
  puts $Datei $modeminit
  puts $Datei $modemnumber
  puts $Datei $dialtype
  puts $Datei $modemdevice
  puts $Datei $repeatdelay
  puts $Datei $modemspeed
  puts $Datei $modemparity
  puts $Datei $modemstopbits
  puts $Datei $modemtimeout
  puts $Datei $showterminal
  close $Datei
}
  
# move goodiepicture (enter)

proc goodieenter {} {
  global linestatus mypicdown2 mypicup2
  if {$linestatus == "LINE up"} {
    .info.action.ppp.info configure -image $mypicup2
  } else {
    .info.action.ppp.info configure -image $mypicdown2
  }
}

# move goodiepicture (leave)

proc goodieleave {} {
  global linestatus mypicdown mypicup
  if {$linestatus == "LINE up"} {
    .info.action.ppp.info configure -image $mypicup
  } else {
    .info.action.ppp.info configure -image $mypicdown
  }
}

proc showterminal {} {

  set result [catch {toplevel .sm}]
  if {$result == 1} return 
  wm title .sm "terminal"
  wm geometry .sm +300+100
  wm focusmodel .sm active
  frame .sm.input
  text .sm.input.text -relief raised -bd 2 -xscrollcommand ".sm.input.scroll2 set" -yscrollcommand ".sm.input.scroll1 set"
  scrollbar .sm.input.scroll1 -command ".sm.input.text yview"
  scrollbar .sm.input.scroll2 -orient horizontal -command ".sm.input.text xview"
  button .sm.exit -text "Exit" -command {destroy .sm}
  
  pack .sm.input -side top
  pack .sm.input.scroll2 -side bottom -fill x
  pack .sm.input.scroll1 -side right -fill y
  pack .sm.input.text -side left
  pack .sm.exit -ipadx 1c -pady 1m -side bottom

}

proc changestate {} {
  global linestatus
  set result [tk_dialog .state "Change state" "Change the state of the line" questhead 2 up down cancel]
  if {$result == 0} {
    set linestatus "LINE up"
    startonlinetime
  }
  if {$result == 1} {
    down
  }
}
    
  

# Main-Program

pppinfo
Time
if {$commandoption == "-start"} {
  up
}
if {$commandoption == "-close"} {
  down
}
# set linestatus "LINE up"
# startonlinetime
