#ifdef	ournix
#include "ournix.h"
#endif
char sccsID[] = "@(#) timesheet.c V1.1 Copyright Julian H. Stacey 1988" ;

#include <stdio.h>
#include <stdlib.h>
char	**ARGV ;

char time_hr_min[] = " Hour:Minute\t\t\t";

#define GRIPE gripe(hour_start,min_start,hour_end,min_end)

main(argc,argv)
	int argc ; char **argv ;
	{
	int	hour_mth = 0 ;	/* hours total in month */
	int	min_mth = 0 ;	/* minutes total in month */
	int	hour_day = 0 ;	/* hours that day */
	int	min_day = 0 ;	/* minutes that day */
	int	hour_start = 0 ;/* start hour */
	int	min_start = 0 ;	/* start minute */
	int	hour_end = 0 ;	/* stop hour */
	int	min_end = 0 ;	/* stop minute */
	int	min_lunch = 0 ;	/* lunch time, minutes */
	long	start, stop, lunch, x ;	/* temps */
	unsigned	line = 1 ;	/* to help user keep track of line number */
	int silent = 1 ;

	ARGV = argv ;
#ifdef	VSL	/* { */
#include	"../../include/vsl.h"
#endif		/* } */
	if (!silent)
		{
		printf(
		 "Examples:\tOvernight session:\t\tStart 21:00,\tEnd 26:00\n");
		printf(
		 "\t\tInclude previous total:\t\tStart 0:0,\tEnd 52:30\n");
		printf(
		 "\t\tDeduct mistake:\t\t\tLunch -80\n\n");
		}
	do	{
b_start:
		if (!silent) printf("%02d\tStart%s",line,time_hr_min);
		if ((scanf("%d:%d", &hour_start ,&min_start) != 2) ||
			( hour_start < 0 ) || (hour_start > 23) ||
			( min_start < 0 ) || (min_start > 59) )
			{ GRIPE ; goto b_start ; }
		start = hour_start * 60 + min_start ;
b_end:
		if (!silent) printf("\tStop %s\t",time_hr_min);
		if ((scanf("%d:%d", &hour_end, &min_end) != 2) ||
			( hour_end < 0 ) )
			/* no check of > 24, this allows us to enter
			overnight jobs stopping at 3am as 27:00 ! */
			{
			GRIPE ;
			goto b_end ;
			}
		if (( min_end < 0 ) || (min_end > 59) )
			 { GRIPE ; goto b_end ; }
		stop = hour_end * 60 + min_end ;

		if ( 0 > (x = (stop - start ))) { GRIPE ; goto b_start ; }
b_lunch:
		/* no checks here, so we can cludge totals */
		if (!silent) printf("\tLunch Duration, (Mins.):\t\t\t\t");
		if (scanf("%d",&min_lunch) != 1)
			{
			GRIPE;
			goto b_lunch ;
			}
		/* no > 59 checks here,
			frequently people say they had a 70 minute lunch */
		lunch = min_lunch ;
		x -= lunch ;

		line++ ;
		hour_day = x / 60 ; min_day = x % 60 ;
		hour_mth += hour_day ; min_mth += min_day ;
		hour_mth += min_mth / 60 ; min_mth %= 60 ;
		printf("At %02d:%02d-%02d:%02d ",
			hour_start,min_start,hour_end,min_end);
		printf("\tMonth, & Day :\t\t%3d:%02d\t%02d:%02d\n\n",
			hour_mth, min_mth,hour_day,min_day);
		}
	while (hour_day || min_day) ;
	exit(0);
	}

gripe(hour_start,min_start,hour_end,min_end)
	int	hour_start ;	/* start hour */
	int	min_start ;	/* start minute */
	int	hour_end ;	/* stop hour */
	int	min_end ;	/* stop minute */
	{
	printf("\nAt %02d:%02d-%02d:%02d ",hour_start,min_start,hour_end,min_end);
	printf("something strange with numbers, ");
	(void) scanf("%*s\n") ;
	printf("try again.\n");
	(void) fflush(stdout);
	}
