#!/usr/bin/perl
#=======================================================================
# OnlineCC   - calculates costs of a SLIP or PPP connection to the
#              main German online providers
#
# Copyright (C) 1996-1997 Reinhold Schoeb (schoeba@str.daimler-benz.com)
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#=======================================================================

# $Log: OnlineCC,v $
# Revision 1.18  1997/04/09 19:28:07  kalli
# New help button
#
# Revision 1.17  1997/01/06 12:41:17  kalli
# GPL added
#
# Revision 1.16  1997/01/01 14:39:18  kalli
# Internet Provider Mode (SWOL) dazu
#
# Revision 1.15  1996/10/26 12:53:39  kalli
# Final rev. 0.6
#
# Revision 1.14  1996/10/25 20:22:28  kalli
# Erweiterung auf Datex-J-Betrieb
#
# Revision 1.13  1996/10/19 16:09:03  kalli
# Umstellung auf OnlineCC
#
# Revision 1.12  1996/10/03 10:02:43  kalli
# Final rev. 1.5
#
# Revision 1.11  1996/09/29 17:20:06  kalli
# Bildschirmaufbau neu angeordnet
#
# Revision 1.10  1996/09/29 16:53:23  kalli
# Berechnung der Gesamtkosten ueber Logfile dazu
#
# Revision 1.9  1996/09/29 09:22:10  kalli
# Bugfix : Logfile wird nur noch beschrieben bei Betaetigen der
# Stop-Taste
#
# Revision 1.8  1996/09/29 09:10:06  kalli
# Feiertage 1997 dazu
#
# Revision 1.7  1996/09/26 15:48:07  kalli
# Problem beim Betaetigen von Stop und Update im Offline-
# Zustand behoben
# Dial-Up Verzoegerungszeit eingefuehrt
#
# Revision 1.6  1996/09/21 18:26:23  kalli
# Feiertage 1996 dazu
#
# Revision 1.5  1996/09/13 21:05:59  kalli
# Umstellung auf T-OnlineCC
#
# Revision 1.7  1996/09/11 20:35:55  kalli
# Start und Stop mit dip-Script-Aufruf
# Farbresourcen fuer Buttons
# Resourcen fuer Online-Button werden im Programm gesetzt
#
# Revision 1.6  1996/09/10 20:20:57  kalli
# Umstellung auf neue Berechnung mit Counter
#
# Revision 1.5  1996/09/10 19:16:49  kalli
# Option "Show logfile" dazu
#
# Revision 1.4  1996/09/08 18:15:19  kalli
# Umstellung auf Radiobuttons
#
# Revision 1.3  1996/09/08 09:56:55  kalli
# Feiertagstarife dazu
#
# Revision 1.2  1996/09/07 15:35:34  kalli
# Umstellung auf Fester mit Update-Button
# Log-File wird beschrieben
#
# Revision 1.1  1996/09/07 11:30:51  kalli
# Initial revision

#---------------------------------------------------------------------
# SetDefaults
#---------------------------------------------------------------------

sub SetDefaults
{
    $Debug = 0;

    # Command to start your Datex-J connection
    $DialupDatexJCommand  = 'xcept >/dev/console &';

    # Common Dialup delay time 
    $DialupDelay = 15;

    # Phone costs per unit (in DM)
    $PhoneCostPerUnit = 0.12;

    # T-Online Datex-J cost per minute (in DM)
    %OnlineCostPerMin   = ("Nacht",    0.02,
                           "Tag",      0.06
			   );

    # CompuServe online costs per minute (in DM)
    $CisOnlineCostPerMin = (2.95 * 1.45) / 60;

    # Time per unit on workdays (Citytarif)
    %WorkdayCity    =  ("Vormittag",     90.0,
                        "Nachmittag",    90.0,
                        "Freizeit",     150.0,
                        "Nacht",        240.0,
                        "Mondschein",   240.0,
                       );
    # Time per unit on Saturdays/Sundays (Citytarif)
    %SatSundayCity  =  ("Vormittag",    150.0,
                        "Nachmittag",   150.0,
                        "Freizeit",     150.0,
                        "Nacht",        240.0,
                        "Mondschein",   240.0,
                       );
    # Time per Unit on holidays (Citytarif)
    %HolidayCity     = ("Vormittag",    150.0,
                        "Nachmittag",   150.0,
                        "Freizeit",     150.0,
                        "Nacht",        240.0,
                        "Mondschein",   240.0,
                       );
    # Time per Unit during the Christmas week (Citytarif)
    %ChristmWeekCity = ("Vormittag",    150.0,
                        "Nachmittag",   150.0,
                        "Freizeit",     150.0,
                        "Nacht",        240.0,
                        "Mondschein",   240.0,
                       );
}

#---------------------------------------------------------------------
# GetDateTime
#---------------------------------------------------------------------

sub GetDateTime
{
    @timelist = localtime (time());

    $day   = $timelist[3];          # get better variablenames
    $month = $timelist[4] + 1;
    $year  = $timelist[5] + 1900;
    $hour  = $timelist[2];
    $min   = $timelist[1];
    $sec   = $timelist[0];

    # Time in seconds
    $Time = ($hour * 60 * 60) + ($min * 60) + $sec;

    %days = (0, "Sunday", 1, "Monday", 2, "Tuesday", 3, "Wednesday",
             4, "Thursday", 5, "Friday", 6, "Saturday");

    foreach $x (keys(%days)) {      # get the right dayname from ass.list
	if ($timelist[6] == $x) {
            $dayname = $days{$x};
        }
    }
}
    
#---------------------------------------------------------------------
# CalculateRateTime
#---------------------------------------------------------------------

sub CalculateRateTime
{
    # Which time rate ?
    if (($Time >= 0) && ($Time < 7200)) {            #  0.00 to  2.00
	$TimeRate = "Mondschein";
    } elsif (($Time >=  7200) && ($Time < 18000)) {  #  2.00 to  5.00
	$TimeRate = "Nacht";
    } elsif (($Time >= 18000) && ($Time < 32400)) {  #  5.00 to  9.00
	$TimeRate = "Freizeit";
    } elsif (($Time >= 32400) && ($Time < 43200)) {  #  9.00 to 12.00
	$TimeRate = "Vormittag";
    } elsif (($Time >= 43200) && ($Time < 64800)) {  # 12.00 to 18.00
	$TimeRate = "Nachmittag";
    } elsif (($Time >= 64800) && ($Time < 75600)) {  # 18.00 to 21.00
	$TimeRate = "Freizeit";
    } elsif (($Time >= 75600) && ($Time < 86400)) {  # 21.00 to 24.00
	$TimeRate = "Mondschein";
    } else {
	$TimeRate = "error";
    }
}

#---------------------------------------------------------------------
# GetDayType
#---------------------------------------------------------------------

sub GetDayType
{
    # Which day ?
    if (($month ==  1) && ($day ==  1)) {        # Neujahrstag
	$DayType = "Feiertag";
    } elsif (($month ==  5) && ($day ==  1)) {   # Maifeiertag
	$DayType = "Feiertag";
    } elsif (($month == 10) && ($day ==  3)) {   # Tag der deutschen Einheit
	$DayType = "Feiertag";
    } elsif (($month == 12) && ($day == 24)) {   # Heiligabend
	$DayType = "Feiertag";
    } elsif (($month == 12) && ($day == 25)) {   # 1. Weihnachtstag
	$DayType = "Feiertag";
    } elsif (($month == 12) && ($day == 26)) {   # 2. Weihnachtstag
	$DayType = "Feiertag";
    } elsif (($month == 12) && ($day == 31)) {   # Silvester
	$DayType = "Feiertag";
    } elsif (($year == 1996) && ($month == 4) && ($day ==  5)) {
	$DayType = "Feiertag";                   # Karfreitag 1996
    } elsif (($year == 1996) && ($month == 4) && ($day ==  8)) {
	$DayType = "Feiertag";                   # Ostermontag 1996
    } elsif (($year == 1996) && ($month == 5) && ($day == 16)) {
	$DayType = "Feiertag";                   # Christi Himmelfahrt 1996
    } elsif (($year == 1996) && ($month == 5) && ($day == 27)) {
	$DayType = "Feiertag";                   # Pfingstmontag 1996
    } elsif (($year == 1997) && ($month == 3) && ($day ==  28)) {
	$DayType = "Feiertag";                   # Karfreitag 1997
    } elsif (($year == 1997) && ($month == 3) && ($day ==  31)) {
	$DayType = "Feiertag";                   # Ostermontag 1997
    } elsif (($year == 1997) && ($month == 5) && ($day == 8)) {
	$DayType = "Feiertag";                   # Christi Himmelfahrt 1997
    } elsif (($year == 1997) && ($month == 5) && ($day == 19)) {
	$DayType = "Feiertag";                   # Pfingstmontag 1997
    } elsif (($month == 12) && (($day >= 27) && ($day <= 30))) {
	$DayType = "Weihnachtswoche";
    } elsif (($dayname eq "Saturday") || ($dayname eq "Sunday")) {
	$DayType = "SaSo";
    } else {
	$DayType = "Werktag";
    }

    if (($DayType eq "Weihnachtswoche") && ($DayType eq "SaSo")) {
	$DayType = "Feiertag";
    }
}

#---------------------------------------------------------------------
# CalculateTimePerUnit
#---------------------------------------------------------------------

sub CalculateTimePerUnit
{
    # Which day rate ?
    if ($DayType eq "Weihnachtswoche") {
	$TimePerUnit = $ChristmWeekCity{$TimeRate};
    } elsif ($DayType eq "SaSo") {
	$TimePerUnit = $SatSundayCity{$TimeRate};
    } elsif ($DayType eq "Feiertag") {
	$TimePerUnit = $HolidayCity{$TimeRate};
    } else {
	$TimePerUnit = $WorkdayCity{$TimeRate};
    }
}

#---------------------------------------------------------------------
# CalculateOnlineRateTime
#---------------------------------------------------------------------

sub CalculateOnlineRateTime
{
    if ($OPmode eq "T-Online mode") {
	# Which rate time ?
	if ($DayType eq "Werktag") {
	    if (($Time >= 0) && ($Time < 64800)) {           #  0.00 to 18.00
	        $OnlineRate = "Tag";
	    } elsif (($Time >= 64800) && ($Time < 86400)) {  # 18.00 to 24.00
		$OnlineRate = "Nacht";
	    } else {
		$OnlineRate = "error";
	    }
	} elsif (($DayType eq "SaSo") || ($DayType eq "Feiertag")) {
	    $OnlineRate = "Nacht";
	} else {
	    $OnlineRate = "error";
	}
	# calculate online rate time
	$ResOnlineCostPerMin = $OnlineCostPerMin{$OnlineRate};
	# add rate for internet access
	$ResOnlineCostPerMin = $ResOnlineCostPerMin + 0.05 if !($OnlineState eq "Datex-J");

    } elsif ($OPmode eq "CompuServe mode") {
	$ResOnlineCostPerMin = $CisOnlineCostPerMin;
    }
}

#---------------------------------------------------------------------
# CalculateCosts
#---------------------------------------------------------------------

sub CalculateCosts
{
    # get lap time
    &GetDateTime;
    $LapTime = $Time;
    # day changed ?
    if ($LapTime < $StartTime) {
	$LapTime = $LapTime + (60 * 60 * 24);
    }
    # get day type
    &GetDayType;
    # calculate
    &CalculateRateTime;
    &CalculateTimePerUnit;
    &CalculateOnlineRateTime;

    # time in dialup delay ?
    if ($LapTime < $StartTime + $DialupDelay) {

	$UsedTime = 0;
	$PhoneCosts = 0;
	$OnlineCosts = 0;

    } else {

	# calculate used time
	$UsedTime = $LapTime - $StartTime - $DialupDelay;
	# reset costs
	$PhoneCost = $PhoneCostPerUnit;
	$OnlineCost = $ResOnlineCostPerMin;
	# Start new counter
	$PhoneCounter = $TimePerUnit;
	$OnlineCounter = 60;

	for ($n = ($StartTime + $DialupDelay); $n < $LapTime; $n++) {

	    if ($PhoneCounter > 0) {
		$PhoneCounter--;

	    } else {
		# get day type
		&GetDayType;
		# calculate
		&CalculateRateTime;
		&CalculateTimePerUnit;
		&CalculateOnlineRateTime;
		# add one unit to the resulting phone costs
		$PhoneCost = $PhoneCost + $PhoneCostPerUnit;
		# Start new counter
		$PhoneCounter = $TimePerUnit;
	    }

	    if ($OnlineCounter > 0) {
		$OnlineCounter--;

	    } else {
		# get day type
		&GetDayType;
		# calculate
		&CalculateRateTime;
		&CalculateTimePerUnit;
		&CalculateOnlineRateTime;
		# add one unit to the resulting online costs
		$OnlineCost = $OnlineCost + $ResOnlineCostPerMin;
		# Start new counter
		$OnlineCounter = 60;
	    }
	}
    }
    # calculate all Costs
    $Costs = $PhoneCost + $OnlineCost;
}

#---------------------------------------------------------------------
# CalcCost
#---------------------------------------------------------------------

sub CalcCost
{
    $total = 0;
    foreach $i (@_) {                # @_ = array 
        $total += $i;               
    }
    $total;                         # return the sum
}

#---------------------------------------------------------------------
# CalcLogfileCosts
#---------------------------------------------------------------------

sub CalcLogfileCosts
{
    # open logfile
    open (LFILE, "$LogFile") || printf "Can't open logfile : $!\n";

    $a = 0;
    $b = 0;

    # read values from logfile
    while (<LFILE>)
    {
	if (/^Phone.*(.{6})$/) {
	    $CResPhoneCosts[$a++] = $1;
	}
	if (/^Online.*(.{6})$/) {
	    $CResOnlineCosts[$b++] = $1;
	}
	if (/^Costs.*(.{6})$/) {
	    $CResCosts[$b++] = $1;
	}
    }

    # close logfile
    close (LFILE);

    $LogfilePhoneCosts = &CalcCost (@CResPhoneCosts);
    $LogfileOnlineCosts = &CalcCost (@CResOnlineCosts);
    $LogfileCosts = &CalcCost (@CResCosts);

}

#---------------------------------------------------------------------
# main
#---------------------------------------------------------------------

# Look for command line parameters
printf "OnlineCC ";
printf "version 0.8, Copyright (C) 1996-1997 Reinhold Schoeb\n";
printf "OnlineCC comes with ABSOLUTELY NO WARRANTY. This is free software, and you are\n";
printf "welcome to redistribute it under the terms of the GNU General Public License.\n";

if ($ARGV[0] eq "-cis") {
    printf "Starting CompuServe mode\n";
    $OPmode = "CompuServe mode";
    # Name of the logfile
    $LogFile = '/usr/local/OnlineCC/log/OnlineCC-cis.log';
    # Command to start a online connection
    $DialupCommand = 'dip /usr/local/OnlineCC/CIS.dip >/dev/console &';
    # Command to stop a online connection
    $DialoffCommand = 'dip -k >/dev/console';

} elsif ($ARGV[0] eq "-to") {
    printf "Starting T-Online mode\n";
    $OPmode = "T-Online mode";
    # Name of the logfile
    $LogFile = '/usr/local/OnlineCC/log/OnlineCC-to.log';
    # Command to start a online connection
    $DialupCommand = 'dip /usr/local/OnlineCC/T-Online.dip >/dev/console &';
    # Command to stop a online connection
    $DialoffCommand = 'dip -k >/dev/console';

} elsif ($ARGV[0] eq "-ip") {
    printf "Starting Internet Provider mode\n";
    $OPmode = "Internet Provider mode";
    # Name of the logfile
    $LogFile = '/usr/local/OnlineCC/log/OnlineCC-ip.log';
    # Command to start a online connection
    $DialupCommand = "/usr/sbin/pppd /dev/modem 115200 connect \"/usr/sbin/chat -V '' 'ATZ\r\n' 'OK' 'ATDT07024942
0\r\n' 'CONNECT'\" crtscts remotename swol.de noipdefault +ua /etc/swol.id defaultroute";
    # Command to stop a online connection
    $DialoffCommand = "kill `ps -a | grep pppd | grep -v grep | cut -b 1-6`;echo pppd stopped >/dev/console";

} elsif ($ARGV[0] eq "-aol") {
    #printf "Starting AOL mode\n";
    #$OPmode = "AOL mode";
    # Name of the logfile
    #$LogFile = '/usr/local/OnlineCC/log/OnlineCC-aol.log';
    # Command to start a online connection
    #$DialupCommand = 'dip /usr/local/OnlineCC/AOL.dip >/dev/console &';
    # Command to stop a online connection
    #$DialoffCommand = 'dip -k >/dev/console';
    printf "AOL mode not implemented\n";
    exit;

} elsif ($ARGV[0] eq "-msn") {
    #printf "Starting MSN mode\n";
    #$OPmode = "MSN mode";
    # Name of the logfile
    #$LogFile = '/usr/local/OnlineCC/log/OnlineCC-msn.log';
    # Command to start a online connection
    #$DialupCommand = 'dip /usr/local/OnlineCC/MSN.dip >/dev/console &';
    # Command to stop a online connection
    #$DialoffCommand = 'dip -k >/dev/console';
    printf "MSN mode not implemented\n";
    exit;

} elsif (($ARGV[0] eq "-help") || ($ARGV[0] eq "-h")) {
    printf "Usage : OnlineCC [param]\n";
    printf "Param : -to   T-Online mode (default)\n";
    printf "        -cis  CompuServe mode\n";
    printf "        -ip   Internet Provider mode\n";
    exit;

} else {
    printf "Starting T-Online mode\n";
    $OPmode = "T-Online mode";
    # Name of the logfile
    $LogFile = "/usr/local/OnlineCC/log/OnlineCC-to.log";
    # Command to start a online connection
    $DialupCommand = 'dip /usr/local/OnlineCC/T-Online.dip >/dev/console &';
    # Command to stop a online connection
    $DialoffCommand = 'dip -k >/dev/console';
}

# Finesse perl script initialization
$ENV{'FINESSEPATH'} = '/usr/local/finesse' if !$ENV{'FINESSEPATH'};
die "Can't open finesse\n" if !-x "$ENV{'FINESSEPATH'}/fsperlinit";
require("$ENV{'FINESSEPATH'}/fsperlinit");

# Set up finesse application server
&Fsopen(@ARGV);

# Create finesse windows
$ShowWindow = "FsWindow -name ShowWindow -title 'OnlineCC 0.8 - $OPmode';

               FsRadio -items 'SaSo/Werktag/Feiertag/Weihnachtswoche'
                       -nrows 1 -label 'Day type :' -var WDayType=Werktag -inputsep '/';

               FsSeparator;

               FsRadio -items 'Vormittag/Nachmittag/Freizeit/Mondschein/Nacht'
                       -nrows 1 -label 'Time rate :' -var WTimeRate=Vormittag -inputsep '/';

               FsSeparator;

               FsRadio -items 'Tag/Nacht'
                       -nrows 1 -label 'Online rate :' -var WOnlineRate=Tag -inputsep '/';

               FsSeparator;

               FsForm  -name form1 -orientation horizontal -nrows 3 -packing tight -spacing 20;
               FsSeparator;
               FsForm  -name form2 -orientation horizontal -nrows 3 -packing tight -spacing 20;
               FsForm  -name form3 -orientation horizontal -nrows 3 -packing tight -spacing 20;
               FsForm  -name form4 -orientation horizontal -nrows 3 -packing tight -spacing 20;

               FsText  -label 'Time per unit (sec) :'       -var WTimePerUnit==$i        -packing tight -parent form1;
               FsText  -label 'Online time (sec) :'         -var WUsedTime==$i           -packing tight -parent form1;

               FsText  -label 'Phone costs (DM) :'          -var WPhoneCosts==$i         -packing tight -parent form2;
               FsText  -label 'Logfile phone costs (DM) :'  -var WLogfilePhoneCosts==$i  -packing tight -parent form2;  

               FsText  -label 'Online costs (DM) :'         -var WOnlineCosts==$i        -packing tight -parent form3;
               FsText  -label 'Logfile online costs (DM) :' -var WLogfileOnlineCosts==$i -packing tight -parent form3;

               FsText  -label 'Resulting costs (DM) :'      -var WCost==$i               -packing tight -parent form4;
               FsText  -label 'Logfile res. costs (DM) :'        -var WLogfileCosts==$i       -packing tight -parent form4;  

               FsSeparator;

               FsRadio -items 'Offline/Online' -name OnOfflineRadio
                       -nrows 1 -var WOnlineState=Offline -inputsep '/';

               FsSeparator;

               FsPushButton -label Internet        -name StartButton  -fsbutton StartButton  -winstat touch -nrows 2;
               FsPushButton -label Datex-J         -name DatexJButton -fsbutton DatexJButton -winstat touch;
               FsPushButton -label Stop            -name StopButton   -fsbutton StopButton   -winstat touch;
               FsPushButton -label Update          -name UpdateButton -fsbutton UpdateButton -winstat touch;
               FsPushButton -label 'Show logfile'  -name ShowButton   -fsbutton ShowButton   -winstat touch;
               FsPushButton -label Help            -name HelpButton   -fsbutton HelpButton   -winstat touch;
               FsPushButton -label Exit            -name ExitButton   -fsbutton ExitButton   -winstat close;
              " if $OPmode eq "T-Online mode";

$ShowWindow = "FsWindow -name ShowWindow -title 'OnlineCC 0.8 - $OPmode';

               FsRadio -items 'SaSo/Werktag/Feiertag/Weihnachtswoche'
                       -nrows 1 -label 'Day type :' -var WDayType=Werktag -inputsep '/';

               FsSeparator;

               FsRadio -items 'Vormittag/Nachmittag/Freizeit/Mondschein/Nacht'
                       -nrows 1 -label 'Time rate :' -var WTimeRate=Vormittag -inputsep '/';

               FsSeparator;

               FsForm  -name form1 -orientation horizontal -nrows 3 -packing tight -spacing 20;
               FsSeparator;
               FsForm  -name form2 -orientation horizontal -nrows 3 -packing tight -spacing 20;
               FsForm  -name form3 -orientation horizontal -nrows 3 -packing tight -spacing 20;
               FsForm  -name form4 -orientation horizontal -nrows 3 -packing tight -spacing 20;

               FsText  -label 'Time per unit (sec) :'       -var WTimePerUnit==$i        -packing tight -parent form1;
               FsText  -label 'Online time (sec) :'         -var WUsedTime==$i           -packing tight -parent form1;

               FsText  -label 'Phone costs (DM) :'          -var WPhoneCosts==$i         -packing tight -parent form2;
               FsText  -label 'Logfile phone costs (DM) :'  -var WLogfilePhoneCosts==$i  -packing tight -parent form2;  

               FsText  -label 'Online costs (DM) :'         -var WOnlineCosts==$i        -packing tight -parent form3;
               FsText  -label 'Logfile online costs (DM) :' -var WLogfileOnlineCosts==$i -packing tight -parent form3;

               FsText  -label 'Resulting costs (DM) :'      -var WCost==$i               -packing tight -parent form4;
               FsText  -label 'Logfile res. costs (DM) :'        -var WLogfileCosts==$i       -packing tight -parent form4;  

               FsSeparator;

               FsRadio -items 'Offline/Online' -name OnOfflineRadio
                       -nrows 1 -var WOnlineState=Offline -inputsep '/';

               FsSeparator;

               FsPushButton -label Internet        -name StartButton  -fsbutton StartButton  -winstat touch;
               FsPushButton -label Stop            -name StopButton   -fsbutton StopButton   -winstat touch;
               FsPushButton -label Update          -name UpdateButton -fsbutton UpdateButton -winstat touch;
               FsPushButton -label 'Show logfile'  -name ShowButton   -fsbutton ShowButton   -winstat touch;
               FsPushButton -label Help            -name HelpButton   -fsbutton HelpButton   -winstat touch;
               FsPushButton -label Exit            -name ExitButton   -fsbutton ExitButton   -winstat close;
              " if $OPmode eq "CompuServe mode";

$ShowWindow = "FsWindow -name ShowWindow -title 'OnlineCC 0.8 - $OPmode';

               FsRadio -items 'SaSo/Werktag/Feiertag/Weihnachtswoche'
                       -nrows 1 -label 'Day type :' -var WDayType=Werktag -inputsep '/';

               FsSeparator;

               FsRadio -items 'Vormittag/Nachmittag/Freizeit/Mondschein/Nacht'
                       -nrows 1 -label 'Time rate :' -var WTimeRate=Vormittag -inputsep '/';

               FsSeparator;

               FsForm  -name form1 -orientation horizontal -nrows 3 -packing tight -spacing 20;
               FsSeparator;
               FsForm  -name form2 -orientation horizontal -nrows 3 -packing tight -spacing 20;
               FsForm  -name form3 -orientation horizontal -nrows 3 -packing tight -spacing 20;
               FsForm  -name form4 -orientation horizontal -nrows 3 -packing tight -spacing 20;

               FsText  -label 'Time per unit (sec) :'       -var WTimePerUnit==$i        -packing tight -parent form1;
               FsText  -label 'Online time (sec) :'         -var WUsedTime==$i           -packing tight -parent form1;

               FsText  -label 'Phone costs (DM) :'          -var WPhoneCosts==$i         -packing tight -parent form2;
               FsText  -label 'Logfile phone costs (DM) :'  -var WLogfilePhoneCosts==$i  -packing tight -parent form2;  

               FsText  -label 'Online costs (DM) :'         -var WOnlineCosts==$i        -packing tight -parent form3;
               FsText  -label 'Logfile online costs (DM) :' -var WLogfileOnlineCosts==$i -packing tight -parent form3;

               FsText  -label 'Resulting costs (DM) :'      -var WCost==$i               -packing tight -parent form4;
               FsText  -label 'Logfile res. costs (DM) :'        -var WLogfileCosts==$i       -packing tight -parent form4;  

               FsSeparator;

               FsRadio -items 'Offline/Online' -name OnOfflineRadio
                       -nrows 1 -var WOnlineState=Offline -inputsep '/';

               FsSeparator;

               FsPushButton -label Internet        -name StartButton  -fsbutton StartButton  -winstat touch;
               FsPushButton -label Stop            -name StopButton   -fsbutton StopButton   -winstat touch;
               FsPushButton -label Update          -name UpdateButton -fsbutton UpdateButton -winstat touch;
               FsPushButton -label 'Show logfile'  -name ShowButton   -fsbutton ShowButton   -winstat touch;
               FsPushButton -label Help            -name HelpButton   -fsbutton HelpButton   -winstat touch;
               FsPushButton -label Exit            -name ExitButton   -fsbutton ExitButton   -winstat close;
              " if $OPmode eq "Internet Provider mode";


# Init defaults
&SetDefaults;

# Set button states
$OnlineState        = 'Offline';
$OfflineButtonColor = 'LightGrey';
$OnlineButtonColor  = 'LightGrey';

# Main loop
for (;;) {

    $sTimePerUnit        = sprintf ("%5.2f", $TimePerUnit);
    $sUsedTime           = sprintf ("%5.2f", $UsedTime);
    $sPhoneCost          = sprintf ("%5.2f", $PhoneCost);
    $sOnlineCost         = sprintf ("%5.2f", $OnlineCost);
    $sCost               = sprintf ("%5.2f", $Costs);
    $sLogfilePhoneCosts  = sprintf ("%5.2f", $LogfilePhoneCosts);
    $sLogfileOnlineCosts = sprintf ("%5.2f", $LogfileOnlineCosts);
    $sLogfileCosts       = sprintf ("%5.2f", $LogfileCosts);

    # Show finesse window
    &Fsdisplay ("-w", $ShowWindow, "-n", "ShowWindow", "-s", "input",
		    "-v", "WDayType=$DayType",
		    "-v", "WTimeRate=$TimeRate",
		    "-v", "WOnlineRate=$OnlineRate",
		    "-v", "WTimePerUnit=$sTimePerUnit",
		    "-v", "WUsedTime=$sUsedTime",
		    "-v", "WPhoneCosts=$sPhoneCost",
		    "-v", "WOnlineCosts=$sOnlineCost",
		    "-v", "WCost=$sCost",
		    "-v", "WOnlineState=$OnlineState",
		    "-v", "WLogfilePhoneCosts=$sLogfilePhoneCosts",
		    "-v", "WLogfileOnlineCosts=$sLogfileOnlineCosts",
		    "-v", "WLogfileCosts=$sLogfileCosts",
		    "-r", "OnOfflineRadio[3]:background:$OfflineButtonColor",
		    "-r", "OnOfflineRadio[4]:background:$OnlineButtonColor"
		    );

    # which button was pressed ?
    if (($fsbutton eq "StartButton") || ($fsbutton eq "DatexJButton")) {

	# Switch button state
	if ($fsbutton eq "DatexJButton") {
	    $OnlineState = "Datex-J";
	} else {
	    $OnlineState = "Online";
	}
	$OnlineButtonColor  = 'Red';
	# get actual day/time
	&GetDateTime;
	# set start time
	$StartTime = $Time;
	# save start time
	$Sday = $day;
	$Smonth = $month;
	$Syear = $year;
	$Shour = $hour;
	$Smin = $min;
	$Ssec = $sec;
	# Calculate costs
	&CalculateCosts;
	# calculate logfile costs
	&CalcLogfileCosts;
	# Start online connection
	if ($OnlineState eq "Datex-J") {
	    system ($DialupDatexJCommand);
	} else {
	    system ($DialupCommand);
	}

    } elsif (($fsbutton eq "StopButton") && (($OnlineState eq "Online") || ($OnlineState eq "Datex-J"))) {

	# Stop online connection
	system ($DialoffCommand) if !($OnlineState eq "Datex-J");
	# Calculate costs
	&CalculateCosts;
	# Open logfile
	if (-w "$LogFile") {
	    open (FILE, ">> $LogFile")  || die "Can't append logfile : $!\n";
	} else {
	    open (FILE, "> $LogFile")   || die "Can't open to logfile : $!\n";
	}
	# Write to logfile
	print  FILE "-" x 40 ."\n";
	printf FILE "Start : %02d.%02d.%04d  %02d:%02d:%02d %s\n", $Sday, $Smonth, $Syear, $Shour, $Smin, $Ssec, $OnlineState;
	printf FILE "Stop  : %02d.%02d.%04d  %02d:%02d:%02d Offline\n", $day, $month, $year, $hour, $min, $sec;
	printf FILE "Phone costs  : DM %06.2f\n", $PhoneCost;
	printf FILE "Online costs : DM %06.2f\n", $OnlineCost;
	printf FILE "Costs        : DM %06.2f\n", $Costs;
	# Switch button state
	$OnlineState = "Offline";
	$OnlineButtonColor  = 'LightGrey';

    } elsif ($fsbutton eq "UpdateButton") {

	# calculate logfile costs
	&CalcLogfileCosts;
	# Calculate costs
	if (($OnlineState eq "Online") || ($OnlineState eq "Datex-J")) {
	    &CalculateCosts;
	}

    } elsif ($fsbutton eq "ShowButton") {

	# Show logfile
	&Fsecho ("-c 35", "-r 35", "-f", "$LogFile");

    } elsif ($fsbutton eq "HelpButton") {

	# Show help
	system "netscape -remote 'openFile(/usr/local/OnlineCC/doc/OnlineCC-doc-en.html)' || netscape /usr/local/OnlineCC/doc/OnlineCC-doc-en.html &";

    } elsif ($fsbutton eq "ExitButton") {

	#Terminate finesse application server
	&Fsclose;
	# Close logfile
	close (FILE);
	# Terminate program
	exit;
    }
}
















































































































































