: # Exec perl
eval 'exec perl -S $0 "$@"'
    if $running_under_some_shell;

# ----------------------------------------------------------------------
# This is the base startup script for SOFTWARE -- all versions, 
# all platforms. It sets some variables, figures out which version to 
# run, and invokes the startup script for that version.
#
# Variables Used Globally
#	$version		Software Version Number (e.g. 6.07)
#	@args			Command line args without athena additions
#	@not_available		List of Platforms on which SOFTWARE
#				doesn't run
#	$sys			(optional) value of $ATHENA_SYS
#	$swtoolsDir		pointer to swtools locker in AFS
#	$msg_dir
# ----------------------------------------------------------------------

$software = "java";
@not_available = ("");
$base_locker = "/mit/${software}";
&initialize ($base_locker);
#--------------------------------------------------------------------
# version skew for IRIX 5.3: last release that works is 1.1.3
if ($sys eq "sgi_53")
{
$version = "1.1.3";
}
else
{
$version = "1.1.6";
}
#---------------------------------------------------------------------


&process_local_args;			# set @args, possibly $version

$sw_version = "${software}_v${version}";
$version_locker = "/mit/${sw_version}";

#$cookie = "JDK1.1.3_080697";
#$message = "$msg_dir/JDK113";
#&send_message ($cookie, $message);

#$cookie = "JDK1.1.5_031698";
#$message = "$msg_dir/JDK115";
#&send_message ($cookie, $message);

$cookie = "JDK1.1.6_081798";
#$message = "$msg_dir/JDK116";
&delete_cookie ($cookie);
sleep(2);
$cookie = "JDK1.2ref_120898";
$message = "$msg_dir/JDK120";
&send_message ($cookie, $message);

&exec_version ($version_locker, $sw_version, $version, $software);

# ----------------------------------------------------------------------
# initialize 
# 
# Use this routine do any sort of initialization.  Code is included
# for setting a reasonable value for $ATHENA_SYS if that is needed.
#
# Globals Set
#	$sys		- ATHENA_SYS environment variable
#	$msg_dir	- Location of magic cookie files
#	$swtoolsDir	- Location of the swtools locker
#	$redirection	- Controls visibility of stdout from attach
#
# ----------------------------------------------------------------------
sub initialize
{

    local ($base) = @_;

    # Uncomment this if you need to use $ATHENA_SYS

    if (! defined ($sys = $ENV{"ATHENA_SYS"})) {
	open (MACH, "/srvd/bin/athena/machtype -S|");
	chop ($sys = <MACH>);
	if (length ($sys) == 0) {
	    $sys = "\@sys";
	}
	close MACH;
    }

    $redirection = "> /dev/null";
    # For messages launched from base locker
    $msg_dir = "${base}/Athena/messages";
    $swtoolsDir="/afs/athena/software/swtools";

}

# ----------------------------------------------------------------------
# process_local_args 
#
#	 Process the command line arguments.  We do this to search
#	 for Athena-specific options, currently limited to a version
#	 specification.
#
# Globals Referenced
#	@ARGV
#
# Globals Set
#	@args	 - Command line arguments without athena-specifc ones
#
# ----------------------------------------------------------------------
sub process_local_args
{
    local ($arg);

    while ($arg = shift @ARGV) {
      ARGS: 
	{
# change to not match on -verbose, which is a javac option alexp 9/5/97
# further change to only match on -ver to avoid capture of -verify etc. 
# alexp 2/24/98
	    if ($arg =~ /^-ver$/) {
		$version = shift (@ARGV);
		last ARGS;
	    }
	    if ($arg eq "-show_stdout") {
		$redirection = "";
		last ARGS;
	    }
	    push (@args, $arg);
	}
    }
	
}

# ----------------------------------------------------------------------
# exec_version
#
#	This routine attaches the version-specific locker and execs
#	that startup script.
#
# ----------------------------------------------------------------------
sub exec_version
{
    local ($version_locker, $sw_version, 
	   $version, $software) = @_;
    local ($pos, $program, $bindir);

    $pos = rindex ($0, "/") + 1;
    $program = substr ($0, $pos);

    system ("attach $sw_version") if ! (-e "$version_locker");
    if (! -d "$version_locker") {
	print <<EOM1

The attach command failed for the locker $sw_version which should
contain $software version $version.  

Is there a problem with the locker, a general AFS problem, or did you
specify a non-existent version?  If you suspect a version problem, try
running $program again without any arguments to get the default
version of $software for this platform.  If you still have problems,
please report this to the OLC consultants or send a bug report using
sendbug.

EOM1
    ;
	    exit 0;
    }

    open (BINDIR, "/usr/athena/bin/athdir -p $version_locker -t bin |") ||
	die "Cannot run athdir command $!\n";
    chop ($bindir = <BINDIR>);
    close BINDIR;
    $executable = "$bindir/$program";
    if ( -e $executable) {
	exec ($executable, @args);
	# Should not get here
	exit 0;
    }
    else {
	print <<EOM2

There doesn\'t appear to be an executable called $program for $software
version $version on this platform.  If you think that this program should
run, try attaching the version-specific locker explicitly and then
restart $program.  To do this type

    add -f $sw_version
    $program

If that still fails, either Athena does not support $program on this
platform, there is a file server failure, or a problam with the local
workstation.  You may want to contact OLC to find out more information.

EOM2
    ;
	exit 0;
    }				#

}

# ----------------------------------------------------------------------
# send_message
#
#	Use this routine to direct messages to users
#
# Parameters
#	$cookie		  - magic cookie
#	$msg		  - message file (full path)
#
# Globals Referenced
#	$swtoolsDir	  - swtools locker
#
# ----------------------------------------------------------------------
sub send_message
{
    local ($cookie, $msg) = @_;
    local ($pid);

    $send_message = "$swtoolsDir/bin/send_message";
    if ( -x $send_message ) {
      FORK: {
	  if ($pid = fork) {
	      # parent continues
	  }
	  elsif (defined $pid) {
	      exec ("$send_message $cookie $msg");
	      # Should not get here
	      exit 0;
	  }
	  elsif ($! =~ /No more proces/) {
	      sleep 5;
	      redo FORK;
	  }
	  else {
	      # weird fork error - just continue
	  }
      }
    }
    else {
	# Cannot access program for some reason - just continue
    }
}

# ----------------------------------------------------------------------
# delete_cookie
#
#	Use this routine to delete a magic cookie from user's dotfile.
#
# Parameters
#	$cookie		  - magic cookie
#
# Globals Referenced
#	$swtoolsDir	  - swtools locker
#
# ----------------------------------------------------------------------
sub delete_cookie
{
    local ($cookie) = @_;
    local ($pid);

    $delete_cookie_prog = "$swtoolsDir/bin/delete_cookie";
  FORK: {
      if ($pid = fork) {
	  # parent continues
      }
      elsif (defined $pid) {
	  exec ("$delete_cookie_prog $cookie");
	  # Should not get here
	  exit 0;
      }
      elsif ($! =~ /No more proces/) {
	  sleep 5;
	  redo FORK;
      }
      else {
	  # weird fork error - just continue
      }
  }
}

