#!/usr/bin/perl
#=====================================================================
# T_OnlineCC   - calculates costs of a SLIP or PPP connection to the
#                German Telecom internet provider T-Online
#
# Reinhold Schoeb (schoeba@str.daimler-benz.com)
#=====================================================================

# $Log: T-OnlineCC,v $
# 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;

    # Name of the logfile
    $LogFile       = '/usr/local/T-OnlineCC/log/T-OnlineCC.log';

    # Command to start a online connection
    $DialupCommand = 'dip /usr/local/T-OnlineCC/T-Online.dip >/dev/console &';

    # Command to stop a online connection
    $DialoffCommand = 'dip -k >/dev/console';

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

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

    # 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 == 01) && ($day == 01)) {        # Neujahrstag
	$DayType = "Feiertag";
    } elsif (($month == 05) && ($day == 01)) {   # Maifeiertag
	$DayType = "Feiertag";
    } elsif (($month == 10) && ($day == 03)) {   # 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";
                                                 # Karfreitag 1997
                                                 # Ostersonntag 1997
                                                 # Ostermontag 1997
                                                 # Christi Himmelfahrt 1997
                                                 # Pfingstsonntag 1997
                                                 # 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
{
    # 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;
}

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

sub CalculateCosts
{
    # get lap time
    &GetDateTime;
    $LapTime = $Time;
    # get day type
    &GetDayType;
    # calculate
    &CalculateRateTime;
    &CalculateTimePerUnit;
    &CalculateOnlineRateTime;
    # calculate used time
    $UsedTime = $LapTime - $StartTime;
    # reset costs
    $PhoneCost = $PhoneCostPerUnit;
    $OnlineCost = $ResOnlineCostPerMin;
    # Start new counter
    $PhoneCounter = $TimePerUnit;
    $OnlineCounter = 60;

    for ($n = $StartTime; $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 phone costs
	    $OnlineCost = $OnlineCost + $ResOnlineCostPerMin;
	    # Start new counter
	    $OnlineCounter = 60;
	}

    }

    # calculate all Costs
    $Costs = $PhoneCost + $OnlineCost;
}

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

# Open logfile
if (-w "/usr/local/T-OnlineCC/log/T-OnlineCC.log") {
    open (FILE, ">>/usr/local/T-OnlineCC/log/T-OnlineCC.log")  || die "Can't append logfile : $!\n";
} else {
    open (FILE, ">/usr/local/T-OnlineCC/log/T-OnlineCC.log")    || die "Can't open to logfile : $!\n";
}

# 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 window
$ShowWindow = "FsWindow -name ShowWindow -title T-OnlineCC;
               FsSeparator;
               FsRadio -items 'SaSo/Werktag/Feiertag/Weihnachtswoche'
                       -nrows 2 -label 'Day type :' -var WDayType=Werktag -inputsep '/';
               FsSeparator;
               FsRadio -items 'Vormittag/Nachmittag/Freizeit/Mondschein/Nacht'
                       -nrows 2 -label 'Time rate :' -var WTimeRate=Vormittag -inputsep '/';
               FsSeparator;
               FsRadio -items 'Tag/Nacht'
                       -nrows 1 -label 'Online rate :' -var WOnlineRate=Tag -inputsep '/';
               FsSeparator;
               FsText  -label 'Time per unit (sec) :'   -var WTimePerUnit==$i;
               FsSeparator;
               FsText  -label 'Online time (sec) :'     -var WUsedTime==$i;
               FsSeparator;
               FsText  -label 'Phone costs (DM) :'      -var WPhoneCosts==$i;       
               FsSeparator;
               FsText  -label 'Online costs (DM) :'     -var WOnlineCosts==$i;       
               FsSeparator;
               FsText  -label 'Resulting costs (DM) :'  -var WCost==$i;
               FsSeparator;
               FsRadio -items 'Offline/Online' -name OnOfflineRadio
                       -nrows 1 -var WOnlineState=Offline -inputsep '/';
               FsSeparator;
               FsPushButton -label Start          -name StartButton  -fsbutton StartButton  -winstat touch;
               FsPushButton -label Stop           -name StopButton   -fsbutton StopButton   -winstat touch;
               FsPushButton -label Update         -name UpdateButton -fsbutton UpdateButton -winstat touch;
               FsSeparator;
               FsPushButton -label 'Show logfile' -name ShowButton   -fsbutton ShowButton   -winstat touch;
               FsPushButton -label Exit           -name ExitButton   -fsbutton ExitButton   -winstat close;
              ";

# Init defaults
&SetDefaults;

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

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

    # 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",
		    "-r", "OnOfflineRadio[3]:background:$OfflineButtonColor",
		    "-r", "OnOfflineRadio[4]:background:$OnlineButtonColor"
		    );

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

	# Switch button state
	$OnlineState = "Online";
	$OnlineButtonColor  = 'Red';
	# get actual day/time
	&GetDateTime;
	# set start time
	$StartTime = $Time;
	# Calculate costs
	&CalculateCosts;
	# Write to logfile
	print  FILE "-" x 40 ."\n";
	printf FILE "Start : %02d.%02d.%04d  %02d:%02d:%02d\n", $day, $month, $year, $hour, $min, $sec;
	# Start online connection
	system ($DialupCommand);

    } elsif ($fsbutton eq "StopButton") {

	# Stop online connection
	system ($DialoffCommand);
	# Switch button state
	$OnlineState = "Offline";
	$OnlineButtonColor  = 'LightSteelBlue';
	# Calculate costs
	&CalculateCosts;
	# Write to logfile
	printf FILE "Stop  : %02d.%02d.%04d  %02d:%02d:%02d\n", $day, $month, $year, $hour, $min, $sec;
	printf FILE "Phone costs  : %5.2f DM\n", $PhoneCost;
	printf FILE "Online costs : %5.2f DM\n", $OnlineCost;
	printf FILE "Costs        : %5.2f DM\n", $Costs;

    } elsif ($fsbutton eq "UpdateButton") {

	# Calculate costs
	&CalculateCosts;

    } elsif ($fsbutton eq "ShowButton") {

	# Show logfile
	&Fsecho ("-c 35", "-r 35", "-f", "/usr/local/T-OnlineCC/log/T-OnlineCC.log");

    } elsif ($fsbutton eq "ExitButton") {

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
















































































































































