#ifdef	ournix
#include "ournix.h"
#endif
char sccsID[] =	"@(#) sleep.c V1.6 Copyright Julian H. Stacey 13th May 1987\n";

/* see manual
 Like Unix sleep, but enhanced for hours, minutes, & continuous display */

#ifdef ns32000	/* { */
#define BSD
#endif		/* } */

#include <stdio.h>
#include <time.h>
#include <sys/types.h> /* BSD Symmetric: typedef int time_t */

#ifdef	BSD
#include	<sys/signal.h>
#else
#include	<signal.h>
#endif

extern	int	atoi() ;
extern	long	atol() ;
extern	char	*ctime() ;
extern	long	time() ;

/* extern sleep(x) unsigned x ; {} ;	compiles, doesnt sleep */
/* extern sleep(i) unsigned i; {}	compiles, doesnt sleep */

#ifdef	unix	/* { */
#define	SLEEP_AVAILABLE
#endif		/* } */
#ifdef	MSDOS	/* { */
#undef	SLEEP_AVAILABLE
#endif		/* } */

typedef	char FLAG ;

char	**ARGV ;

long_sleep(seconds)
	unsigned long	seconds ;
	{
	long	now, finish ;
	for (finish = time(&now) + seconds ; now < finish ; (void)time(&now) ) ;
	}

user_int() 
	{
#ifndef	MSDOS	/* { */
	(void) putchar('\n') ;
#endif		/* } */
	exit(1) ;
	}

main(argc,argv)
	int argc; char **argv ;
	{
	unsigned long	seconds = 0L ;
	long		now, finish ;
	FLAG		unset	= 1 ;
	FLAG		verbose = 0 ;
	FLAG		bells = 0 ;
	unsigned	interval = 1 ;
	char		output[200] ;
	char		*pp ;
	unsigned	string = 0 ;
	unsigned	count ;
	unsigned	volume = 
				/* multiple bells == louder - note
				bells running directly on msdos sound louder/
				longer than bells sent from unix to an msdos
				running kermit terminal emulator */
#ifdef	unix	/* { */
	2
#else		/* } { */
	4
#endif		/* } */
			;

	ARGV = argv ;
#ifdef	VSL	/* { */
#include	"../../include/vsl.h"
#endif		/* } */
	while (--argc > 0)
		{
		if (**++argv == '-')
			{
			/* an option selector */
			switch(*++*argv)
				{
				case 'y': /* year */
				case 'e': /* easter */
				case 'M':	/* month */
					fprintf(stderr,
				"Year, Easter, Month: All not implemented\n"
					);
					usage(1) ;
					break ;
				case 'l': /* lunar month = 4 weeks */
					seconds += 4L * 7L * 24L * 60L * 60L *
						atol(++*argv) ;
					unset = 0 ;
					break ;
				case 'w': /* week */
					seconds += 7L * 24L * 60L * 60L *
						atol(++*argv) ;
					unset = 0 ;
					break ;
				case 'd': /* days */
					seconds += 24L * 60L * 60L *
						atol(++*argv) ;
					unset = 0 ;
					break ;
				case 'h': /* hours */
					seconds += 60L * 60L * atol(++*argv) ;
					unset = 0 ;
					break ;
				case 'm':		/* minutes */
					seconds += 60L * atol(++*argv) ;
					unset = 0 ;
					break ;
				case 's': /* seconds */
					seconds += atol(++*argv) ;
					unset = 0 ;
					break ;
				case 'v': /* verbose */
					verbose = 1 ;
					break ;
				case 'b': /* bells */
					bells = 1 ;
					if (*++*argv != '\0')
						interval = atoi(*argv) ;
					break ;
				case 'V': /* volume */
					volume = atoi(++*argv) ;
					break ;
				default:usage(1);
				}
			}
		else	/* not an option selector */
			{
			seconds += atol(*argv) ;
			unset = 0 ;
			break ;
			}
		}

	/* check invocation */
	if (unset) usage(1) ;

	if (!verbose)
		{
#ifdef	SLEEP_AVAILABLE	/* { */
		sleep((unsigned)seconds) ;
#else			/* } { */
		long_sleep(seconds) ;
#endif		/* } */
		}
	else	{
		finish = time(&now) + seconds ;
		printf("Started :\t%s", ctime(&now) ) ;
		printf("Will finish :\t%s", ctime(&finish) ) ;
		printf("Time now :\t") ;
		while ( now <= finish )
			{	/* continuosly update time on screen */
			(void) sprintf(output,"%s",ctime(&now));
			for ( pp = output ; *pp++ ; ) ;
			*(pp - 2) = '\0' ;	/* delete '\n' */
			while (string--) (void) putchar('\b') ;
			printf("%s",output) ;
#ifdef		unix	/* { */
			(void) fflush(stdout) ;
#endif		/* } */
			string = strlen(output) ;
			long_sleep(1L) ;	/* avoid using excessive cpu
						usage, vdu display blurring
						from continuous update, and
						unnecessary RF interference
						if terminal has badly shielded
						serial line */
			(void)time(&now) ;
			}
		}

	if (bells)
		{
		(void) signal(SIGINT, user_int);
		while(1)
			{
			for (count = volume ; count-- ; ) 
#define		BELL	7
				(void) putchar((char)BELL) ;
#ifdef		unix	/* { */
			(void) fflush(stdout) ;
#endif			/* } */
			long_sleep((unsigned long)interval) ;
			}
		}

#ifndef	MSDOS	/* { */
	if (verbose) printf("\n");
#endif		/* } */

	exit(0);
	}	/* end of main */

usage(ret)
	int ret ;
	{
	fprintf(stderr,"%sSyntax:\t%s\n\t%s\n%s\n", sccsID, *ARGV,
		"[-v] [-b[interval]] [-Vvolume] [-lLunarMonths] [-wWeeks]",
			"[-hHours] [-mMinutes] [-sSeconds] [Seconds]",
			(ret)?" Aborting":"Continuing");
	if (ret) exit(ret);
	}
/* end of file */
