#!/usr/athena/bin/perl

require '/afs/athena/user/m/k/mkgray/perl/io.pl';

print(`clear`);
print("Welcome to the automatic vtwm.gamma window manager installation\n");
print("program.\n");

$whoami = `/usr/ucb/whoami`;
chop($whoami);
$hess = `hesinfo $whoami passwd`;
@foo = split(/:/, $hess);
@bar = split(/,/, $foo[4]);
@foo = split(/ /, $bar[0]);
$name = $foo[0];
print("\n\nHello $name.  Before we actually install vtwm.gamma, let's\n");
print("check a few things to make sure this runs smoothly.\n");

print("\n\nChecking current windowmanager...\n");
if(!(-e "/mit/$whoami/.environment")){
	print("That was easy, you don't even have a .environment file.\n");
	print("That, sadly enough, means you are running mwm.  Poor soul.\n");
	print("But, you are on the road to salvation...\n");
}
else{
$wm = `grep WINDOW_MANAGER /mit/$whoami/.environment`;
chop($wm);
if($wm) {
	print("Hmm, you seem to have an explicitly set windowmanager...\n");
	@foo = split(/ /, $wm);
	@bar = split(/\//, $foo[$#foo]);
	$wm = $bar[$#bar];
	print("and that would appear to be $wm\n");
	
	if($wm eq 'vtwm.gamma'){
		print("\n\nSilly person... Why are you running this?  You've\n");
		print("already got vtwm.  Bye.\n");
		exit(1);
	}
	elsif($wm eq 'vtwm'){
		print("Oops, you're running vtwm, but not the gamma version.\n");
		print("Now, what's the difference you may ask.  Well, vtwm.gamma\n");
		print("has some bugs, but vtwm has LOTS of bugs.\n");
		print("We'll fix this for you.\n");
	}
	elsif($wm eq 'mwm'){
		print("Oh by Athena, why have you explicitly set your\n");
		print("window manager to mwm.  Afraid it will get worse?\n");
		print("We'll fix this right away.\n");
	}
	elsif($wm eq 'twm'){
		print("Ok, twm is good, but naturally, vtwm.gamma is better.\n");
		print("This should be an easy transition.\n");
	}
	elsif($wm eq 'gwm'){
		print("Why are you switching from gwm?  Have you\n");
		print("forgotten WOOL?  If you want, I guess...\n");
	}
	else{
		print("I'm not sure I'm familiar with the window-manager\n");
		print("so, I guess we'll let you change.\n");
}
&ditch();
}
else {
	print("Well, it looks like you're using the default, mwm.  Yech.\n");
	print("Thank goodness you're running this script.\n");
}

}
&addvtwm();
&putvtwmrcfilein();
&startvtwm();

sub addvtwm{
	open(EN ">>/mit/$whoami/.environment/");
	print(EN "setenv WINDOW_MANAGER /afs/athena/project/windowmanagers/${hosttype}bin/vtwm.gama");

	close(EN);
}

sub putvtwmrcfilein{
if(`cp /afs/athena/contrib/dotfiles/Vtwm/basic.vtwmrc /mit/$whoami/.vtwmrc`){
	print("Oh oh.  Something screwed up..\n");
	exit(5);
}

}

sub startvtwm{
	open(PSAUX "ps auxwww");
	while(<PSAUX>){
		split;
		if($_[10]=~/$wm/){
			$wmpid = $_[1];
			`kill -9 $wmpic`;
}
}
`/mit/windowmanagers/@sys/vtwm.gamma`;
}

sub ditch{
	print("Are you sure you want to ditch your current window manager\n");
	print("and switch to vtwm.gamma?  (personally, I'd say yes)\n");
	$yesorno = &io'input();
	if($yesorno !~ /y/){
		print("Fine, if you don't want to switch, that's ok. :-)\n");
		exit(1);
}
	print("Fixing your .environment file\n");
	open(EN, "/mit/$whoami/.environment");
	while(<EN>){
		if(!/WINDOW_MANAGER/){
		$file .= $_;}
	}
	if(!'rm /mit/$whoami/.environment'){
		open(OEN, ">/mit/$whoami/.environment");
		print(OEN $file);
		close(OEN);
	}
	else{
		print("Error in erasing old .environment.\n");
		exit(1);
}


}
