#!/bin/sh
get_arch()
{
#!/bin/sh
#
# find_arch.sh - determines the machine architechture for Wind/U purposes
#
# Copyright (c) 1994 Bristol Technology Inc.
# All rights reserved.
#
# History
# 08/11/94   JDS   Initial version
# 08/23/94   CCC   Return error exit status on failure
# 11/04/94   JDS   Look in $WUARCH followed by $ARCH for preset arch
#

# be sure "uname" can be found
PATH=$PATH:/bin:/usr/bin

me=`basename $0`

# check WUARCH then ARCH for valid Wind/U architecutre
for environment_var in "WUARCH" "ARCH"
do
  eval `expn=\$$environment_var`
#echo "environment_var = $environment_var $expn"
  if [ "$expn" != "" ]; then
    env_var=$environment_var
    env_arch=$expn
    case "$expn" in
      sun4|sol2|sol2_x86|hp700|hp700mt|rs6000|sgimt|decaosf|ncr) env_arch=$expn
                   break 2 ;;
      *) env_arch="ERROR"
    error_expn=$expn ;;
    esac
  fi
done
if [ "$env_arch" = "ERROR" ]; then
  echo "($me) WARNING: Unrecognized architecture of" \
       "\"$error_expn\" in $env_var."  >&2
  env_arch=""
fi

# figure it out ourselves
# Try to use "uname" - it's POSIX-compliant
found_arch="ERROR"
( exec uname ) > /dev/null 2>&1
if [ $? -eq 0 ]; then
  case "`uname`" in
     OSF1) found_arch=decaosf ;;
    HP-UX) case "`uname -r`" in
	    *.10.*) found_arch=hp700mt;;
	         *) found_arch=hp700;;
	   esac
	   ;;
    IRIX64) found_arch=sgimt ;;
       IRIX) case "`uname -r`" in
       5.3) found_arch=sgi ;;
       6.2) found_arch=sgimt ;;
       esac ;;
      AIX) found_arch=rs6000 ;;
  UNIX_SV) found_arch=ncr ;;
    SunOS) case "`uname -r`" in
             4.1.*) found_arch=sun4 ;;
               5.1) found_arch=sol2_x86 ;;
               5.*) found_arch=sol2 ;;
                 *) found_arch="ERROR" ;;
           esac ;;
        *) found_arch="ERROR" ;;
  esac
fi
if [ "$found_arch" = "ERROR" ]; then
  if [ -f /usr/kvm/stand/kadb ] ; then
    if [ `/bin/arch` = "sun4" ] ; then
      found_arch=sun4
    fi
  elif [ -f /hp-ux ] ; then
    found_arch=hp700
  elif [ -f /usr/lib/boot/unix ] ; then
    found_arch=rs6000
  elif [ -f /ufsboot ] ; then
   if [ "`uname -m`" = "i86pc" ]
   then
     found_arch=sol2_x86
   else
      found_arch=sol2
   fi
  else
    found_arch="ERROR"
  fi
fi

# check to be sure env var setting matches platform
#echo "found_arch = $found_arch   env_arch = $env_arch"
if [ "$env_arch" != "ERROR" -a "$env_arch" != "" -a \
     "$found_arch" != "ERROR" -a "$found_arch" != "$env_arch" ]; then
  echo "($me) WARNING: Architecture of \"$env_arch\" set in $env_var" \
       "should be \"$found_arch\"." >&2
  wuarch=$env_arch # use env value anyway; heuristic could be wrong
elif [ "$env_arch" = "ERROR" -o "$env_arch" = "" ]; then
   wuarch=$found_arch # no env value, use ours
else
  wuarch=$env_arch # env value checks out; use it
fi

# print a message if the arch can not be determined
if [ "$wuarch" = "ERROR" ] ; then
  echo "($me) ERROR: Can't figure our your architecture."  >&2
  echo $wuarch 
  exit 1
else
#  if [ "`hostname`" = "handel" -o "`hostname`" = "otto" ] ; then
#   wuarch="hp700mt"
#  fi
  echo $wuarch
  exit 0
fi
}

#!/bin/sh
#
# windu_registry -- convenient script to start/stop windu_registryd
#
# Copyright (c) 1996 Bristol Technology Inc.
# All rights reserved.
#

#
# all of these can be passed in to windu_registry as arguments
# 
# args gets passed to the windu_registryd
# command must be either start, stop or help
# daemonLocation is where the daemon should be started from
#
args=""
command=""
forcestart="FALSE"
daemonLocation=""

#
# If the user is root, add 1 to the userId so it doesn't start up a system
# wide daemon
#
userId=`id | cut -d '(' -f 1 | cut -d '=' -f 2`
if [ "$userId" = "0" ]
then
   userId="1";
fi

#
# check the command line arguments
#
while [ "$1" != "" ]
do
   if [ "$1" = "-dir" ]
   then
      shift
      daemonLocation="$1"
   elif [ "$1" = "start" -o "$1" = "stop" -o "$1" = "help" ]
   then
      command="$1"
   elif [ "$1" = "-system" ]
   then
      userId="0";
   elif [ "$1" = "-forcestart" ]
   then 
      forcestart="TRUE"
   elif [ "$1" = "-d" ]
   then
      shift
      dataDir="$1"
   else
      args="$args $1"
   fi
   shift
done

#
# daemonLocation must be something.  At boot time, it will be whatever the
# installer says it is.  For instance, -dir /var/adm/windu.  Otherwise, it
# is okay to start it from $WUHOME/lib.$ARCH
#

if [ "$daemonLocation" = "" ]
then
   if [ -z "$WUHOME" ]
   then
      echo "Error starting windu_registryd"
      echo "Either WUHOME must be set or -dir must be specified."
   else
      # Find the dameon executable
      ARCH=`get_arch`
      daemonLocation="$WUHOME/lib.$ARCH"
   fi
fi

#
# Figure out the name of the daemon.  It's always the latest version of the
# daemon in the location specified by the user.
#
currDir=`pwd`
cd $daemonLocation

latestVers=0
for x in `ls windu_registryd*`
do
   currentVers=`echo $x | cut -c 16-`
   if [ `expr $latestVers` -lt `expr $currentVers` ]
   then
      latestVers="$currentVers"
   fi
done
WINDU_REGISTRYD="windu_registryd$latestVers"
cd $currDir

# check what the command is
if [ "$command" = "help" ]
then
   echo "----------------------------------------------------------------------"
   echo "Wind/U Registry Daemon Startup/Shutdown Script - Copyright (c) 1996-97"
   echo "Bristol Technology Incorporated.  All rights reserved."
   echo ""
   echo "SYNTAX: windu_registry	[-dir <directory>] [ -forcestart ] [ -system ]"
	echo " 			[arg [arg]] start|stop|help"
   echo ""
   echo "FUNCTION: convenient script to start/stop the Wind/U registry daemon"
   echo ""
   echo "SWITCHES:"
   echo ""
   echo "-dir <directory>"
   echo "	tells the script where the $WINDU_REGISTRYD"
   echo "	executable is.  This should only be used for administrators"
   echo "	who need to have the daemon start at boot time."
   echo ""
   echo "-forcestart"
   echo "	used only for $WINDU_REGISTRYD startup at boot time in case the"
   echo "	daemon goes down badly before the reboot.  It tells this script to"
   echo "	ignore a .pid file if it exists from a previous run of the daaemon"
   echo ""
   echo "-system"
   echo "	starts a system registry daemon instead of a user-private one"
   echo ""
   echo "arg"
   echo "	these args can be any list of arguments usable by the"
   echo "	$WINDU_REGISTRYD.  See the Wind/U User's Guide for information"
   echo "	about the daemon or type: $WINDU_REGISTRYD -help"
   echo ""
   echo "start	starts the registry daemon owned by this user"
   echo "stop	stops the registry daemon owned by this user"
   echo "help	shows this help"
   echo "----------------------------------------------------------------------"
   exit
fi

# Figure out where to put the data.
if [ "$dataDir" = "" ]
then  
   dataDir="$HOME"
fi

#
# Figure out where the pid file is.  If system is specified or the default
# directory is specified, use the windu subpath, not the .windu path
#
if [ "$userId" = "0" -o "$dataDir" = "/var/adm" ]
then
   pidFile="$dataDir/windu/windu_reg.pid"
else
   pidFile="$dataDir/.windu/windu_reg.pid"
fi

#
# For root, this stops the central daemon
# For a user, it stops the individual user's daemon writing in $HOME/windu
#
if [ "$command" = "stop" ] 
then
   if [ -f "$pidFile" ]
   then
      regPid=`cat "$pidFile"`
      kill -TERM $regPid
      exit 0
   fi
   echo "Could not find $pidFile.  If the daemon was started"
	echo "using the -d option, the windu_reg.pid file might be in another"
	echo "location which I can't seem to locate or there is no registry"
	echo "daemon running.  Use ps -ef (or -aux on sun4) to verify whether"
	echo "a windu_registryd is running."
   exit 0
fi

#
# These are defined in wurpc.h as:
# REGISTRYPROG   ((unsigned long)(0x30030000))
# REGISTRYVERS   ((unsigned long)(2))
# REGISTRYOFFSET ((unsigned long)(0x00030000))
#
REGISTRYPROG=805502976
REGISTRYVERS=2
REGISTRYOFFSET=196608

#
# check whether windu_registryd is there and is executable
#
if [ "$command" = "start" ]
then
   if [ ! -x $daemonLocation/$WINDU_REGISTRYD ] 
   then
      echo "Error: $daemonLocation/$WINDU_REGISTRYD doesn't exist or doesn't have the right permissions."
      exit 1
   fi

   # start the daemon if it's not running already
   if [ -f "$pidFile" -a "$forcestart" != "TRUE" ]
   then
      echo "Error: $pidFile exists.  There may already be a"
      echo "       daemon running.  If you are sure a daemon is not running, remove"
      echo "       $pidFile and run windu_registry start"
      exit 1
   fi
   progNum=`expr $REGISTRYPROG + $userId`
   cd $daemonLocation
   /usr/bin/nohup ./$WINDU_REGISTRYD -d $dataDir -vers $REGISTRYVERS -prog $progNum $args & >> /dev/null
fi


