#!/usr/local/bin/perl

# Generates an HTML presentation from datafiles.
# Part of the Time Sheet toolkit.
# (C) Wilhelm Person 2002
# Version 2

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

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

#
# Determine what name to put on the top of the report.
#

open(CONFFILE,"< $configfile") || die "Could not find ".$configfile."\n";
while(<CONFFILE> ne "begin user\n"){}
$row=<CONFFILE>;
chop($row);
while($row ne "end"){
    @filesplit = split(/:/, $row);
    if(@filesplit[0] eq "name"){
	print "Writing a report for: ";
	$name=@filesplit[1];
	print $name."\n";
	goto FOUND_NAME;
    }
    $row=<CONFFILE>;
    chop($row);
}
print "Cound not find any 'name' entry in ".$configfile."\n";
die;
FOUND_NAME:
    close CONFFILE;




#
# Determine what month to generate report for.
#

($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;

if( $#ARGV +1==1){
        print "Generate report for: ".$ARGV[0]."? [Y/n]\n";
        $foo = <STDIN>;
        if($foo eq "\n" || $foo eq "Y\n" || $foo eq "y\n") {
                $date=$ARGV[0];
                goto DATEOK;
        }
}

print "It is:".$realdate.", generate report? [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).\n";
$foo = <STDIN>;
chop($foo);
$date=$foo;

DATEOK: 

#
# How long is that month
#
# For now we assume that february has 29 days...
#
        @rowsplit = split(/-/, $date);   
    $foo=$rowsplit[1];
if ($foo=="01"||$foo=="03"||$foo=="05"||$foo=="07"||$foo=="08"||$foo=="10"||$foo=="12"){
    $monthlen=31;
}elsif($foo=="04"||$foo=="06"||$foo=="09"||$foo=="11"){
    $monthlen=30;
}else{
    $monthlen=29;
}

#
# 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 info from all datafiles
#

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 "Reading data from : ";
	$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;

# Read the file for each day.
for($i=1;$i<=$monthlen;$i++){
   $foo=$i;
   if($foo<10){
        $foo = "0".$foo;
   }

   $filename=$datahome.$date."-".$foo;
   open(DAY,$filename)|| goto NEXTDAY;
      print "Found entry for $filename\n";
      $thisdate=<DAY>;
      for($j=0;$j<$num_act;$j++){
         $line = <DAY>;
	 chop($line);
         @linesplit = split(/:/, $line);
	 $outdata[$i][$j]=@linesplit[1];
      }
   
   $diaryentry=<DAY>;
   $outdata[$i][$num_act+1]=$thisdate.$diaryentry;

   close(DAY);
NEXTDAY:
}

#
# Open the output file and write the report
#


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 "reportdir"){
	$reportdir=@filesplit[1];
	goto FOUND_REPORTDIR;
    }
    $row=<CONFFILE>;
    chop($row);
}
print "Cound not find any 'reportdir' entry in ".$configfile."\n";
die;
FOUND_REPORTDIR:
    close CONFIGFILE;

$reportname = $reportdir.$date.".html";
print "Writing report to $reportname\n";
open (REPORT, ">$reportname")|| die "Could not open $reportname for writing.\n"; 

# Header
print REPORT "<HTML><TABLE BORDER COLS=".($monthlen+2)." WIDTH=100% NOSAVE>\n";
print REPORT "<FONT SIZE=+2>$name - $date</FONT>\n";
print REPORT " <TR NOSAVE>\n";
print REPORT "  <TD>Activity</TD>";
    for($i=1;$i<=$monthlen;$i++){
	print REPORT "  <TD> $i </TD>\n";
    }      
print REPORT "  <TD>Tot.<TD>\n";
print REPORT "</TR>\n";


# Data
$monthsum=0;
for($j=0;$j<$num_act;$j++){
    print REPORT "<TR>\n <TD>$act_name[$j]</TD>\n";
    $hoursum=0;
    for($i=1;$i<=$monthlen;$i++){  
	$foo=$outdata[$i][$j];
       	$hoursum=$hoursum+$foo;
	if($foo==""){
	    $foo="&nbsp;";
	}
	elsif($foo<10){
	    $foo = " ".$foo;
	}
	print REPORT " <TD>$foo</TD>\n";
    }
    print REPORT  "<TD>$hoursum</TD>\n</TR>\n";
    $monthsum=$monthsum+$hoursum;
}


# Tail
print REPORT "</TABLE></HTML>\n";
# The sum row
print REPORT "Total this month: $monthsum hours.";

# The diary entries

print REPORT "<HR>Diary entries this month:<BR><BR>\n";

for($i=1;$i<=$monthlen;$i++){  
    if(length($outdata[$i][$num_act+1])>10){
	$foo=$outdata[$i][$num_act+1];
	print REPORT "$foo <BR>\n";
    }
}
print REPORT "<HR>\n";
close(REPORT);






