#!/afs/athena/contrib/perl/perl
'di';
'ig00';
#
# tompalka
#
# $Id: make-timecard.pl,v 2.41 93/05/30 16:05:33 tompalka Exp $
#
# $Log:	make-timecard.pl,v $
# Revision 2.41  93/05/30  16:05:33  tompalka
# added an eval() to count location specific fillin hours
# 
# Revision 2.40  93/05/25  17:53:15  tompalka
# added 'special' keyword
# 
# Revision 2.37  93/05/24  16:27:06  tompalka
# print out the name of the user too.
# 
# Revision 2.36  93/05/24  16:14:15  tompalka
# the ascii version, with manpage and the whole shameel
# 
#

($mypath,$WHOAMI) = ( $0 =~ /^([^ ]*)\/([^\/]*)$/ );

$homedir      = $ENV{"HOME"};
$sheetfile    = "/afs/athena.mit.edu/user/t/o/tompalka/.timeform";

$rcfile       = $homedir . "/.timecardrc";
%daynumber    = ('mon',1,'tue',2,'wed',3,'thu',4,'fri',5,'sat',6,'sun',7);


&open_rcfile;

# read the file.  $week is set to the current week -- thanks to
# the $* and $/ we're slurping in a week at a time.  whenthe
# loop exits, then $week contains the last one.
#
$* = 1;				# multi-line slurps
$/ = "-------\n";		# record separator (slurp by weeks)
open(timecard,$TIMECARDFILE) || die "cannot open $TIMECARDFILE: $!\n";
while(<timecard>){
    $week = $_;
}
close(timecard);

# process each line of the week.
foreach $line (split('\n',$week)) {
    &parse_line($line);
}

# process the fillins and qa topics
foreach (sort(keys %qa_topics)) {
    $qa_topics_string .= $_." ";
}

foreach (sort(keys %fillins)) {
    $fillin_string .= $_." ";
}

# print out the whole thing.
&print_it_out;

if ($extra_hours =~ /[a-zA-Z]/ ) {
    print '!' x 50 ,"\n";
    print "EXTRA HOURS --- DESCRIPTIONS NOT INCLUDED ABOVE:\n";
    print $extra_hours,"\n";
    print '!' x 50 ,"\n";
}			

print "\nyou have worked a total of ",$GRANDTOTAL," hours this week.\n";

if( $pay != 0 ) {
    $pay *= $GRANDTOTAL;
    $paytax = $pay * .82;

    printf "that amounts to \$%6.2lf, or about \$%6.2lf after taxes.\n\n",
    $pay, $paytax;
}

exit;



# ----------------------------------------
# parse_line routine parses the line of the current hour entry.
#

sub parse_line
{
    local($_)=@_;
    local($total) = 0;

    s/\s+/ /g;
    tr/A-Z/a-z/;

    if( ! /^ / ) {
	($day,$numday,$month,$start,$end,$total,$desc) = split(' ',$_,7);
    } else 	{
	($start,$end,$total,$desc) = split(' ',$_,4);
    }

    # follow the convention for description
    # ^olc     -> scheduled olc hour
    # ^mcr     -> scheduled mcr hour
    # ^atic    -> scheduled atic hour

    # ^olc meeting -> olc meeting
    # ^mcr meeting -> mcr meeting
    # ^meeting     -> olc meeting

    # ^qa          -> olc qa
    # ^qm          -> olc qm

    # ^olc fillin  -> name of consultant whose hour we took
    # ^mcr fillin  -> name of consultant whose hour we took
    # ^fillin      -> name of consultant whose hour we took

    $_ = $desc;

    LOOP:
    {
	/^olc meeting/ && ( $olc_meeting  += $total, next LOOP );
	/^mcr meeting/ && ( $mcr_meeting  += $total, next LOOP );
	/^meeting/     && ( $olc_meeting  += $total, next LOOP );

	/^qm/          && ( $qm_scheduled += $total, next LOOP );

	/^qa/ && do {
	    $qa_scheduled += $total;
	    s/^qa//;
	    s/\s+/\ /;
	    foreach $i (split(' ',$_)) { $qa_topics{$i}++; }
	    next LOOP;
	};

	/^olc fillin/  && ( &parse_fillin("olc",$_), next LOOP );
	/^mcr fillin/  && ( &parse_fillin("mcr",$_), next LOOP );
	/^fillin/      && ( &parse_fillin("olc",$_), next LOOP );
 
	/^olc special/ && ( &parse_special("olc",$_),next LOOP );
	/^mcr special/ && ( &parse_special("mcr",$_),next LOOP );

	/^olc/         && ( $olc_scheduled  += $total, next LOOP );
	/^mcr/         && ( $mcr_scheduled  += $total, next LOOP );
	/^atic/        && ( $atic_scheduled += $total, next LOOP );
	
	# what if it doesn't match any of the above?  consider
	/[^ ]/         && &parse_special("olc",$_);
    }

    $day_total{$day} += $total;
    $GRANDTOTAL      += $total;
}

##################################################
#
# parse_special( $location, $line )
#

sub parse_special {
    local($loc,$_)=@_;

    eval( '$special_'.$loc.' += '.$total.';' );

    s/^.*special\s*//;
    push(@special_desc, $_);
}

##################################################
#
# parse_fillin( $location, $line )
#

sub parse_fillin {
    local($loc,$_)=@_;

    $fillin_total += $total;
    s/^fillin//;
    s/\s+/\ /;
    foreach $i (split(' ',$_)) { 
	$fillins{$i}++;
    }

    # eval something like $olc_fillin += $total;
    $cmd='$'.$loc.'_fillin += '.$total.';'; #'
    eval($cmd);
}


# ----------------------------------------
#
#
sub open_rcfile
{
    $*=0;
    $/="\n";
    open(RCFILE,$rcfile) || die "cannot open $rcfile: $!\n";
    while(<RCFILE>) {
	s/\s+/ /g;
	($field,$contents) = ( $_ =~ /([^ ]*): (.*)$/ );
	$userinfo{$field} = $contents;
    }
    
    close(RCFILE);
    
    $TIMECARDFILE = $userinfo{datafile};
    $pay          = $userinfo{pay};
    
    $verbose = 1;
}


# ----------------------------------------
#
#
sub print_it_out {

    print <<EOF;

 **************************************************
 ****  U S E   A T   Y O U R   O W N   R I S K ****
 **************************************************

 Name:  $userinfo{signature}

 Enter the TOTAL TIME you worked each day:
 
EOF
    ;

    if( -e $sheetfile ) {
	open(TF,$sheetfile) || die "cannot open $sheetfile: $!\n";
	$* = 0;
	$/ = "\n";

	# slurp the whole file into $_
	while(<TF>) {
	    /^\s+..\/.. ...:/ && do {
		($day) = ($_ =~ /^\s+..\/.. (...):/);
		s/:.*$/:/;
		$day =~ tr/A-Z/a-z/;
		$day =~ s/thr/thu/;
		chop;
		print " ",$_,"  ", $day_total{$day},"\n";
	    };
	}
	close TF;
    } else {
	#
	# no $sheetfile there!
	#
	print <<EOF;
		*************************
		*** this has no dates ***
		*************************

		     Mon:  $day_total{"mon"}
		     Tue:  $day_total{"tue"}
		     Wed:  $day_total{"wed"}
		     Thu:  $day_total{"thu"}
		     Fri:  $day_total{"fri"}
		     Sat:  $day_total{"sat"}
                     Sun:  $day_total{"sun"}
EOF
    ;
    }

    print <<EOF;
    Total Hours for Week:  $GRANDTOTAL

Enter the TOTAL TIME you worked in each category:

Scheduled Hours: Athena: $olc_scheduled
                  Micro: $mcr_scheduled
            Access Tech: $atic_scheduled

Fill-In Hours:   Athena: $olc_fillin
                  Micro: $mcr_fillin
               For Who?: $fillin_string

Cons Meeting:    Athena: $olc_meeting
                  Micro: $mcr_meeting

Self-Train Hours:Athena:
                  Micro:
            Doing What?:

             QA: Athena: $qa_scheduled
               On What?: $qa_topics_string

              QM:Athena: $qm_scheduled

Special Projects:Athena: $special_olc
                  Micro: $special_mcr
EOF
     ;
    
    #
    # now print out "Doing What? For Who?: " with a description
    # for each special project

    foreach $proj (@special_desc) {
	print "   Doing What? For Who?: ",$proj,"\n";
    }

    print "\n";
}
###############################################################

    # These next few lines are legal in both Perl and nroff.

.00;                       # finish .ig
 
'di           \" finish diversion--previous line must be blank
.nr nl 0-1    \" fake up transition to first page again
.nr % 0         \" start at page 1
'; __END__ ##### From here on it's a standard manual page #####

.TH MAKE-TIMECARD 1 "May 24, 1993"
.AT 3
.SH NAME
make-timecard \- produce an ascii timecard form
.SH SYNOPSIS
\fBmake-timecard 
.SH DESCRIPTION
.I Make-timecard
reads through a data file to produce a listing of the hours worked.
.LP
.I Make-timecard 
needs a data file containing information about what hours were worked.
One file holds information about all weeks -- the weeks are separated with
lines that contain at least four consecutive dashes.  Each entry takes one
line, and should contain the date, starting time, ending time, total
time, and a description of the hour taken.  Here are some sample
entries:
.LP
.TP 5
   tue 23 apr 0900 1000 1\.00 \f olc 
.TP 1
   wed 24 apr 1515 1545 0\.50 \f olc qa omail omatlab 
.TP 1
   wed 24 apr 1715 1745 0\.50 \f mcr meeting
.TP 1
   fri 26 apr 1945 2015 0\.50 \f qa omail omatlab
.TP 1 
   fri 27 apr 1800 1900 1\.00 \f mcr
.TP 1
   sat 28 apr 1900 2000 1\.00 \f fillin fooconsultant
.LP
The only really important fields are the name of the day, the total
amount of hours worked, and the description.  Since the strings are
space-delimited, you have to put something for the date, starting and
ending times.  If you do not want to use them, putting any character
will work.
.LP
The description fields are very important.  Any description can be prefixed with 
.I olc
or
.I mcr
or
.I atic
to specify which hotline it was worked on.  The following fields can be used to describe the detailed 
function:
.I meeting
for meetings,
.I fillin
for hours that you took for someone else,
.I qa
for Q/A hours (remember to specify what you did QA on),
.I qm
for queue management hours.
.LP
.SH $HOME/.TIMECARDRC
The 
.I $HOME/.timecardrc
file contains your signature and the name of the file that has the data for hours worked.
The following two fields are necessary. The 
.I signature
is the string that gets put on the top of the timecard and the extra-hours form.  An example could be
.I Joe User (joeuser)
for Joe User whose username is "joeuser".
The
.I datafile
specifies the full path to the datafile.
.LP
Also, the keyword
.I pay
can be used to specify a salary.  In that case 
.I make-timecard
will report the total amount of money earned this week.  A sample 
.I $HOME/.timecardrc
file might be:
.TP 1
   datafile: /mit/shmoe/olc/timecard-file
.TP 1
   signature: joe shmoe (schmoe@athena.mit.edu)
.TP 1
   pay: 13.00
.LP
If you run the program and you don't have the .timecardrc file the program will attempt to create one.
.DT
.LP
.SH AUTHOR
Thomas M. Palka (tompalka@mit.edu).
