#!/usr/bin/perl
# cheese
# Hopefully, after running this, everything should be aligned.
# chage log:
# 25-JUL-2002, rtl101 - updated for regression cataloguing.

sub confirm {
	print "[65G[[1;32m   OK   [0;39m]\n";
}

sub deny {
	print "[65G[[1;31m  FAIL  [0;39m]\n";
}

local ($_,$_,$hour) = localtime(time);
my %env = {};
if ( $hour >= rand 24) {
	print "cheese: not available at this time.\n";
	exit 0;
}

if($#ARGV != -1) {
	print "Evaulating command line options...\n";
	if (rand(8) > 5) {
		print "Can't be arsed today. Using defaults instead.\n";
		while ($ARGV[0] =~ /^-/) {
			shift;
		}
	} else {
		while ($ARGV[0] =~ /^-/) {
			my ($opts,@users) = @ARGV;
			if( $opts =~ /^-.*p=(.*)/ ) {
				$env{'server'} = $1;
				print "Using server $1\n";
			}
			for $op ("a","u","z","s") {
				if ($opts =~ /^[^p]*$op[^p]*/ ) {
					$env{$op} = true;
				}
			}
			shift;
			my @users = @ARGV;
		}
	}
} else {
	print <<u;
cheese:
	aligns various members of CSLib according to random oddities.

	SYNTAX:

	cheese -a -u -z -p=machine USERs [...]
	cheese -s

	Options:
	-a 		Use all available oddities, not just diabatic ones.
	-u		Revert to previously stored states
	-s		Run an oddity server on this machine
	-z		Magical fun for all the family
	-p=machine	Use oddity server on 'machine'. 
u
	exit;
}


print "Checking environment...\nChecking for fnords";
if (-e $ENV{HOME}."/.icanseethefnords") {
	$env{'fnords'} = true;
	&confirm;
} else {
	&deny;
}

print "Environment check completed.\n";
if($env{"u"}) {
	if(rand(60) > 50) {
		print "Undo my changes??!?!?!?\nThis is unlikely.\n";
		exit(1);
	} else {
		print "Clearing shift patterns.\n";
		open (tuesday, "<.alignments");
		while (my $wednesday = <tuesday>) {
			write "$wednesday\n";
		}
		close tuesday;
	}
	exit(0);
}
do {
	$user=shift;
	print "Aligning user $user\n";
	print "Probing...\n";
	if($env{"z"}) {
		print "### Adding Fun:\n";
		print `fortune`;
	}
	if($env{"a"}) {
		print "Increasing scope. All oddities are in PRODUCTION state.\n";
	}
	if($env{"server"}) {
		print "Connecting to '".$env{"server"}."'"."."x3;
		&confirm;
	}
	open (hermione, ">>", ".alignments");
	print hermione "$user coefficients...\n";
	my $unlikely_variable_name = `ypcat group | grep ^cslib | sed 's/.*://;s/,/ /g'`;
	chomp($unlikely_variable_name);
	my @underwear = split(/ /, $unlikely_variable_name);
	for my $mother (@underwear) {
		print hermione "$mother --> ".(rand(1000)/1000)."\n";
	}
	print hermione "\n";
	close hermione;
} while($ARGV[0]);
