/* 
	Copyright (C) Erik Ljungström (insight@metalab.unc.edu) 2001-12-22 
	 
    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> 
void filex ();
int dial ();
void readlog ();
void dellog ();
void savelog ();
void Usage ();
void leave ();
using namespace std;
bool ex;
int
main (int argc, char *argv[])
{
	if (argc != 2)
	{
		cout << "Try kppp_logger --help" << endl;
		exit (0);
	}
	if (strcmp (argv[1], "-c") == 0)
		dial ();
	else if (strcmp (argv[1], "--connect") == 0)
		dial ();
	else if (strcmp (argv[1], "-r") == 0)
		readlog ();
	else if (strcmp (argv[1], "-s") == 0)
		savelog ();
	else if (strcmp (argv[1], "-h") == 0)
		Usage ();
	else if (strcmp (argv[1], "-d") == 0)
		dellog ();
	else if (strcmp (argv[1], "--help") == 0)
		Usage ();
	else
	{
		cout << "Try kppp_logger --help" << endl;
		exit (0);
	}
}

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;
	if (seconds > 15)
		seconds = seconds - 15;
	cout << endl << asctime (tblock) << "You were connected for: " << endl
		<< hours << " hours" << endl << minutes;
	cout << " minutes" << endl << (seconds) << " seconds " << endl <<
		endl;
	skit << endl << asctime (tblock) << "Connected for: " << endl << hours
		<< " hours" << endl;
	skit << minutes << " minutes" << endl << (seconds) << " seconds " <<
		endl << endl;
	skit.close ();
	leave ();
}

void
filex ()
{
	FILE *tf = fopen ("/usr/local/kppp_logger/online.log", "r");
	if (!tf)
		cout << endl << "File does not exist. Creating one.." << endl;
	system ("touch /usr/local/kppp_logger/online.log");

}


void
readlog ()
{
	filex ();
	system ("cat /usr/local/kppp_logger/online.log");
	leave ();


}

void
dellog ()
{
	filex ();
	system ("rm -rf /usr/local/kppp_logger/online.log");
	leave ();
}

void
savelog ()
{
	filex ();
	cout << endl <<
		"Your file will be saved as 'online.log.save' in /usr/local/kppp_logger/"
		<< endl;
	system ("cp /usr/local/kppp_logger/online.log /usr/local/kppp_logger/online.log.save");
	leave ();

}

void
Usage ()
{
	cout << endl;
	cout << "Usage: kppp_logger [OPTION]" << endl << endl;
	cout << " 	-c,   --connect 	Starts a Kppp dialog (connects)" << endl;
	cout << " 	-r	Displays the online log" << endl;
	cout << " 	-d	Deletes the online log" << endl;
	cout << " 	-s	Saves the online log" << endl;
	cout << " 	-v	Prints out version message and exits" << endl;
	cout << " 	-h,  --help  	Prints out this message and exits" <<
		endl << endl;
	exit (0);
}

void
leave ()
{
	cout << "kppp_logger, coded by Erik Ljungström (C) 2001 Please read gpl.txt for copying notices." << endl << endl;
	exit (0);
}