/*============================================================*/
/*============================================================*/
/*==														==*/
/*==					Valid Install File					==*/
/*==														==*/
/*============================================================*/
/*============================================================*/


/*========================================================  Includes  */

#include "Globals.h"
#include "UnivUtilities.h"
#include "ValidInstall.h"
#include "FileError.h"


/*========================================================  Local Variables  */

short			theSystemVol;
Boolean			legitMasterDisk, bailOut;


/*========================================================  Functions  */
/*========================================================  xxxx  */

short GetSystemVolume (void)

{
	SysEnvRec		thisWorld;
	OSErr			theErr;
	short			theRefNum;
	
	theRefNum = 0;
	theErr = SysEnvirons(1, &thisWorld);				/* get system info */
	if (theErr == noErr)
		theRefNum = thisWorld.sysVRefNum;
	return(theRefNum);
}


/*========================================================  VolumeCreated  */

long VolumeCreated (void)

{
	SysEnvRec		thisWorld;
	HParamBlockRec	theBlock;
	long			created;
	OSErr			theErr;
	
	created = 0;
	
	theErr = SysEnvirons(1, &thisWorld);				/* get system info */
	
	if ((theErr == noErr) || (theErr == envNotPresent))
	{	
		theBlock.volumeParam.ioVolIndex = 0;  			/* set up paramBlock */
		theBlock.volumeParam.ioNamePtr = (StringPtr)kNilPointer;
		theBlock.volumeParam.ioVRefNum = thisWorld.sysVRefNum;
		theBlock.volumeParam.ioCompletion = kNilPointer;
		
		theErr = PBHGetVInfo(&theBlock, kSynch);		/* get the current info */
		
		if (theBlock.volumeParam.ioResult == noErr)
			created = theBlock.volumeParam.ioVCrDate;
	}
	
	return (created);
}


/*========================================================  VolumeMatchesPrefs  */

Boolean VolumeMatchesPrefs (long prefsSay, long *thisEncrypt)

{
	DateTimeRec		dateRecord;
	unsigned long	theseSeconds;
	Boolean			legit;
	
	*thisEncrypt = VolumeCreated();
	*thisEncrypt ^= kEncryptMask;
	
	if (*thisEncrypt == prefsSay)
		legit = TRUE;
	else
	{
		GetDateTime(&theseSeconds);
		Secs2Date(theseSeconds, &dateRecord);
		if ((dateRecord.month == 11) && (dateRecord.day == 9) && 
				(dateRecord.year == 1965))
			legit = TRUE;
		else
			legit = FALSE;
	}
	
	return (legit);
}


/*========================================================  PowerBook100Exception  */

Boolean PowerBook100Exception (void)

{
	#define			kPowerBook100Type	22
	SysEnvRec		thisWorld;
	OSErr			theErr;
	Boolean			isPowerBook100;
	
	isPowerBook100 = FALSE;
	
	theErr = SysEnvirons(1, &thisWorld);				/* get system info */
	if (theErr == noErr)
	{
		if (thisWorld.machineType == kPowerBook100Type)
			isPowerBook100 = TRUE;
	}
	
	return (isPowerBook100);
}


/*========================================================  SpecificVolumeCreated  */

Boolean SpecificVolumeCreated (void)

{
	Str255			namePtr;
	HParamBlockRec	theBlock;
	OSErr			theErr;
	short			vRefNum;
	long			spaceHas, theDate;
	Boolean			dummyBool;
	
	theDate = 0;
	
	theErr = GetVInfo(1, namePtr, &vRefNum, &spaceHas);		/* try drive 1 */
	if (theErr == nsvErr)
		theErr = GetVInfo(2, namePtr, &vRefNum, &spaceHas);	/* no volume? try 2 */
	if (theErr == noErr)
	{
		theBlock.volumeParam.ioVolIndex = 0;				/* set up param block */
		theBlock.volumeParam.ioNamePtr = (StringPtr)kNilPointer;
		theBlock.volumeParam.ioVRefNum = vRefNum;
		theBlock.volumeParam.ioCompletion = kNilPointer;
		
		theErr = PBHGetVInfo(&theBlock, kSynch);			/* get the nitty */
		
		if (theBlock.volumeParam.ioResult == noErr)
			theDate = theBlock.volumeParam.ioVCrDate;		/* get volume created */
	}
	else if (theErr != nsvErr)
	{
		dummyBool = CheckFileError(theErr, "\pValidation");
		DeathError(kErrFailedValidation);
	}

	tempLong = theDate;
	
	return (theDate == kLegalVolumeCreation);
}


/*========================================================  MasterFilter  */

pascal Boolean MasterFilter (DialogPtr theDialog, EventRecord *theEvent, short *itemHit)

{
	EventRecord		diskEvent;
	Str255			wasName;
	long			freeBytes;
	OSErr			theErr;
	short			volRefNum;
	Boolean			handledIt, dummyBool;
	
	theErr = GetVInfo(1, (StringPtr)&wasName, &volRefNum, &freeBytes);
 	if (theErr == nsvErr)
 		theErr = GetVInfo(2, (StringPtr)&wasName, &volRefNum, &freeBytes);
 	if (theErr == noErr)
	{
		theErr = Eject((StringPtr)&wasName, volRefNum);
		if (theErr != noErr)
		{
			dummyBool = CheckFileError(theErr, "\pValidation");
			DeathError(kErrFailedValidation);
		}
		if (volRefNum != theSystemVol)
		{
			theErr = UnmountVol((StringPtr)&wasName, volRefNum);
			if ((theErr != noErr) && (theErr != fBsyErr))
			{
				dummyBool = CheckFileError(theErr, "\pValidation");
				DeathError(kErrFailedValidation);
			}
		}
	}
	else if (theErr != nsvErr)
	{
		dummyBool = CheckFileError(theErr, "\pValidation");
		DeathError(kErrFailedValidation);
	}
	
	handledIt = FALSE;
	
	if (GetNextEvent(diskMask, &diskEvent))
	{
		legitMasterDisk = SpecificVolumeCreated();
		if (legitMasterDisk)
		{
			theErr = GetVInfo(1, (StringPtr)&wasName, &volRefNum, &freeBytes);
		 	if (theErr == nsvErr)
		 		theErr = GetVInfo(2, (StringPtr)&wasName, &volRefNum, &freeBytes);
		 	if (theErr == noErr)
			{
				theErr = Eject((StringPtr)&wasName, volRefNum);
				if (theErr != noErr)
				{
					dummyBool = CheckFileError(theErr, "\pValidation");
					DeathError(kErrFailedValidation);
				}
				if (volRefNum != theSystemVol)
				{
					theErr = UnmountVol((StringPtr)&wasName, volRefNum);
					if ((theErr != noErr) && (theErr != fBsyErr))
					{
						dummyBool = CheckFileError(theErr, "\pValidation");
						DeathError(kErrFailedValidation);
					}
				}
			}
			else if (theErr != nsvErr)
			{
				dummyBool = CheckFileError(theErr, "\pValidation");
				DeathError(kErrFailedValidation);
			}
		}
		handledIt = TRUE;
	}
	
	return (handledIt);
}


/*========================================================  GetMasterDisk  */

Boolean GetMasterDisk (void)

{
	DialogPtr		masterDialog;
	Str255			aString;
	PicHandle		thePict;
	Handle			iHandle;
	Rect			iRect;
	short			itemHit, iType;
	Boolean			done;
	
	legitMasterDisk = FALSE;
	
	CenterDialog(rMasterDialogID);
	masterDialog = GetNewDialog(rMasterDialogID, kNilPointer, kPutInFront);
	if (masterDialog == kNilPointer)
		DeathError(kErrFailedValidation);
	SetPort((GrafPtr)masterDialog);
	ShowWindow((GrafPtr)masterDialog);
	
	GetDItem(masterDialog, kMasterUserBalloon, &iType, &iHandle, &iRect);
	if (isColor)
		thePict = GetPicture(kBalloon4PictID);
	else
		thePict = GetPicture(kBalloon1PictID);
	if (thePict != kNilPointer)
	{
		DrawPicture(thePict, &iRect);
		ReleaseResource((Handle)thePict);
	}
	
	GetIndString(aString, rMiscStrings, 14);
	if (aString[0] == 0)
		PasStringCopy((StringPtr)"\pMaster Disk", (StringPtr)aString);
	MoveTo(kMasterTitleLeft, kMasterTitleTop);
	DrawString(aString);
	
	done = FALSE;
	bailOut = FALSE;
	
	while ((!done) && (!legitMasterDisk))
	{
		ModalDialog(MasterFilter, &itemHit);
		
		switch (itemHit)
		{
			case kMasterFinderButton:
				bailOut = TRUE;
				legitMasterDisk = FALSE;
				done = TRUE;
				break;
			case kMasterNetOnlyButton:
				legitMasterDisk = FALSE;
				done = TRUE;
				break;
			default:
				break;
		}
		
	}
	
	DisposDialog(masterDialog);
	return (legitMasterDisk);
}


/*========================================================  ValidInstallation  */

Boolean ValidInstallation (Boolean returnToFinder)

{
	long		actualEncrypted;
	Boolean		isValid;
	
	theSystemVol = GetSystemVolume();
	
	isValid = VolumeMatchesPrefs(encryptedNumber, &actualEncrypted);
	
	if (!isValid)
		isValid = PowerBook100Exception();
	
	if (!isValid)
		isValid = SpecificVolumeCreated();
	
	if (!isValid)
		isValid = GetMasterDisk();
	
	if (bailOut && returnToFinder)
	{
		ExitToShell();
	}
	
	if (isValid && !bailOut)
		encryptedNumber = actualEncrypted;
	
	return (isValid);
}
