##	$OpenBSD: rc,v 1.90 1998/10/12 03:02:57 art Exp $
#
# System startup script run by init on autoboot
# or after single-user.
# Output and error are redirected to console by init,
# and the console is the controlling terminal.

stty status '^T'

# Set shell to ignore SIGINT (2), but not children;
# shell catches SIGQUIT (3) and returns to single user after fsck.
trap : 2
trap : 3	# shouldn't be needed

HOME=/; export HOME
PATH=/sbin:/bin:/usr/sbin:/usr/bin
export PATH


trap "echo 'Boot interrupted.'; exit 1" 3

set `ifconfig le0`
IPADDR=$8
hostname=`grep $IPADDR /etc/hosts | awk '{print $2}'`

#swapon -a
#swapon /swap.`echo ${hostname} | cut -f1 -d.`

umount -a >/dev/null 2>&1
mount -uw /		# root on nfs requires this, others aren't hurt

# if there's no /etc/host.random, make one through /dev/urandom
#if [ ! -f /etc/host.random ]; then
#  dd if=/dev/urandom of=/etc/host.random bs=1024 count=64 >/dev/null 2>&1
#  chmod 600 /etc/host.random
#fi

# set hostname, turn on network
echo 'starting network'

#. /etc/netstart

hostname $hostname
if [ -f /etc/defaultdomain ]; then
  domainname `cat /etc/defaultdomain`
fi

# pick up option configuration
. /etc/rc.conf

# Configure the IP filter before configuring network interfaces
#if [ X"${ipfilter}" = X"YES" -a -f "${ipfilter_rules}" ]; then
#  echo 'configuring IP filter'
#  ipf -Fa -f ${ipfilter_rules} -E
#else
        ipfilter=NO
#fi

# set the address for the loopback interface
ifconfig lo0 inet localhost

# use loopback, not the wire
route -n add -host $hostname localhost
route -n add -net 127 127.0.0.1 -reject

(
  tmp="$IFS"
  IFS="$IFS."
  set -- `echo /etc/hostname*`
  IFS=$tmp
  unset tmp

  while [ $# -ge 2 ] ; do
    shift            # get rid of "hostname"
    (
      read af name mask bcaddr extras
      read dt dtaddr
#JML
      bcaddr="255.255.255.0"

      if [ ! -n "$name" ]; then
        echo "/etc/hostname.$1: invalid network configuration file"
        exit
      fi

      cmd="ifconfig $1 $af $name "
      if [ "${dt}" = "dest" ]; then cmd="$cmd $dtaddr"; fi
      if [ -n "$mask" ]; then cmd="$cmd netmask $mask"; fi
      if [ -n "$bcaddr" -a "X$bcaddr" != "XNONE" ]; then
        cmd="$cmd broadcast $bcaddr";
      fi
      cmd="$cmd $extras";

      $cmd
    ) < /etc/hostname.$1
    shift
  done
)

# default multicast route
route -n add -net 224.0.0.0 -interface $hostname

# Configure NAT after configuring network interfaces
if [ "${ipnat}" = "YES" -a "${ipfilter}" = "YES" -a -f "${ipnat_rules}" ]; then
  echo 'configuring NAT'
  ipnat -CF -f ${ipnat_rules}
else
  ipnat=NO
fi


# clean up left-over files

if [ X"${ipfilter}" = X"YES" -a X"${ipmon_flags}" != X"NO" ]; then
  echo 'starting ipmon';		ipmon ${ipmon_flags}
fi

# $photurisd_flags is imported from /etc/rc.conf;
# If $photurisd_flags == NO or /etc/photuris/photuris.conf doesn't exist, then
# photurisd isn't run.
if [ "X${photurisd_flags}" != X"NO" -a -e /etc/photuris/photuris.conf ]; then
  echo 'starting photurisd';	photurisd ${photurisd_flags}
fi


# $timed_flags is imported from /etc/rc.conf;
# if $timed_flags == NO, timed isn't run.
if [ "X${timed_flags}" != X"NO" ]; then
  echo -n ' timed'; timed $timed_flags
fi
echo '.'

mount -a -t nfs


# build ps databases
echo 'building databases...'
#kvm_mkdb
dev_mkdb

#[ -f /etc/rc.securelevel ] && . /etc/rc.securelevel
#if [ X${securelevel} != X"" ]; then
#  echo -n 'setting kernel security level: '
#  sysctl -w kern.securelevel=${securelevel}
#fi

echo -n starting network daemons:

# $gated and $routed_flags are imported from /etc/rc.conf.
# If $gated == YES, gated is used; otherwise routed.
# If $routed_flags == NO, routed isn't run.
if [ X${gated} = X"YES" -a -e /etc/gated.conf ]; then
  echo -n ' gated';		gated $gated_flags
elif [ "X${routed_flags}" != X"NO" ]; then
  echo -n ' routed';		routed $routed_flags
fi

# $mrouted_flags is imported from /etc/rc.conf;
# If $mrouted_flags == NO, then mrouted isn't run.
if [ "X${mrouted_flags}" != X"NO" ]; then
  echo -n ' mrouted';		mrouted $mrouted_flags
fi


#if [ X${inetd} = X"YES" ]; then
#  echo -n ' inetd';		inetd
#fi


echo '.'


echo -n standard daemons:

date

# Start Xserver, Connect fs to fontpool, terminate at logout, then restart
# If no connect in 60 seconds, die.

while :
do
  /usr/X11R6/bin/X -to 60 -query xpool.math.lsa.umich.edu -fp tcp/xfspool.math.lsa.umich.edu:8888 -terminate >/dev/null 2>&1
#  sleep 3
done

exit 0

