// LockFileTest.m -- test out the lock file class

#import <daymisckit/daymisckit.h>
#import <appkit/appkit.h>
#import <stdio.h>

void main()
{
	id lockFile1 = [[DAYLockFile alloc] init];
	id lockFile2 = [[DAYLockFile alloc] init];

	printf("Standard lock/unlock test...");
	[lockFile1 setFileName:[[DAYString alloc] initString:"lock1"]];
	if (![lockFile1 lock]) { printf("failed on lock.\n"); exit(1); }
	printf("\n");
	system("ls -alsFg lock1");
	system("cat lock1"); printf("\n");
	if (![lockFile1 haveLock]) { printf("haveLock is wrong.\n"); exit(1); }
	if (![lockFile1 unlock]) { printf("failed on unlock.\n"); exit(1); }
	system("ls -alsFg lock1");
	printf("passed.\n");

	printf("Attempt to obtain lock when someone else has it...");
	[lockFile2 setFileName:[[DAYString alloc] initString:"lock1"]];
	if (![lockFile1 lock]) { printf("failed on initial lock.\n"); exit(1); }
	if ([lockFile2 lock]) { printf("second client got lock.\n"); exit(1); }
	if (![lockFile1 haveLock]) { printf("haveLock #1 is wrong.\n"); exit(1); }
	if ([lockFile2 haveLock]) { printf("haveLock #2 is wrong.\n"); exit(1); }
	if (![lockFile1 unlock]) { printf("failed on unlock.\n"); exit(1); }
	if ([lockFile2 unlock]) { printf("lock #2 unlocked.\n"); exit(1); }
	printf("passed.\n");

	printf("Dual lock/unlock test...");
	[lockFile1 setFileName:[[DAYString alloc] initString:"lock1"]];
	[lockFile2 setFileName:[[DAYString alloc] initString:"lock2"]];
	if (![lockFile1 lock]) { printf("failed on lock #1.\n"); exit(1); }
	if (![lockFile2 lock]) { printf("failed on lock #2.\n"); exit(1); }
	printf("\n");
	system("ls -alsFg lock1 lock2");
	system("cat lock1"); printf("\n");
	system("cat lock2"); printf("\n");
	if (![lockFile1 haveLock]) { printf("haveLock #1 is wrong.\n"); exit(1); }
	if (![lockFile2 haveLock]) { printf("haveLock #2 is wrong.\n"); exit(1); }
	if (![lockFile1 unlock]) { printf("failed on unlock #1.\n"); exit(1); }
	if (![lockFile2 unlock]) { printf("failed on unlock #2.\n"); exit(1); }
	system("ls -alsFg lock1 lock2");
	printf("passed.\n");

	exit(0);
}
