#! /bin/csh -fb
# PackageScript(.csh)
#
# This is the main script for starting packaged worlds.  Things like
# CommanderWorld and RawViewersWorld are front ends to this.  This parses
# the command line and then makes a local copy of the packaged world
# before executing it.
# Philip James, March 14, 1990 12:30:34 pm PST
# Willie-Sue Orr, April 29, 1992 12:57:41 pm PDT
# Brent Welch, June 28, 1990 1:31:43 pm PDT
# JKF, May 20, 1991 12:55:40 pm PDT
#
# Usage: PackageScript package←name options ...
#	Valid options are
#		-localdir localdir
#		-localdirENV environ←var
#		-remotedir remotedir
#		-remotedirENV environ←var
#		-nocopy
#		-cirioPort number
#		-vp num←vps
#		-stdiop num←iops
#		-thread num←threads
#		-mem stack←memory ??
#		-heap initial←heap←size
#		+Targs thread←arg←to←add
#		-Targs thread←arg←to←replace←defaults
#		+PCRargs pcr←arg←to←add
#		-PCRargs pcr←arg←to←replace←defaults
#		-rung~o  set switch to load unoptomized code
#
#	Obsolete options that are ignored are
#		-opt		(all packages are optimized now)
#		-noOpt		(ditto)
#		-nocirio	(cirio is in all packages now)
#		-plain		(I don't know what this meant)
#		-sv		(Everything uses SysV shared memory now)
#

#
# Sanity checks against the environment
#
if ( `/usr/ucb/printenv cedar10←1` == "" ) setenv cedar10←1 /project/cedar10.1/
if (! -d $cedar10←1) then
    echo 'Cannot find Cedar home ($cedar10←1): ' "$cedar10←1"
    exit 1
endif
if (! -d $cedar10←1/bin) then
    echo 'Cannot find Cedar binaries: ($cedar10←1/bin): ' "$cedar10←1/bin"
    exit 1
endif
if (! -d $cedar10←1/release) then
     echo 'Cannot find Cedar release ($cedar10←1/release): ' "$cedar10←1/release"
     exit 1
endif

#
# Set up default parameters: $1 is the package name
# $package←name is all lowercase.
# $installed←package is the latest version of the package from $remotedir
#

set local←package = $1
set initialCommand = $2
# setenv COMMANDER←INITIAL←COMMAND $2
set package←name = `/bin/echo $local←package | /bin/tr A-Z a-z`
shift argv
shift argv

set remotedir = ""
set mysteryArg = ""

if ( ! $?TMPDIR) setenv TMPDIR /tmp
if ( ! $?XR←HOME ) setenv XR←HOME /pseudo/xrhome
if ( ! $?XR←CONFIG ) setenv XR←CONFIG Threads
if ( ! $?XR←MACH ) setenv XR←MACH sparc
if ( ! $?XR←VERSION ) setenv XR←VERSION INSTALLED
if ( ! $?XR←XHOST ) setenv XR←XHOST `/bin/hostname`

set threads←args = ( -slaveiop 1 -slave 1 )
set num←vp = 2		# -vp default
set num←iop = 2		# -stdiop default
set num←threads = 80	# -thread default
set mem = 300000	# -mem default
set shmtype = sv	# -shmtype default

set pcr←args = ( -t )
set heap = 5000000	# -h default

set override←Targs = "no"
set override←PCRargs = "no"
set note = "no"

set tcopy = `/usr/ucb/printenv NOCOPY`
if ("$tcopy" != "") then
    set copy = "no"
else
    set copy = "yes"
endif

set alwayscopy = `/usr/ucb/printenv ALWAYSCOPY`
if ("$alwayscopy" != "") then
    set alwayscopy = "yes"
else
    set alwayscopy = "no"
endif

#
# Parse command line
#

top:
   if ( $#argv > 0 ) then
	switch ($argv[1])
		case -localdir:
		    shift argv
		    if (! -d $argv[1]) then
			/bin/echo "Bad localdir: $argv[1]"
			exit 1
		    endif
		    set localdir = $argv[1]
		    shift argv
		    goto top
		case -localdirENV:
		    shift argv
		    set DIR←VAR = $argv[1]
		    #
		    # One level of indirection to get the user's
		    # preferred local directory from the environment.
		    # The standard scripts use this method, even though
		    # the user may not define the environment variable.
		    #
		    set localdir = `/usr/ucb/printenv $DIR←VAR`
		    if ("$localdir" == "") then
			#
			# no pre-defined directory in the environment
			#
			unset localdir
			set note = "yes"
		    else
			if (! -d $localdir) then
			    /bin/echo "Bad $local←package directory from $DIR←VAR environment variable"
			    exit 1
			endif
		    endif
		    shift argv
		    goto top

		case -remotedir:
		    shift argv
		    if (! -d $argv[1]) then
			/bin/echo "Bad remotedir: $argv[1]"
			exit 1
		    endif
		    set remotedir = $argv[1]
		    shift argv
		    goto top
		case -remotedirENV:
		    shift argv
		    set DIR←VAR = $argv[1]
		    #
		    # One level of indirection to get the user's
		    # preferred remote directory from the environment.
		    # The standard scripts use this method, even though
		    # the user may not define the environment variable.
		    #
		    set remotedir = `/usr/ucb/printenv $DIR←VAR`
		    if ("$remotedir" != "") then
			if (! -d $localdir) then
			    /bin/echo "Bad $local←package directory from $DIR←VAR environment variable"
			    exit 1
			endif
		    endif
		    shift argv
		    goto top
		case -nocopy:
		    shift argv
		    set copy = "no"
		    goto top
		case -alwayscopy:
		    shift argv
		    set alwayscopy = "yes"
		    goto top
		case -cirioPort:
		    shift argv
		    setenv CirioPort $argv[1]
		    shift argv
		    goto top
		case -vp:
		    shift argv
		    set num←vp = ( $argv[1]:q )
		    shift argv
		    goto top
		case -stdiop:
		    shift argv
		    set num←iop = ( $argv[1]:q )
		    shift argv
		    goto top
		case -thread:
		    shift argv
		    set num←threads = ( $argv[1]:q )
		    shift argv
		    goto top
		case -mem:
		    shift argv
		    set mem = ( $argv[1]:q )
		    shift argv
		    goto top
		case -shmtype:
		    shift argv
		    set shmtype = ( $argv[1]:q )
		    shift argv
		    goto top
		case -heap:
		    shift argv
		    set heap = ( $argv[1]:q )
		    shift argv
		    goto top
		case -Targs:
		    shift argv
		    set threads←args = ( $argv[1]:q )
		    set override←Targs = "yes"
		    shift argv
		    goto top
		case +Targs:
		    shift argv
		    set threads←args = ( $threads←args:q $argv[1]:q )
		    shift argv
		    goto top
		case -PCRargs:
		    shift argv
		    set pcr←args = ( $argv[1]:q )
		    set override←PCRargs = "yes"
		    shift argv
		    goto top
		case +PCRargs:
		    shift argv
		    set pcr←args = ( $pcr←args:q $argv[1]:q )
		    shift argv
		    goto top
		case -rung~o:
		    shift argv
		    set initialCommand = ( "rung -~o; " $initialCommand )
		    goto top
    #
    # Old arguments
    #
		case -plain:
		    /bin/echo "-plain is obsolete"
		    shift argv
		    goto top
		case -opt:
		    /bin/echo "-opt is obsolete"
		    shift argv
		    goto top
		case -noOpt:
		    /bin/echo 'All packages are optimized.'
		    exit 1
		case -sv:
		    shift argv
		    /bin/echo "-sv option is obsolete"
		    goto top
		case -nocirio:
		    /bin/echo "packaged world without Cirio no longer exists."
		    shift argv
		    goto top
		case -makedo:
		    /bin/echo 'packaged world with language tools no longer exists.'
		    shift argv
		    goto top
		case -*:
		    /bin/echo "argument { $argv[1] } not understood"
		    exit 1
		default:
		    set mysteryArg = ( $argv[1]:q )
		    shift argv
		    goto top
		endsw
	endif

if ($override←Targs == "no") then
    set threads←args = ( $threads←args:q -vp $num←vp -stdiop $num←iop -thread $num←threads -mem $mem -shmtype $shmtype  )
endif
if ($override←PCRargs == "no") then
    set pcr←args = ( $pcr←args:q -h $heap  )
endif
if ($mysteryArg:q != "") then
    set initialCommand = ( $initialCommand:q \; $mysteryArg:q )
endif

# echo $initialCommand:q
setenv COMMANDER←INITIAL←COMMAND "$initialCommand:q"

#
# See if a remotedir has been set, or supply a default
#
if ($remotedir == "") then
    set remotedir = $cedar10←1/release/packagedworlds/sun4
endif

#
# Choose a local directory (/tmp or /usr/tmp) with lots of space.
#

if ($copy == "yes" && ! $?localdir) then
    #
    # Nothing specified explicitly.  Look around for an existing package,
    # otherwise choose a directory with lots of room.
    #
    if (-e /tmp/$local←package) then
	set localdir = /tmp
    else if (-e /usr/tmp/$local←package) then
	set localdir = /usr/tmp
    else
	set spacetmp = `/bin/df /tmp | /bin/sed '/Filesystem/d' | /bin/awk '{print $4}'`
	set spaceusr = `/bin/df /usr/tmp | /bin/sed '/Filesystem/d' | /bin/awk '{print $4}'`
	if ($spaceusr > $spacetmp) then
	    set localdir = "/usr/tmp"
	else
	    set localdir = "/tmp"
	endif
	if ("$note" == "yes") then
	    /bin/echo "Note: the $DIR←VAR env-var specifies a place for a copy of $local←package"
	    /bin/echo "Otherwise, either /tmp or /usr/tmp is used, depending on space available"
	endif
    endif
else if (! $?localdir) then
    set localdir	# so if test doesn't fail below.  copy == no here.
endif

#
# get the latest version of the package from remotedir
#
set installed←package = `/bin/ls -t $remotedir/$package←name.~*~ | /usr/ucb/head -1`

#
# Decide if we should copy the installed version to the local copy
# Need a CHANGED variable to distinquish between using no copy because
# unchanged and no copy because we're using the remote file.
#

set CHANGED = $copy	# yes if make local copy, no if use remote file

if ($copy == "yes" && -e $localdir/$local←package ) then
    set x1 = `/bin/ls -lL $installed←package | /bin/awk '{ print $4, $5, $6, $7 }'`
    set x2 = `/bin/ls -lL $localdir/$local←package | /bin/awk '{ print $4, $5, $6, $7 }'`
    if ( "$x1" == "$x2" ) set CHANGED = "no" 
endif
if ( $CHANGED == "yes" ) then
    if ( -e $localdir/$local←package) then
		if ( $alwayscopy == "yes" ) then
			set YES = "y"
		else
			/bin/echo "Your copy of $local←package is different than the installed one"
			/bin/echo -n "do you want to update ? "
			set YES = $<
		endif
    else
		set YES = "y"
    endif
    if ( $YES =~ y* ) then

    #
    # check disk space before copy
    #
	if ( -e $localdir/$local←package) rm -f $localdir/$local←package

	set size = `/bin/ls -lL $installed←package | /bin/awk '{printf "%d",  $4 / 1024}'`
	set space = `/bin/df $localdir | /bin/sed '/Filesystem/d' | /bin/awk '{print $4}'`
	/bin/echo "$local←package kbytes $size, $localdir free kbytes $space"
	if ($size > $space) then
	    /bin/echo "No space in $localdir for copy"
	    set copy = "no"
	else
	    /bin/echo "cp -p $installed←package $localdir/$local←package"
	    cp -p $installed←package $localdir/$local←package 
	    chmod +w $localdir/$local←package
	endif
    endif
endif

#
# Run the package
#

if ($copy == "yes") then
    set program = $localdir/$local←package
else
    set program = $installed←package
endif
/bin/echo "Running $program"

if ( $#argv > 0 ) then
    exec $program ${threads←args} -tmpdir $TMPDIR -- ${pcr←args} -@ $argv[1-] -@
else
    exec $program ${threads←args} -tmpdir $TMPDIR -- ${pcr←args}
endif

# eof