# Initial dptcl script which is run from inetd entry.
# Sets up environment for executing runMP tcl script.
# $Log: not supported by cvs2svn $
# Revision 1.20  2005/12/09 19:41:58  soliday
# Removed redefinition of the environment variable HOME. This was causing
# problems when running the machine procedures though an ssh session. The
# original reason for redefining HOME is no longer an issue.
#
# Revision 1.19  2005/08/29 20:54:18  soliday
# Removed obsolete path settings for very old versions of Solaris.
#
# Revision 1.18  2004/04/28 15:40:01  soliday
# Replaced dp_RPC calls with comm calls because comm is still supported by the
# Tcl community.
#
# Revision 1.17  2003/10/27 16:31:01  soliday
# Parallel processes now log the correct stack.
#
# Revision 1.16  2003/09/23 18:34:52  soliday
# Updated the LD_LIBRARY_PATH to include /usr/local/epics/extensions/lib/solaris
#
# Revision 1.15  2003/09/03 19:47:29  soliday
# Updated the LD_LIBRARY_PATH.
#
# Revision 1.14  1998/04/22 18:04:14  borland
# Now works with tcl 8.0 and dp 4.0.
# Also, will now find interpreter and runMP in oag patch area.
#
# Revision 1.13  1996/12/16 16:38:33  saunders
# Fixed race condition in abort sequence. Added info exists test in
# MpAbort for childPortToSd, since normal exit of child may unset this,
# but leave apsMpParallelList populated. Also added a debugging switch
# for future diagnosis work.
#
# Revision 1.12  1996/11/22 22:37:56  saunders
# PATH and LD_LIBRARY_PATH updated to match solaris 5.5.1.
#
# Revision 1.11  1996/10/17 21:21:01  saunders
# Numerous changes made to implement logDaemon logging of machine
# procedure execution Start,Finish,Error,Abort. Currently logs all
# pem and parallel procedures. More work needed to selectively log
# only call level 2 procedures (ie. only highest level).
#
# Revision 1.10  1996/10/02 13:55:00  saunders
# Added defn of $HOME, since Solaris 5.5.1 inetd doesn't initialize
# this.
#
# Revision 1.9  1996/10/01 15:23:41  saunders
# Mistyped Log entry, so cvs log info wasn't being added here.
#
#
set runMPservDebug 0

#
# Set up path and other env vars to duplicate standard Cshrc.aps
# style setup.
#

if {$runMPservDebug} {
    set fid [open "/tmp/runMPserv.log" w+]
}

proc logText {message} {
    global fid runMPservDebug
    if {$runMPservDebug} {
        puts $fid "$message"
    }
}
logText "runMPserv started."

#set env(HOST_ARCH) [exec /usr/local/epics/startup/HostArch]
if {([info exists env(APSU)]) && ($env(APSU) == 1)} {
} else {
    set env(HOST_ARCH) [exec /usr/local/epics/startup/EpicsHostArch]
    set env(EPICS_HOST_ARCH) [exec /usr/local/epics/startup/EpicsHostArch]
}

set env(PSPRINTER) lp
set env(EPICS_DISPLAY_PATH) /usr/local/iocapps/adlsys:/home/helios2/xfdsys/op/adl:/usr/local/iocapps/adlsys/temp

set env(EPICS_TS_MIN_WEST) 360
set env(EPICS_AR_PORT) 7002
set env(LI) /home/helios1/injsys/linac/linacApp
set env(LI_OP) /home/helios1/injsys/linac/linacApp/op
set env(LI_DATA) /home/helios3/linacdata

# OAG path additions
#set env(HOME) /home/helios/OAG
set env(PATH) $env(PATH):/usr/local/oag/bin_patch/$env(HOST_ARCH)
set env(PATH) $env(PATH):/usr/local/oag/apps/bin/$env(HOST_ARCH)

# EPICS extensions path additions
set env(PATH) $env(PATH):/usr/local/epics/extensions/bin/$env(EPICS_HOST_ARCH)

set env(RPN_DEFNS) /usr/local/oag/apps/src/appconfig/rpn/defns.rpn
set env(ADTHOME) /usr/local/oag/apps/src/appconfig/adt
set env(ORBITSCREENHOME) /usr/local/epics/extensions/src/orbitscreen

set env(LD_LIBRARY_PATH) \
    /usr/dt/lib:/opt/SUNWspro/lib:/usr/lib:/usr/openwin/lib:/usr/dt/lib:/usr/ccs/lib:/opt/X11R5/lib:/usr/ucblib:/usr/local/XR5/Xlib:/opt/gnu/lib:/usr/local/epics/extensions/lib/$env(EPICS_HOST_ARCH)

set env(PATH) /usr/dt/bin:/usr/bin:/opt/SUNWspro/bin:/usr/openwin/bin:/opt/X11R5/bin:/opt/local/bin:/usr/ccs/bin:/opt/gnu/bin:/usr/lib/nis:/usr/sbin:/usr/ucb:/usr/local/bin:/usr/local/XR5/bin:$env(PATH)

set env(PATH) $env(PATH):.

#
# Get host, port, and display from invoking PEM. For some reason,
# only "gets stdin" seems to work. Should be able to use dp_receive.
# At any rate, at this point inetd has dup'd the socket descriptor
# onto stdin, stdout, stderr.
#
set pemData [gets stdin]
regexp {([^~]*)~([^~]*)~([^~]*)~([^~]*)~([^~]*)~([^~]*)~([^~]*)~([^~]*)~([^ ]*)} $pemData \
    match pemServerPort pemHost display interface debugPath debugAutoPath user geometry stack
logText "pemData: $pemData"

set env(DISPLAY) $display

# Honor the APSDebugPath mechanism
if {[string length $debugPath] > 0} {
    set env(OAG_DEBUG_PATH) $debugPath
}

if {[string length $debugAutoPath] > 0} {
    set env(OAG_DEBUG_AUTO_PATH) $debugAutoPath
}

set interpreter /usr/local/oag/bin_patch/$env(HOST_ARCH)/oagwish
if {![file exists $interpreter]} {
    set interpreter /usr/local/oag/apps/bin/$env(HOST_ARCH)/oagwish
    if {![file exists $interpreter]} {
        set interpreter /lustre/oagsoftware/bin/oagwish
        if {![file exists $interpreter]} {
            set interpreter oagwish
        }
    }
}
logText "interpreter: $interpreter"

set script /usr/local/oag/bin_patch/$env(HOST_ARCH)/runMP
if {![file exists $script]} {
    set script /usr/local/oag/apps/bin/$env(HOST_ARCH)/runMP
    if {![file exists $script]} {
        set script /lustre/oagsoftware/bin/runMP
        if {![file exists $script]} {
            set script runMP
        }
    }
}
logText "script: $script"

exec $interpreter $script $pemServerPort $pemHost $env(HOST_ARCH) $interface $user $geometry $stack &
exit

