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


# ----------------------------------------------------------------------
# This is the base startup script for framemaker -- 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 frame 
#				doesn't run
#	$sys			(optional) value of $ATHENA_SYS
#	$swtoolsDir		pointer to swtools locker in AFS
#	$msg_dir
# ----------------------------------------------------------------------

$software = "frame";
@not_available = ("");
$base_locker = "/mit/${software}";
$version = "5.5.6";
&initialize ($base_locker);
&process_local_args;			# set @args, possibly $version

$cookie = "frame_v556_default";
&delete_cookie ($cookie);
sleep(2);

$cookie = "frame_v556_def-now";
$message = "$msg_dir/upgradev556_defnow";

&send_message ($cookie, $message);

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

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

# ----------------------------------------------------------------------
# initialize 
#
# Globals Set
#	$sys	 	- ATHENA_SYS environment variable
#	$msg_dir	- Location of magic cookie files
#	$redirection	- Controls visibility of stdout from attach
#
# ----------------------------------------------------------------------
sub initialize
{

    local ($base) = @_;

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

    if (($0 =~ /maker/) || ($0 eq "fmbatch") || ($0 eq "fmprint")) {
	$redirection = "";
    }
    else {
	$redirection = "> /dev/null";
    }

    # For messages launched from base locker
    $msg_dir = "${base_locker}/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
#	$redirection - Controls visibility of stdout from attach
#
# ----------------------------------------------------------------------
sub process_local_args
{
    local ($arg);
    local ($cookie, $message);

    while ($arg = shift @ARGV) {
      ARGS: 
	{
	    if ($arg =~ /^-ver[ s|si|sio|sion]?/) {
		$version = shift (@ARGV);
		next ARGS;
	    }
	    if ($arg eq "-show_stdout") {
		$redirection = "";
		next 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 $redirection") 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

    attach $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) = @_;

    $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";
    if ( -x $delete_cookie_prog ) {
      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
	  }
      }
    }
}

