#!/usr/local/bin/perl

# Interactively adds an entry to the users time sheet directory
# Part of the Time Sheet toolkit.
# (C) Wilhelm Person 2002
# Version 2

$configfile = "/home/wilper-8/Timesheet_dev/timesheet.conf";

# --------------------------

#
# Determine what date to use
#

($Second, $Minute, $Hour, $Day, $Month, $Year, $Weekday, $DayOfYear, $IsDTS)=localtime(time);

$realmonth = $Month +1;
if($realmonth<10){
	$realmonth= "0".$realmonth;
}

$realday = $Day;
if($realday<10){
	$realday = "0".$realday;
}

$realyear = $Year + 1900;

$realdate=$realyear."-".$realmonth."-".$realday;

if( $#ARGV +1==1){
	print "Date was given as: ".$ARGV[0].", use it? [Y/n]\n";
	$foo = <STDIN>;
	if($foo eq "\n" || $foo eq "Y\n" || $foo eq "y\n") {
		$date=$ARGV[0];
		goto DATEOK;
	}
}

print "Todays date is:".$realdate.", use it? [Y/n]\n";
$foo = <STDIN>;
if($foo eq "\n" || $foo eq "Y\n" || $foo eq "y\n") {
	$date=$realdate;
	goto DATEOK;
}

print "Please enter desired date (yyyy-mm-dd).\n";
$foo = <STDIN>;
chop($foo);
$date=$foo;

DATEOK:	


#
# read activity labels from the timesheet.conf file
#

open(CONFFILE,"< $configfile") || die "Could not find".$configfile."\n";
while(<CONFFILE> ne "begin activity\n"){}
$row = <CONFFILE>;
$num_act=0;
while($row ne "end\n"){
	@rowsplit = split(/:/, $row);	
	chop(@rowsplit[1]);
	
	@act_key[$num_act]=@rowsplit[0];
	@act_name[$num_act]=@rowsplit[1];

	$row = <CONFFILE>;
	$num_act=$num_act+1;
}
$num_act=$num_act;
close(CONFFILE);

#
# Read where to put the data-files from timesheet.conf 
#

open(CONFFILE,"< $configfile") || die "Could not find ".$configfile."\n";
	while(<CONFFILE> ne "begin files\n"){}
$row=<CONFFILE>;
chop($row);
while($row ne "end"){
	@filesplit = split(/:/, $row);
	if(@filesplit[0] eq "datafiles"){
		print "Storing data in: ";
		$datahome=@filesplit[1];
		print $datahome."\n";
		goto FOUND_DATAHOME;
	}		

	$row=<CONFFILE>;
	chop($row);
}
print "Cound not find any 'datafiles' entry in ".$configfile."\n";
die;
FOUND_DATAHOME:
close(CONFFILE);


#
# Check to see if that date already has data.
#

$datafilename=$datahome.$date;

# print "Checking if $datafilename already exists.\n";

open(DATAFILE,"<$datafilename")|| goto ENTER_DATA;

print "There already is an entry for this date.\n";
print "Use it for defaults? [Yn]:\n";
$foo = <STDIN>;
if($foo eq "\n" || $foo eq "Y\n" || $foo eq "y\n") {
    $line = <DATAFILE>; # read past the date
    for($i=0;$i<$num_act;$i++){
	$line = <DATAFILE>;
	chop($line);
	@linesplit = split(/:/, $line);
	@act_value[$i]=@linesplit[1];
    }
    $diary = <DATAFILE>; # the diary is the last line in the file
}
close(DATAFILE);

ENTER_DATA:
#
# read info from the user
#
    
    print "Enter values:\n";
for($i=0;$i<$num_act;$i++){
    print "@act_name[$i] .[@act_value[$i]]  : ";
    $foo = <STDIN>;
    chop($foo);
	if($foo==""){
	}else{
	    @act_value[$i]=$foo;
	}
}



print "You have given the following time report for " . $date . "\n";
for($i=0;$i<$num_act;$i=$i+1){
	print @act_name[$i] . " : " . @act_value[$i] . " hours.\n";
}

print "Is this correct? [Y/n]\n";
$foo = <STDIN>;
if($foo eq "\n" || $foo eq "Y\n" || $foo eq "y\n") {
	goto ENTER_DIARY;
}
goto ENTER_DATA;


ENTER_DIARY:
print $diary; # if there is a diary entry it will be shown
print "Add/Replace diary entry? [Y/n]\n";
$foo = <STDIN>;
if($foo eq "\n" || $foo eq "Y\n" || $foo eq "y\n") {
    print "Write an entry, end with [Enter].\n";
    $diary = <STDIN>;
    
    print "Is the diary entry  correct? [Y/n]\n";
    $foo = <STDIN>;
    if($foo eq "\n" || $foo eq "Y\n" || $foo eq "y\n") {
	goto WRITE_DATA;
    }
    goto ENTER_DIARY;
}
goto WRITE_DATA;


WRITE_DATA:
#
# Open the datafile for writing and add the information.
#

$datafilename=$datahome.$date;
open(DATAFILE,">$datafilename")|| die "Could not create datafile\n";
print DATAFILE $date."\n";
for($i=0;$i<$num_act;$i=$i+1){
    if(@act_value[$i]==0){
	print DATAFILE @act_key[$i].":"."\n";
    }else{
	print DATAFILE @act_key[$i] . ":" . @act_value[$i] . "\n";
   }
}
print DATAFILE $diary."\n";
close(DATAFILE);

print "Done!\n"













