

/*
 * Copyright 1991 RightBrain Software.  All rights reserved.
 *
 * No part of this code may be reproduced in any form, compiled
 * or source code, nor used for any purpose without the express
 * written permission of RightBrain Software.
 * 
 * Entered into the public domain 12/15/93 by RightBrain Software.
 *
 */

#import "timebomb.h"

#import <appkit/TextField.h>
#import <appkit/appkit.h>

#import <pwd.h>
#import <ctype.h>
#import <time.h>
#import <strings.h>

#import "InfoPanel.h"
#import "Time.h"

#define DEVELOP 0
#ifndef EDIT_N
    #define EDIT_N 0
#endif
#ifndef VERSION
    #define VERSION 1.0
#endif

char users[256] = "**********";

@implementation InfoPanel

+ new
{
    self = [super new];
    return self;
}

- show
{
    [panel makeKeyAndOrderFront: self];
    return self;
}

/****************************** timeBombCheck */
- timeBombCheck
{
    Time *time;
    long cur, end;	/* time values */
    
    time = [Time new];
    cur = [time currentIntTime];
//    end = cur + 900;		// current time plus fifteen minutes (60 * 15)
    end = EXPIRE_TIME;		

    if ( cur > end ) {
	NXRunAlertPanel("Expired",
	    "This beta software has expired.  Call +1 415-326-2974 to order a working copy", "Quit", NULL, NULL
	);
	[NXApp terminate: self];
    }
    [time free];
    return self;
}


/****************************** setupVersionText */
- setupVersionText
{
    char version[128];

    sprintf ( version, "(v%2.1f edit %d", VERSION, EDIT_N);
    if ( TIME_BOMB_VERSION ) {
	strcat ( version, "*");
    }
    if ( !SINGLE_USER ) {
	strcat ( version, "M");
    }
    strcat ( version, ")" );
    [versionText setStringValue: version];
    myInfoPanel = self;

    return self;
}


- personalizeButton:sender
{

    [NXApp stopModal];
    return self;
    
} // personalize


- quitButton:sender
{
    [NXApp terminate:self];
    return self;
}

- checkRegistration:sender
{
    FILE *fd;
    char *chptr;
    int ch, cnt, tmp, tmpidx, tmplen, usercount, len;
    char host[28], userbuff[5], namebuff[128], userID[128], entry[1024];
    char userName[512], userCompany[512], userDate[512];
    long idx, placename, placecount;
    struct passwd *pwd;
    short found;
    long theTime;
    struct tm *date;
    NXModalSession session;
    int button, eventmask;
    int readOnly;

    #if SINGLE_USER
    if ( !strncmp ( "/LocalApps/", NXArgv[0], 11 ) ) {
	NXRunAlertPanel("Single User",
	    "This software is only licensed for 1 CPU and will not run from /LocalApps.  Call +1 415-326-2974 to order a multiple-use copy", "Quit", NULL, NULL
	);
	[NXApp terminate: self];
    }
    #endif
    
    gethostname ( host, 28 );
    pwd = getpwuid ( getuid() );
    sprintf ( userID, "%s@%s\0", pwd->pw_name, host );
    len = strlen ( userID );
    readOnly = FALSE;
    fd = fopen ( NXArgv[0], "r+" );
    if ( (int)fd <= 0 ) {
	fd = fopen ( NXArgv[0], "r" );
	if ( (int)fd <= 0 ) {
	    NXRunAlertPanel ( "Launch",
		"Problems: couldn't find licensing information.",
		NULL, NULL, NULL
	    );
	    exit ( 0 );
	}
	readOnly = TRUE;
    }
    // find the secret place
    idx = 0; placename = 0; placecount = 0;
    while ( (ch=fgetc(fd)) != EOF ) {
	idx++;
	if ( ch == '*' ) {
	    cnt=0;
	    while ( (ch=fgetc(fd)) != EOF ) {
		if ( ch == '*' ) { cnt++; } else {
		    if ( cnt > 5 ) {
			placecount = idx + 10;
			placename = idx + 13;	// leave the stars...
		    } else { idx += cnt + 1; }
		    break;
		}
	    }
	    if ( placename ) break;
	} // if
    } // while
    if ( ch != EOF ) {
	usercount = 0;
	// find out how many users are already in the database:
	for ( tmp=0; tmp<3; tmp++ ) {
	    ch=fgetc(fd);
	    usercount += (int)ch;
	    userbuff[tmp] = ch;
	}
	userbuff[tmp] = '\0';
	if ( usercount ) {
	    sscanf ( userbuff, "%d", &usercount );
	    found = TRUE;
	}
	// check to see if this user is already there:
	if ( DEVELOP ) {
	    [personalName setStringValue: "Glenn Reid"];
	    [personalCompany setStringValue: "RightBrain Software"];
	    [personalDate setStringValue: "UNDER DEVELOPMENT"];

	    [personalButton setEnabled:FALSE];
	    [quitButton setEnabled:FALSE];
	    [personalButton setTransparent:YES];
	    [quitButton setTransparent:YES];
	} else if ( usercount ) {
	    tmp = 0;
	    while ( (ch=fgetc(fd)) != '\n' ) {
		namebuff[tmp++] = ch;
		placename++;
	    }
	    namebuff[tmp] = '\0';
	    placename++;
	    
	    // pick it apart to put in the user panel
	    tmp = tmpidx = 0;
	    chptr = NULL;
	    tmplen = strlen ( namebuff );
	    while ( tmp < tmplen ) {
		if ( namebuff[tmp] == '~' ) {
		    namebuff[tmp] = '\0';
		    strcpy ( userName, namebuff );
		    tmpidx = tmp + 1;
		    chptr = (char *)(namebuff + tmp + 1);
		} else if ( (tmpidx != 0) && namebuff[tmp] == '\(' ) {
		    namebuff[tmp-1] = '\0';
		    strcpy ( userCompany, (char *)(namebuff + tmpidx) );
		    tmpidx = tmp + 1;
		    chptr = (char *)(namebuff + tmp + 1);
		} else if ( (chptr!=NULL) && namebuff[tmp] == ')' ) {
		    tmpidx = tmp + 1;
		    chptr = (char *)(namebuff + tmp + 1);
		}
		tmp++;
	    }
	    if ( tmpidx != 0 ) {
		strcpy ( userDate, chptr );
	    }
	    [personalName setStringValue: userName];
	    [personalCompany setStringValue: userCompany];
	    [personalDate setStringValue: userDate];
	    [personalName setEnabled: NO];
	    [personalCompany setEnabled: NO];
	    [personalDate setEnabled: NO];

	    [personalButton setEnabled:FALSE];
	    [quitButton setEnabled:FALSE];
	    [personalButton setTransparent:YES];
	    [quitButton setTransparent:YES];
	} else {
	    if ( readOnly ) {
		NXRunAlertPanel (
		    "Protection",
		    "Must be able to open the Application file for WRITING; %s",
			NULL, NULL, NULL,
		    "please give the program WRITE PERMISSION for you or other."
		);
		exit ( 1 );
	    }
	    /* set up modal session to watch for "Personalize" button */

	    // don't put the full name into the panel if the user account
	    // is "me", because it won't be helpful:
	    if ( strncmp ( pwd->pw_name, "me", 2 ) ) {
		[personalName setStringValue: pwd->pw_gecos];
	    }
	    [personalCompany setStringValue: ""];
	    [personalName setEnabled:YES];
	    [personalName setEditable:YES];
	    [myInfoPanel performClose:self];
	    [myInfoPanel makeFirstResponder:[myInfoPanel contentView]];
	    [myInfoPanel setWorksWhenModal: YES];
	    eventmask = [myInfoPanel addToEventMask:
		(NX_KEYDOWNMASK | NX_KEYUPMASK)
	    ];
	    [myInfoPanel makeKeyAndOrderFront: self];
	  retry:
	    [personalMatrix selectTextAt:0];
	    [NXApp beginModalSession:&session for:myInfoPanel];
	    for (;;) {
		button = [NXApp runModalSession:&session];
		if (button != NX_RUNCONTINUES)
		    break;
	    }
	    [NXApp endModalSession:&session];
	    if ( button != NX_RUNSTOPPED ) {
		exit ( 0 );
	    }
	    if ( [personalName stringValue] && strlen([personalName stringValue]) ) {
		strcpy ( userName, [personalName stringValue] );
	    } else {
		NXBeep();
		NXRunAlertPanel ( "Personalize",
		    "You must personalize with both Name and Company",
		    NULL, NULL, NULL
		);
		goto retry;
	    }
	    if (   [personalCompany stringValue]
		&& strlen([personalCompany stringValue])
	    ) {
		strcpy ( userCompany, [personalCompany stringValue] );
	    } else {
		NXBeep();
		NXRunAlertPanel ( "Personalize",
		    "You must personalize with both Name and Company",
		    NULL, NULL, NULL
		);
		goto retry;
	    }
	    [personalButton setEnabled:FALSE];
	    [quitButton setEnabled:FALSE];
	    [personalButton setTransparent:YES];
	    [quitButton setTransparent:YES];
	    [personalName setEnabled: NO];
	    [personalCompany setEnabled: NO];
	    [personalDate setEnabled: NO];
	    [myInfoPanel orderOut:self];
	    [myInfoPanel setEventMask: eventmask];
	    NXPing();
	    theTime = time(0);
	    date = localtime ( &theTime );
	    sprintf (
		entry, "%s (%s) %02d/%02d/%02d %02d:%02d\n",
		userID, pwd->pw_gecos,
		date->tm_mon + 1, date->tm_mday, date->tm_year,
		date->tm_hour, date->tm_min
	    );
	    [personalDate setStringValue:entry];
	    sprintf (
		entry, "%s~%s (%s) %s %02d/%02d/%02d %02d:%02d\n",
		userName, userCompany, pwd->pw_gecos, userID,
		date->tm_mon + 1, date->tm_mday, date->tm_year,
		date->tm_hour, date->tm_min
	    );
	    usercount++;
	    // if ( !bad ) {
		fseek ( fd, placecount, SEEK_SET );
		fprintf ( fd, "%3d", usercount );

		fseek ( fd, placename, SEEK_SET );
		fprintf ( fd, "%s", entry );
	    // }
	    NXPing();
	}
    } // if ch != EOF
    fclose ( fd );
    return self;
}

@end
