/* Program: MOTD - 'Motto of the day' window
 *
 * By: Christopher Lane (lane@sumex-aim.stanford.edu)
 *     Symbolic Systems Resources Group
 *     Knowledge Systems Laboratory
 *     Stanford University
 *
 * Contributors:
 *
 *    Izumi Ohzawa (izumi@violet.berkeley.edu)
 *    Group in Neurobiology/School of Optometry
 *    University of California, Berkeley
 *
 *    Steve Hayman (sahayman@cs.indiana.edu)
 *    Computer Science Department Workstation Manager
 *
 * Date:  5 November 1992
 *
 * Copyright: 1989, 1990, 1991 & 1992 by The Leland Stanford Junior University.
 * This program may be distributed without restriction for non-commercial use.
 */

#import "MOTD.h"
#import "DefaultsTable.h"

#import <pwd.h>
#import <utmp.h>

#define FTPPREFIX "ftp"
#define TTYDEVICE "console" /* basename(ttyname(0)) doesn't work in LoginHook */

#define WTMPFILE getDefault("WTMPFile")
#define ONLYONCE getBoolDefault("OnlyOnce")

void timer(DPSTimedEntry teNumber, double now, id self)
{
	static int seconds;
	static BOOL first = YES;
	static const char *string;
	static char buffer[BUFSIZ];
	id button = [self holdButton];
	
	if (first) {
		string = NXCopyStringBuffer([button title]);
    	seconds = [self wait];
		first = NO;
		}

	if (![button state]) {
		if (seconds-- <= 0) [self terminate:self];
		else if ([self do_Motd]) {
			(void) sprintf(buffer, string, seconds);
			[[[button setTitle:buffer] setTransparent:NO] setEnabled:YES];
			}
		}
}

void main(int argc, char *argv[])
{
	FILE *f_utmp;
	struct stat s_stat;
	struct utmp s_utmp;
	BOOL flag, first = YES, do_motdL = YES;
	MailLevel maillevelL = NONE;
	char pathnamebuf[MAXPATHLEN];
	struct passwd *p_passwd = NULL;

	NXApp = [MOTD new];
    
	if (!(flag = (--argc && (p_passwd = getpwnam(argv[argc])) != NULL))) p_passwd = getpwuid((int) getuid());

	[[[NXApp setLoginHook:flag] setMailLevel:maillevelL] setDo_Motd:do_motdL];

	if (p_passwd != NULL) {
		[[NXApp setUserName:p_passwd->pw_name] setUID:p_passwd->pw_uid];
		
		if (flag) { /* only check if LoginHook */
			(void) strcat(strcpy(pathnamebuf, MAILDIR), [NXApp userName]); /* check for mail arrival */
			if (stat(pathnamebuf, &s_stat) != CERROR && s_stat.st_size != 0)
				[NXApp setMailLevel:(maillevelL = (s_stat.st_atime <= s_stat.st_mtime) ? NEW : OLD)];
			}

		if (ONLYONCE && stat([NXApp file], &s_stat) != CERROR && (f_utmp = fopen(WTMPFILE, "r")) != NULL) {
			while(fread(&s_utmp, sizeof(s_utmp), 1, f_utmp)) {
				if (s_stat.st_mtime < s_utmp.ut_time && strncmp(s_utmp.ut_name, [NXApp userName], sizeof(s_utmp.ut_name)) == EQ) {
					if ((flag && strncmp(s_utmp.ut_line, TTYDEVICE, sizeof(s_utmp.ut_line)) == EQ) ||
					    (strncmp(s_utmp.ut_line, FTPPREFIX, sizeof(s_utmp.ut_line)) != EQ)) {
						if (first) { first = NO; continue; } /* Kludge: Can't use first entry under 3.0 as it is the current login */
						[NXApp setDo_Motd:(do_motdL = NO)];
						break; /* while() */
						}
					}
				}
			(void) fclose(f_utmp);
			}
		}

	if (do_motdL || maillevelL != NONE) { /* if motd file not seen yet OR if there is mail */
		[NXApp loadNibSection:"MOTD.nib" owner:NXApp];
		if (do_motdL) [[NXApp cancelButton] setEnabled:flag];    
		[[NXApp run] free];
		}
	else [NXApp free];
	
	exit(EXIT_SUCCESS);	
}
