/* 
	Copyright (C) Erik Ljungström 2001-11-19 
	 
    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 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include <fstream.h> 
#include <string.h> 
#include <time.h> 
#include <stdlib.h> 
#include <stdio.h> 
int menu ();
void clear ();
bool filex ();
int dial ();
using namespace std;

int main ()
{
	clear ();
	menu ();		
}

int dial ()
{
	int seconds = 0, minutes = 0, hours = 0;
	fstream skit ("/usr/local/kppp_logger/online.log", ios::app);
	time_t starttime, endtime, timer;
	struct tm *tblock;
	timer = time (NULL);
	tblock = localtime (&timer);
	starttime = time (0);
	system ("kppp");
	endtime = time (0);
	seconds = endtime - starttime;
	minutes = seconds / 60;
	if (minutes >= 60)
	{

		hours = minutes / 60;
		minutes %= 60;
	}
	seconds = seconds % 60;
	clear();
	cout << endl << asctime (tblock) << "You were connected for: " << endl
		<< hours << " hours" << endl << minutes;
	cout << " minutes" << endl << (seconds - 15) << " seconds " << endl;

	skit << endl << asctime (tblock) << "You were connected for: " << endl
		<< hours << " hours" << endl;
	skit << minutes << " minutes" << endl << (seconds - 15) << " seconds " <<
		endl << endl;
	skit.close ();



}

int menu ()
{
	int val;
	char sure[10];
	bool ex;
	cout << endl << endl <<
		"\t[1] Connect \n\t[2] View online log \n\t[3] Erase online log \n\t[4] Save online log \n\t[5] Exit\n\n";
	cout << "\tPlease enter your choice: ";
	cin >> val;

	if (val == 3)
	{
		clear ();
		ex = filex ();
		if (ex == false)
		{
			cout << endl <<
				"\tFile does not exist. Try running option 1 first."
				<< endl;
			menu ();
		}
		else
		{
			cout << "\tAre you sure you want to do this? (Y/N): ";
			cin.ignore (10, '\n');
			cin.get (sure, 10);
			if ((strcmp (sure, "Y") ==0) || (strcmp(sure, "y"))  == 0)
			{
				system ("rm -rf /usr/local/kppp_logger/online.log");
				cout << "\tDone!" << endl;
				menu ();
			}
			else
				menu ();
		}
	}

	if (val == 1)
	{
		dial ();
		menu ();
	}
	
	
	if (val == 4){
		ex = filex();
		if (ex == false)
		{
			clear();
			cout << "\n\tFile does not exist. Try runing option 1 first." << endl;
			cin.clear();
			menu();
		}
		
		cout << endl << "\tYour file will be saved as 'online.log.save' in /usr/local/kppp_logger/";
		system("cp /usr/local/kppp_logger/online.log /usr/local/kppp_logger/online.log.save");
		menu();
	}
		
		
	
	
	if (val == 5)
	{
		clear ();
		exit (0);
	}

	if (val == 2)
	{
		for (int i = 0; i < 80; i++)
		{
			cout << endl;
		}
		ex = filex ();
		if (ex == false)
		{
			cout << endl <<
				"\tFile does not exist. Try running option 1 first."
				<< endl;
			menu ();
		}
		else
		{
			system ("cat /usr/local/kppp_logger/online.log");
			cin.clear ();
			cin.ignore (10, '\n');
			menu ();
		}
	}
}


bool filex ()
{
	FILE *tf = fopen ("/usr/local/kppp_logger/online.log", "r");
	if (!tf)
	{
		
		cin.ignore (10, '\n');
		return false;
	}
	else
		return true;
}


void clear ()
{
	for (int i = 0; i < 80; i++)
	{
		cout << endl;
	}
}