#	$OpenBSD: rc,v 1.241 2004/03/22 15:02:35 cedric 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.

# Subroutines (have to come first).

# Strip comments (and leading/trailing whitespace if IFS is set)
# from a file and spew to stdout
stripcom() {
	local _file="$1"
	local _line

	{
		while read _line ; do
			_line=${_line%%#*}		# strip comments
			test -z "$_line" && continue
			echo $_line
		done
	} < $_file
}

# End subroutines

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

if [ $1x = shutdownx ]; then
	dd if=/dev/urandom of=/var/db/host.random bs=1024 count=64 >/dev/null 2>&1
	chmod 600 /var/db/host.random >/dev/null 2>&1
	if [ $? -eq 0 -a -f /etc/rc.shutdown ]; then
		echo /etc/rc.shutdown in progress...
		. /etc/rc.shutdown
		echo /etc/rc.shutdown complete.
		if [ "X${powerdown}" = X"YES" ]; then
			exit 2
		fi

		# bring carp interfaces down gracefully
		for hn in /etc/hostname.carp[0-9]*; do
			# Strip off /etc/hostname. prefix
			if=${hn#/etc/hostname.}
			test "$if" = "carp[0-9]*" && continue

			ifconfig $if down
		done
	else
		echo single user: not running /etc/rc.shutdown
	fi
	exit 0
fi

# Configure ccd devices.
if [ -f /etc/ccd.conf ]; then
	ccdconfig -C
fi

# Configure raid devices.
for dev in 0 1 2 3; do
	if [ -f /etc/raid$dev.conf ]; then
		raidctl -c /etc/raid$dev.conf raid$dev
	fi
done

# Check parity on raid devices.
raidctl -P all

swapctl -A -t blk

if [ -e /fastboot ]; then
	echo "Fast boot: skipping disk checks."
elif [ $1x = autobootx ]; then
	echo "Automatic boot in progress: starting file system checks."
	fsck -p
	case $? in
	0)
		;;
	2)
		exit 1
		;;
	4)
		echo "Rebooting..."
		reboot
		echo "Reboot failed; help!"
		exit 1
		;;
	8)
		echo "Automatic file system check failed; help!"
		exit 1
		;;
	12)
		echo "Boot interrupted."
		exit 1
		;;
	130)
		# interrupt before catcher installed
		exit 1
		;;
	*)
		echo "Unknown error; help!"
		exit 1
		;;
	esac
fi

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

umount -a >/dev/null 2>&1
 mount -o ro /dev/fd0a /etc

if [ -e /etc/isfloppy ]; then
 if [ ! -e /etc/ssh/ssh_host_key ]; then
  fsck -y /etc
  mount -u -o rw /etc
 fi
 . /etc/rc
 exit 0
else
 umount /etc
 mount -o ro /dev/sd0a /etc
 if [ -e /etc/isfloppy ]; then
   if [ ! -e /etc/ssh/ssh_host_key ]; then
    mount -u -o rw /etc
   fi
   . /etc/rc
   exit 0
 else 
   echo "/etc/rc does not exist, starting installer"
   echo "install to:"
   echo "1. Floppy (/dev/fd0a)"
   echo "2. Umass (/dev/sd0a)"

   read INSDEV
   if [ "$INSDEV" = "1" ] ; then
     umount /etc
     mount_mfs swap /tmp
     echo "copying /etc files to floppy"
     echo "you may want to get a cup of \$BEVERAGE, as this takes a while"
     disklabel -R fd0 /usr/local/jtan/floppy.disklabel
     newfs /dev/fd0a
     mount -o rw /dev/fd0a /mnt/etc
     cp -pR /usr/local/jtan/floppy/* /mnt/etc
     rm /mnt/etc/rc
     cp /mnt/etc/floppyrc /mnt/etc/rc
     cp /mnt/etc/floppyfstab /mnt/etc/fstab
     cd /usr/local/jtan
     #sh /usr/local/jtan/install.sh
      perl /usr/local/jtan/install.pl

   elif [ "$INSDEV" = "2" ] ; then
	umount /etc
	mount_mfs swap /tmp
	echo "copying /etc files to floppy"
	echo "you may want to get a cup of \$BEVERAGE, as this takes a while"
	disklabel -d /dev/sd0a
	newfs /dev/sd0a
	mount -o rw /dev/sd0a /mnt/etc
     	cp -pR /usr/local/jtan/floppy/* /mnt/etc
     	rm /mnt/etc/rc
    	cp /mnt/etc/floppyrc /mnt/etc/rc
	cp /mnt/etc/floppyfstab /mnt/etc/fstab
	cd /usr/local/jtan
        perl /usr/local/jtan/install.pl

   else
     echo "what device are you talking about?"
     . /etc/rc
     exit
   fi
 fi
fi

