#!/sbin/sh
#
# $Revision: 2.0.0.15 $ $State: Exp $
# $Date: 1993/11/10 03:49:15 $
#
# Xkernel init program
#
# Purpose, perform all commands required to get machine ready to run
# the X server processes (Xsun).
#
# Copyright (c) 1992,1993 Seth Robertson
#		          seth@ctr.columbia.edu

########################################
# Set up frequently used variables
#
PATH=/sbin:/; export PATH
HOME=/; export HOME
Xserver=/Xsun; export Xserver

########################################
# Shell functions
shcat ()
{
while read i
 do
  echo "$i"
done
}

busy_loop ()
{
 shcat < /sbin/message.busy_loop
 while :
  do
   :
  done
 exit 3
}


########################################
# Last resort for Debugging
#
# This will not actually print anything on the screen, but it might
# give a hint to someone who is having problems.
#
# The reason why I did this instead of something more informative is
# because as far as I know, there is NO way I can influence ANYTHING
# without /dev/console (even the fs is ro)
#
if [ ! -c /dev/console ]; then
 echo SOL
 exit 2
else
 echo /sbin/init is running > /dev/console
fi

########################################
# Single user mode
#
if [ "x$1" = "x-s" ];then
 (/sbin/sh) > /dev/console 2>&1 < /dev/console
fi

#########################################
# Set up the I/O stuff
#
exec >/dev/console 2>&1

########################################
# Debugging warnings
#
if [ ! -x $Xserver -o ! -f /etc/hosts ]
 then
  shcat < /sbin/message.nofile
  busy_loop
 fi

########################################
# Configure the loopback interface
# I do not know if this is necessary, but
# it should not hurt.
#
ifconfig lo0 127.0.0.1 up

########################################
# Configure the external interface, if possible
#
# If you have multiple ifconfig_cmd commands (for example if you have
# both 3/50s and 3/180--meaning both le0 and ie0 interfaces), you
# should be able to have both present.  You will get an error message,
# but you can ignore it.
#
# If you have machines with different BROADCAST or NETMASK needs but
# with the same INTERFACE, you must either have seperate /export/root
# directories for them, or you must do some "case" or "if" switching
# on the `hostname`
#
# I understand Sun has some new wierd hostconfig stuff which will use
# bootparams and other things to set the up for you.  I never bothered
# with it and I think it is only available for Sun 4s anyway.
#
# Of course, if the default settings of the interface is fine, then
# remove /etc/ifconfig_cmd*
#
for f in /etc/ifconfig_cmd.*
 do
  sh -c $f
 done

########################################
# Set up default router
#
# If you have specified a default route, this will set it up for you.
# If you have hosts which need different default routes, then you
# either can do case or if switching on `hostname` or you can make a
# statically linked version of in.routed or in.gated.  If you know
# what I am talking about, and need to, then go for it.
#
if [ -f /etc/defaultrouter ]; then
 read defaultrouter < /etc/defaultrouter
 route add default $defaultrouter 1
fi

########################################
# Mount NFS filesystems.
#
# Under normal Xkernel 2.0 setup, you do NOT need to perform ANY NFS
# mounts yourself.  The root directory is mounted by the kernel, and
# you need nothing else.  However, it is possible that because of some
# local wierdity (e.g. you cannot run the font server) you must mount
# some NFS file systems.  Now is a good time.
#
if [ -f /etc/fstab ]; then
 mount -antv nfs
fi

########################################
# More terminal error checking
#
if [ ! -f /usr/lib/X11/rgb.pag -o ! -f /usr/lib/X11/rgb.dir ]
 then
  shcat < /sbin/message.norgb
  busy_loop
 fi

########################################
# Set the date (if you like your syslog messages to be correct)
#
#/sbin/rdate -s primaryxdmhost

########################################
# Start up system message logging
#
# This is not absolutely necessary, but by default, kernel printfs are
# turned off (otherwise they mess up the graphics device in X).  The
# message which the kernel might have printed might well be of
# interest, so this will forward the syslog message to loghost.
#
# If you want to see kernel printfs, read the Xpert FAQ,
# or if you know what you are doing, use adb to set
# noprintf to 0 in vmunix
#
# vanilla syslogd will die a horrible death
#
if [ -f /etc/syslog.conf -a -f /sbin/syslogd ]; then
	syslogd
fi

########################################
# Clear sockets
#
# This is to prevent a problem we have with fontserver
# (namely Xsun will use the same socket to contact
# the fontserver and then both will get confused)
#
# This uses a random number of socket so that Xsun
# will hopefully use a different one when it talks
# to the fontserver

clearsockets fontserver

########################################
# Start Xsun
#
# If we were feeling confident we could exec it...
#
# You may ignore the message about the Unix Domain socket if
# the X server does not exit because of it.  See -pn below
#
#
# Xsun has billions of options.  RTFM for Xsun and Xserver
# Just as a quick description of the ones I use.
#
# -pn	Partial Network.  This prevents the X server from exiting if
#	it cannot, for instance, establish the Unix Domain socket for
#	communications with the X server.  This is good, since we do
#	not have a /tmp and it would not be writable if we did have
#	it.  You will still get the error message, but it can be
#	safely ignored.
#
# -indirect  XDMCP indirect logins.  This option tells the X server to
#	use XDMCP (which provides some security and allows xhosts from
#	remote machines) and to use the indirect option to XDM.  This
#	means that the chooser stuff will be executed by the remote
#	XDM.  This allows the user at the X terminal to choose which
#	host it wants to log in to.
#
# -query  XDMCP direct logins.  I use indirect over direct now, but for
#	your application, you may wish to use this instead.  This
#	option just bypasses the menu which allows you to select which
#	host you want to log in to.
#
# -fp	This is the font server option.  This option tells the X
#	server that instead of getting the fonts from the Unix
#	filesystem (read via NFS) it should contact a font server.
#	Read fs(1) for more information.  (Thanks to Jay Bourland
#	<jayb@math.stanford.edu> for getting me the scoop on this
#	option).
#
# If you are having problems, delete the /dev/null or /dev/log redirections
#
#
# If you need to exec different Xsun depending on the host (e.g. some
# machines are color and some monochrome and you want to use the
# smaller mono-only version on the monochrome versions) you can either
# generate an extra /export/root for the color machines, or you can do
# something like:
#
#case `hostname` in
#	sinope*|rousseau*|callisto*|ganymede*)	Xserver=/Xsuns/Xsun;;
#esac
#

FONTPATH=/usr/lib/X11/fonts; export FONTPATH 
if [ -d $FONTPATH ]; then
  # Hmm, we have a font directory, so we must be using NFS for fonts. 
  for i in $FONTPATH/*; do
    if [ -f $i/fonts.dir ]; then
       FP="${FP:+$FP/,}$i"
    fi
  done
else
  FP="tcp/fontserver:7000/all"
fi

# change -indirect to -query if desired, below.
XDMCMD="-query primaryxdmhost"

XSRVCMD="$Xserver -fp $FP -co /usr/lib/X11/rgb -pn $XDMCMD"

if [ -x /sbin/syslogd -a -f /etc/syslog.conf -a -p /dev/log ]; then
  $XSRVCMD >/dev/log 2>&1
else
  $XSRVCMD # >/dev/null 2>&1
fi

########################################
# We should never get here.
#
shcat < /sbin/message.Xterm
busy_loop
