: # Exec perl
eval 'exec perl -S $0 "$@"'
    if $running_under_some_shell;
#
#
# this script runs Splus by figuring out which specific version locker to
# use. It then attaches the appropriate locker if necessary, then invokes 
# a version-specific script with the same name, which finally invokes the
# binary. 
#
#
# Start of Main program.  This program sets up the environment for 
# running Splus 3.3 on Athena.  It is used by all platforms.
# (Sun, SGI, Linux)
#
$software = "splus";
# set the default version
$version = "5.1";
&do_args;                       # set $sw_version, $version, @args
#
# Standard things for sending messages
#
$swtoolsDir="/afs/athena/software/swtools";
$msg_dir = "/mit/${software}/Athena/messages";

$cookie = "splus5_avail_102798";
&delete_cookie ($cookie);
system ("sleep 2");

$cookie = "splus-lnx-avail_22599";
&delete_cookie ($cookie);
system ("sleep 2");

$cookie = "splus_5.1_avail_0799";
$message = "${msg_dir}/Splus_5.1_av";
&send_message($cookie, $message);

system ("sleep 2");

$version_locker = "/mit/${sw_version}";
$program = $software;

if (! -d "$version_locker") {
    system ("attach $sw_version");
    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);
}
else {
    print <<EOM2

There doesn\'t appear to be an executable called $program for $software
version $version on this platform.  If you think there should be, 
please report this to the OLC consultants or send in a bug report 
using the sendbug command.

EOM2
;
    exit 0;
}

# -----------------------------------------------------------------------------

# do_args - Process the command line arguments.  We do this to search
#           for Athena-specific options, currently limited to a version
#           specification.
#
sub do_args
{
    while (@ARGV) {
      ARGS: {
          $_ = shift (@ARGV);
          if ( /-v/ ) {
              $version = shift (@ARGV);
              last ARGS;
          }
          push (@args, $_);
      }
    }
    $sw_version = "${software}_v${version}";

}

# ----------------------------------------------------------------------
# send_message
#
# Use this routine to direct messages to users
#
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
    }
}

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


















