#!/bin/csh -f

# shell script to serve as {new/old/}scribe on both scribe servers
# and clients.  Determines where and how to run which version of scribe.

set prog = `/usr/bin/basename $0`
set domain = `domainname`
set host = `hostname | sed ' s/\..*//'`
set default = /usr/local/bin/scribe.7.1750
set newscribe = /usr/local/bin/scribe.7.1750
set oldscribe = /usr/local/bin/scribe.7

if ($?SCRIBE_REMOTE) then
    set remote = $SCRIBE_REMOTE
else if (-e $default && -e /usr/lib/scribe.sit) then
    set remote = $host
else
    switch ($domain)
	case ringmaster:
	    set remote = porthos
	    breaksw
	case caiphill:
	    set remote = aramis
	    breaksw
	case lcsr:
#	    set remote = klinzhai
	    set remote = farside
	    breaksw
	case lcsr-grad:
	    set remote = paul
	    breaksw
	case dcs:
	    set remote = remus
	    breaksw
#	case clowns:
#	    set remote = pyrite
#	    breaksw
# xfer above to research:
	case research:
	    set remote = research
	    breaksw
	case brouhaha:
	    set remote = hardees
	    breaksw
	case ruccs.rutgers.edu:
	    set remote = ruccs
	    breaksw
    endsw
else
    echo "  There is a problem with scribe on this system.  The scribe"
    echo "  binary does not exist here, and I do not know where to run"
    echo "  it remotely.  Please contact your system administrator."
    #
    #    who should arrange things with watrous@aramis...
    #
    exit 1
endif

switch ($1)
    case "-directory*":
#
# this is a remote execution.  (first arg specified working directory.)
# command should be niced.
#
	set dir = `echo $1 | sed 's;.*:;;'`
	if (-d $dir) then
	    cd $dir
	    shift
	else
            echo $dir is does not exist on $host.
            echo Please use a structure which does, or consult
	    exec echo your system administrator.
	endif
	set nice = "nice -5"
	breaksw
    default:
#
# this is a local execution.  use normal priority.
#
    set nice = ""
endsw
    
if ($host != $remote) then
    #
    # we are not on scribe server.  make up and exec rsh command.
    #
    if ("$*" != "") then
       set filespec = $1
       shift
    echo " [ remotely executing $prog on $remote - please wait ] "
       exec rsh $remote -n /usr/local/bin/${prog} -directory:`pwd | sed  's;^/a/;/;'` $filespec "$*"
    else
       echo You are running a shell script here which is running the
       echo actual ${prog} job on $remote.  You might observe some odd
       echo behavior \(such as prompts not appearing before input is
       echo expected and recognition not working\).  The more
       echo conventional way to run this shell script is
       echo \ \"$prog filename \[-switches\]\"
       echo ""
       exec rsh $remote /usr/local/bin/${prog} -directory:`pwd | sed  's;^/a/;/;'`
    endif
endif

#
# we are on scribe server.
#

#
# figure out which scribe is which
#

switch ($prog)
    case newscribe:
        if (-e $newscribe) then
            set scribe = $newscribe
        else
            set scribe = $default
        endif
        breaksw
    case oldscribe:
        if (-e $oldscribe) then
            set scribe = $oldscribe
        else
            set scribe = $default
        endif
        breaksw
    default:
        set scribe = $default
        breaksw
endsw

#
# ok, all set.  now do it.
#

exec $nice $scribe "$*"
