#!/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");
    $wm = 'mwm';
    &addvtwm2();
}
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");
	$wm = 'mwm';
    }
&addvtwm1();    
}

&putvtwmrcfilein();
&startvtwm();
exit(1);

sub addvtwm1{
    open(EN, ">>/mit/$whoami/.environment");
    print(EN "setenv WINDOW_MANAGER /afs/athena/project/windowmanagers/\${hosttype}bin/vtwm.gamma\n");
    
    close(EN);
}
sub addvtwm2{
    open(EN, ">/mit/$whoami/.environment");
    print(EN "setenv WINDOW_MANAGER /afs/athena/project/windowmanagers/\${hosttype}bin/vtwm.gamma\n");
    
    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{
    print("Trying to find $wm process to kill.\n");
    open(PSAUX, "/bin/ps auxwww|");
    while(<PSAUX>){
	split;
	if($_[$#_]=~/$wm/){
	    print("Found this: $_[$#_]\n");
	    $wmpid = $_[1];
	    print("Killing $_[1]\n");
	    `kill -9 $wmpid`;
	}
    }
    $h = `machtype`;
    chop($h);
    $h .= 'bin';
    print("Bindir: $h\n");
	$fpid = fork();
	if(!$fpid){
    `/afs/athena/project/windowmanagers/$h/vtwm.gamma &`;}
else{
	print("Running new windowmanager...\n");
	print("Use Ctrl-arrows to move around the desktop\n");
	exit(0);}
}

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);
}


}
