#!/usr/bin/perl -w
#
# One-time halfd upgrade script 
#
# Change History:
#
#	rabbott 2001-05-10	- Creation date
#

######################################################################
# setDefaults - set default/global variables
#
sub setDefaults {
	# Files that install in HLDIR
	@oldhldirfiles = ( "hlds_ld.txt", "hlds_ld_client.cfg", "hlds_ld_cluster.cfg" );

	@newhldirfiles = ( "halfd.txt", "halfd_client.cfg", "halfd_cluster.cfg" );

	# Files that install in HLDIR/mod
	@oldmodfiles   = (	"hlds_ld.cfg", 
						"hlds_ld.admin",
						"hlds_ld.badwords",
						"hlds_ld.exclude",
						"hlds_ld.foul",
						"hlds_ld.ping",
						"hlds_ld.tk",
						"hlds_ld.teammap",
						"hlds_ld.weapon" );

	@newmodfiles   = (	"halfd.cfg", 
						"halfd.admin",
						"halfd.badwords",
						"halfd.exclude",
						"halfd.foul",
						"halfd.ping",
						"halfd.tk",
						"halfd.teammap",
						"halfd.weapon" );
}

sub findMods {
	local( $entry, @allfiles, $mymods, $path );
	$mymods = "";

	opendir DIRHANDLE, $hldir or die "Couldn't open $hldir: $!\n";

	@allfiles = readdir DIRHANDLE;
	close DIRHANDLE;

	$didit = 0;
	foreach $entry ( @allfiles ) {
		next if( $entry eq "." );
		next if( $entry eq ".." );
		if( -d $entry ) {
			$path = $hldir . "/" . $entry . "/" . "hlds_ld.cfg";
			if( -e $path ) {
				if( !$didit ) {
					$mymods = $mymods . $entry;
				} else {
					$mymods = $mymods . " $entry";
				}
				$didit++;
			}
		}
	}

	return $mymods;
}

sub getHldir {
	local( @list ) = keys( %ENV );
	if( ! grep { /HLDIR/ } @list ) {
		return "";
	} else {
		return $ENV{HLDIR};
	}
}

sub copyFile {
	local( $oldfile, $newfile, $dir ) = @_;
	local( $oldpath, $newpath );

	$oldpath = "$dir/$oldfile";
	$newpath = "$dir/$newfile";

	if( -e $oldpath ) {
		&checkPerms( $newpath ) if( -e $newpath );

		print "COPYING $oldfile to $newfile in $dir\n";
		system "cp $oldpath $newpath" || die( "Copy failed: $!\n" );
	}
}

sub checkDir {
	local( $desc, $dir ) = @_;
	local( $first_byte );

	$first_byte = substr( $dir, 0, 1 );

	if( "$first_byte" ne "/" ) {
		print "\n\n$desc must NOT be a relative pathname!\n";
		print "Please enter another value.  It MUST start with a slash '/'.\n";
		return -1;
	} else {
		return 0;
	}
}

sub checkPerms {
	local( $target ) = @_;

	if( -w $target ) {
		# we're ok
	} else {
		print "\n\n$target isn't writable!\n";
		print "\n\nPlease fix your permissions or install as another user.\n";
		exit( 1 );
	}
		
}


sub main {
	local( $tmp );

	&setDefaults;
	select STDOUT;
	$| = 1;

	print "\n\n";
	print "This script will copy existing hlds_ld files to halfd files.\n";
	print "You should only need to run this once, and *before* you install halfd.\n";

	print "All defaults are enclosed in [square-brackets].\n";

	&checkPerms( "/tmp" );

	print "\nHave you stopped all hlds_ld processes? (Y/N) [n] ";
	chop( $ans = <STDIN> );
	die "\nPlease stop all hlds_ld processes before install!\n" . 
		"You can leave hlds_run running if it was started by hlds_ld.\n" 
		if ( ! grep { /^y/i } $ans );

	# Loop until they enter a valid directory
	while( 1 ) {
		print "\n\nEnter the directory where you have 'hlds_run' installed:\n";
		print "(note that this is your HLDIR)\n";
		$hldir = getHldir;
		print "[$hldir] ";
		chop( $myhldir = <STDIN> );
		$hldir = $myhldir if( ! $myhldir eq "" );

		$tmp = $hldir . "/hlds_run";
		if( ! -x $tmp ) {
			print "\nI couldn't find an executable hlds_run in '$hldir'!\n";
			next;
		}

		next if( &checkDir( "HLDIR", $hldir ) < 0 );
		last;
	}
	&checkPerms( $hldir );

	$mods = findMods();

	print "\n\n";
	print "I found the following MODs with hlds_ld files in their directories.\n";
	print "You can change this list if you like.  Enter \"none\" for no mods.\n";
	print "[$mods] ";
	chop( $mymods = <STDIN> );
	$mods = $mymods if( ! $mymods eq "" );

	print "\n\nParameters:\n";
	print "HLDIR      = '$hldir'\n";
	print "MODs       = '$mods'\n";

	print "\nOK to continue? (Y/N) [y]: ";
	chop( $ans = <STDIN> );
	die if ( $ans=~ /^n/i );

	print "\n";

	# Copy files in HLDIR
	print "Processing files in HLDIR:\n";
	$i = 0;
	foreach $file ( @oldhldirfiles ) {
		$newfile = $newhldirfiles[$i];
		&copyFile( $file, $newfile, $hldir );
		$i++;
	}

	if( "$mods" ne "none" ) {
		print "\n\n";
		# Install mod-specific files
		foreach $mod ( split( / /, $mods )) {
			print "\n\nProcessing files in HLDIR/$mod:\n";
			$moddir = "$hldir/$mod";
			if( -d $moddir ) {
				$i = 0;
				foreach $oldfile ( @oldmodfiles ) {
					$newfile = $newmodfiles[$i];
					&copyFile( $oldfile, $newfile, $moddir );
					$i++;
				}
			} else {
				print "Skipping $mod, not found...\n";
			}
		}
	}

	print "\n\nDone.  You should now run the halfd install script with ";
	print "'perl ./install'\n";

	exit( 0 );
}

&main;

