/*! cc -o # -O % -ltermlib
 */
#ifndef lint
static char sccsid[] = "@(#)stdate.c	1.4 (UKC) 10/7/86";
#endif  lint
/***

* program name:
	stdate.c
* function:
	Reads the current time and sets the time on the Atari ST
	terminal emulator
* switches:
	-c	Turn the ST clock on
* libraries used:
	standard
* compile time parameters:
	cc -o stsetdate -O stsetdate.c
* history:
	Written 4/July/1986
	Peter Collinson UKC

***/
#include <stdio.h>
#include <sys/types.h>
#include <sys/time.h>


main(argc, argv)
char **argv;
{	time_t ti;
	register struct tm *tm;
	struct tm *localtime();

	if (not_an_atari_pc())
		exit(0);

	(void) time(&ti);
	tm = localtime(&ti);
	
	printf("\033[%d;%d;%dv\033[%d;%d;%dw",
			tm->tm_mday,
			tm->tm_mon+1,
			tm->tm_year,
			tm->tm_hour,
			tm->tm_min,
			tm->tm_sec);
	if (argc != 1)
		printf("\033[v");
	exit(0);
}

/*
 *	Use termcap to detect whether we are an atari pc or not
 */
not_an_atari_pc()
{	char	tbuf[BUFSIZ];
	register char *scanch;
	register char *TtyName;
	register char *lookfor;
	register looklen;
	char *getenv();

	TtyName = getenv("TERM=");
	if (TtyName == NULL)
		return(1);

	if (tgetent(tbuf, TtyName) != 1)
		return(1);
	lookfor = "atari-pc";
	looklen = strlen(lookfor);
	for (scanch = tbuf; *scanch != ':';)
	{	if(*lookfor == *scanch && strncmp(lookfor, scanch, looklen) == 0)
			return(0);
		while (*scanch != '|' && *scanch != ':')
			scanch++;
		if (*scanch == '|')
			scanch++;
	}
	return(1);
}
