#!/usr/bin/env perl

# ----------------------------------------------------------------------
# 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}";
# added by alexp 4/12/06 written by zacheiss
# to not wait for message send on backgrounded launch when zephyr not used
$SIG{'TTOU'} = 'IGNORE';
$SIG{'TTIN'} = 'IGNORE';
&initialize ($base_locker);

if (-s "/etc/athena/reactivate") {
  $isathena = "1"; } else {
  $isathena = "0"; 
  $vermsg=`fs sysname`;
  chomp($vermsg);
  $q1=index($vermsg, "'");
  $q2=index($vermsg,"'",$q1+1);
  $athenasys=substr($vermsg,$q1+1,$q2-$q1-1);
if (! defined ($ENV{"ATHENA_SYS"})) {
  $ENV{"ATHENA_SYS"} = $athenasys;
}
  $ENV{"PATH"} = "${base_locker}" . "/arch/" . $ENV{"ATHENA_SYS"} . "/bin:" . $ENV{"PATH"};
}

$version = "1.8.0_172";

&process_local_args;			# set @args, possibly $version

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

#$cookie = "JDK1.6.0_13def_040109";
#&delete_cookie ($cookie);
#sleep(2);

#$cookie = "JDK1.6.0_16def_081809";
#$message = "$msg_dir/JDK160_16def";
#&delete_cookie ($cookie);
#sleep(1);

&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, "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, $verflag);
# alexp 9/19/05 fixed to not stop prematurely if a command-line arg is ""
    $verflag = 0;
    foreach $argnum (0 .. $#ARGV) {
      ARGS: 
	{
        $arg = $ARGV[$argnum];
# 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 = $ARGV[$argnum+1];
                $verflag = 1;
		last ARGS;
	    }
	    if ($arg eq "-show_stdout") {
		$redirection = "";
		last ARGS;
	    }
            if ($verflag)
            {
                $verflag = 0;
                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);

  if ($isathena) {

    system ("attach -q $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;
    }
                } else #not Athena
  {
      if (! -x "/usr/bin/afslocker") {
  print <<EOM3
You appear to be running the OpenAFS client on a non-Athena machine,
but the script /usr/bin/afslocker was not found or is not executable. 
This is required in order to run software from this locker.

EOM3
;
  exit 0; }
  system("afslocker $sw_version") if ! (-e "$version_locker");
  if (! -d "$version_locker") {
  print <<EOM2

You appear to be running the OpenAFS client on a non-Athena machine.
The program that emulates the Athena attach command for locker
$sw_version has failed for some reason and your application cannot run.

EOM2
    ;
exit 0; } 
         }
    open (BINDIR, "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);
# added by alexp 4/12/06 written by zacheiss
    $send_message = "$swtoolsDir/bin/send_message_nb";
    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
      }
  }
}
