#!/bin/sh

# Setup a log file for trouble-shooting problems.
exec >/opt/ia/remote_dm.log 2>&1
set -x

#
# This line identifies the PC requiring the DM/FV without checking the
# /etc/hosts file, so if you are using DHCP keep the following line.
# If you want to check the /etc/hosts file for added security comment out
# the following line.
# 
NAME=`echo $1 | awk -F: '{ print $1 }'`

#
# This line is required if the PC has static IP Address. This line is
# needed to check the /etc/hosts file. Remark out this line if you use
# DHCP for IP addresses.
#
# IP_ADDR=`echo $1 | awk -F: '{ print $1 }'`

PATH=/bin:/usr/bin:/usr/sbin:/etc:/usr/ucb:/usr/local:/usr/ccs/bin:.:/usr/opt/SUNWmd/sbin:/opt/gms/bin:/usr/fox/wp/bin/tools
export PATH

# Need to set up library path for jettool (needs libview.so.x)
# LD_LIBRARY_PATH=/usr/openwin/lib ; export LD_LIBRARY_PATH
LD_LIBRARY_PATH=/opt/gms/lib:/usr/openwin/lib:/usr/dt/lib:/opt/windu/lib:/opt/windu/fox ; export LD_LIBRARY_PATH
l () ls -l $*
lc () ls -CF $*

#
# Added "-sig" arg to blink_task to avoid the Display Manager starting
# up before blink_task is fully initialized.  blink_task uses SIGUSR1
# to signal back.  This signal is different between the platforms.
# So we need to determine what signal to trap on
#
SIG_PATH=/usr/include/sys/signal.h
SIGUSR1=`grep SIGUSR1 $SIG_PATH | awk '{ printf ( "%s", $3 ) }'`

#
# This line must be remarked out if you are using DHCP or do not want
# the IP address associated with a PC name from the /etc/hosts file.
#
# NAME=`awk '{if ($1 == "'$IP_ADDR'") print $2}' /etc/hosts`

#
# Comment out the following line if you are using Exceed's Xstart and
# then add the new REM_STATION line underneath it.
#
# REM_STATION=`who am i| awk '{print $6}' | tr -d "()"`
REM_STATION=$NAME:0

LOC_STATION=`uname -n`

#
# Comment out this line if you are using Exceed's Xstart and then add
# the new DISPLAY line underneath it.
# DISPLAY=$REM_STATION:0 ; export DISPLAY
DISPLAY=$REM_STATION ; export DISPLAY

echo Setting DISPLAY to $DISPLAY

WUHOME=/opt/windu ; export WUHOME
GMS_HOME=/opt/gms ; export GMS_HOME
HHPATH=/opt/windu/bin ; export HHPATH
HHHOME=/opt/windu ; export HHHOME

if [ -z "$REM_STATION" -o "$REM_STATION" = "$LOC_STATION" ]
then
   exit
fi

#
# If this is not a WP or AW, exit
# Determine station type from hldb file
#    11 DEC 97   TEC  Added station 206 (Bug# 12129)
#

HLDB=/usr/fox/sp/hldb

if [ -s $HLDB ]
then
   TYPE=`awk '{   if ($1 == "'$LOC_STATION'")
                  {
                     # filter out non-WP/AW stations
                     # WP/AWs station types:
                     #    AW	30x
                     #    WP	40x
                     if ( $2 ~ /[234]0[0-9]/ )
                     {
                        print $2
                     }
                     exit
                  }
              }' $HLDB`
fi

if [ -z "$TYPE" ]
then
   exit
fi

# Remark the next line out so the "u" key works while using Exceed.
# /usr/fox/wp/bin/jettool -Wi -f &

#If you want to use jettool from the remote PC to the I/A Series printer
# you can add this line.
# /usr/fox/wp/bin/jettool -grabkey 23 -Wi -f &

PID_PRINT=`echo $!`

#
# Wait for SIGUSR1 signal from blink_task or 30 second
# timeout.
#
trap "GO=Y" $SIGUSR1
GO=N

/usr/fox/wp/bin/blink_task -sig -r -hb 5 &
PID_BLINK=`echo $!`

tot=0
while test "$GO" = "N"
 do
    sleep 1
    tot=`expr $tot + 1`
    if test $tot -eq 30
     then
       GO="Y"
    fi
 done

trap "" $SIGUSR1


PID_BLINK=`echo $!`
/usr/fox/wp/bin/dm_wp50

### BEGIN 07/16/98 FONTDEF.DAT STARTUP FILE FIX ###

#
# Set the startup directory for default_dm, so that
# SL-GMS applications use the correct fontdef.dat file.
#		(07/15/98 AJ Cerier)
#
# Get the Vendor name for our X Server
#
VENDOR="`/usr/openwin/bin/xdpyinfo | grep 'vendor string' | awk '{ print $3 }' `"
#
# If this is a Sun X server ...
#
if [ "$VENDOR" = "Sun" ] 
then
   # 
   # For the Sun X Server, we start from /usr/fox.  Because
   # there is no fontdef.dat file in /usr/fox, SL-GMS applications
   # will user the fontdef.dat file in /usr/fox/wp/data.  This file
   # contains font selections optimized for Sun X Servers.
   #
   cd /usr/fox
else
   #
   # For non-Sun X Server, we start from /opt/ia, where
   # the "remote" fontdef.dat file is located.  This file
   # contains font selections optimized for non-Sun X Servers..
   #
   cd /opt/ia
fi

### END 07/16/98 FONTDEF.DAT STARTUP FILE FIX ###

# /opt/fox/wp/bin/default_dm
if [ -n "$PID_BLINK" ]
then
	# echo About to kill blink_task
	kill -9 $PID_BLINK
fi
if [ -n "$PID_PRINT" ]
then
	# echo About to kill jettool
	kill -USR1 $PID_PRINT
fi
exit