#!/bin/sh
#
# $Id: rc.replacement,v 1.17 2002/08/07 02:15:16 bsd Exp $
#
# Copyright 2001, 2002  Brian S. Dean <bsd@bsdhome.com>
# All Rights Reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY BRIAN S. DEAN ``AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL BRIAN S. DEAN BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
# OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
# DAMAGE.
# 
# ACKNOWLEGDEMENTS:
# 
# This code contains software Copyrighted by Matt Dillon and The
# FreeBSD Project under the following conditions:
# 
# Copyright (c) 1999  Matt Dillon
# All rights reserved.
#
# Copyright (c) 2000  The FreeBSD Project
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
# 

/bin/echo "CDROOT replacement rc running"

bootmode=$1
stty status '^T'
trap : 2
trap : 3
HOME=/
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin
export HOME PATH

ckrc() {
  case $1 in
    0)
      ;;
    *)
      if [ "$3" = "exit" ]; then
        exit $1
      else
        echo "$2 failed: dropping into /bin/sh"
        /bin/sh
      fi
      ;;
  esac
}

echo "Load rc.conf environment"

#
# Load defaults and overrides, if present
#
if [ -r /etc/defaults/rc.conf ]; then
	. /etc/defaults/rc.conf
	source_rc_confs
elif [ -r /etc/rc.conf ]; then
	. /etc/rc.conf
fi

meg2blocks () {
  /bin/expr "$1" \* 1024 \* 2
}

etcsize=${etcsize:=4}
devsize=${devsize:=1}

export ETC_MFS=`meg2blocks ${etcsize}`
export DEV_MFS=`meg2blocks ${devsize}`

# Figure out sizes for /tmp and /var MFS's based on how much physical
# memory we have
export MEMSZ=`/sbin/sysctl -n hw.physmem`
if [ "$MEMSZ" -le 16777216 ]; then
  tmpsize=${tmpsize:=2}
  varsize=${varsize:=4}
elif [ "$MEMSZ" -le 20971520 ]; then
  tmpsize=${tmpsize:=2}
  varsize=${varsize:=8}
elif [ "$MEMSZ" -le 33554432 ]; then
  tmpsize=${tmpsize:=4}
  varsize=${varsize:=8}
elif [ "$MEMSZ" -le 67108864 ]; then
  tmpsize=${tmpsize:=10}
  varsize=${varsize:=15}
else
  tmpsize=${tmpsize:=20}
  varsize=${varsize:=20}
fi

export TMP_MFS=`meg2blocks ${tmpsize}`
export VAR_MFS=`meg2blocks ${varsize}`

echo "Configure /dev"

#
# Set up /dev
#
# extract a list of device entries, then copy them to a writable partition
/sbin/mount_mfs -s $TMP_MFS -b 8192 -f 1024 -i 512 -T qp120at dummy /tmp
(cd /; /usr/bin/find -x dev | /usr/bin/cpio -o -H newc) > /tmp/dev.tmp
/sbin/mount_mfs -s $DEV_MFS -b 8192 -f 1024 -i 512 -T qp120at dummy /dev
(cd /; /usr/bin/cpio -i -H newc -d < /tmp/dev.tmp)
rm /tmp/dev.tmp

echo "Mount floppy disk if present"

#
# Mount the floppy drive to use to override /etc files
#
FDMP=/mnt
FD=`/bin/df | /usr/bin/tr -s ' ' | /usr/bin/cut -f6 -d' ' | \
      /usr/bin/grep $FDMP`
if [ "$FD" != "$FDMP" ]; then
  echo "Mounting non-volatile data disk"
  FDMOUNTED=YES
  /sbin/mount -r /dev/fd0 $FDMP
  rc=$?
  if [ "$rc" -ne 0 ]; then
    echo "error mounting floppy disk, no overrides will be provided"
    FDMOUNTED=NO
  fi
fi

echo "Bootstrap /etc MFS"

#
# Copy /etc to /tmp temporarily while we bootstrap /etc
#
/bin/cp -Rp /etc /tmp
ckrc $? "cp /etc to /tmp/etc MFS"
/sbin/mount_mfs -s $ETC_MFS -b 8192 -f 1024 -T qp120at dummy /etc
ckrc $? "MFS mount on /etc"
/bin/chmod 755 /etc
/bin/cp -Rp /tmp/etc/* /etc
ckrc $? "cp /tmp/etc to /etc MFS"
rm -rf /tmp/etc

echo "Check floppy for /etc overrides"

#
# Check for overrides from the floppy disk
#
if [ -d $FDMP/etc ]; then
  echo "Override /etc files with those from $FDMP/etc"
  /bin/cp -Rp $FDMP/etc/* /etc
  ckrc $? "cp $FDMP/etc to /etc MFS"
fi

if [ "$FDMOUNTED" = "YES" ]; then
  /sbin/umount $FDMP
fi

#
# Don't trust the floppy override files - ensure owner and group are
# secure
#
echo "Set /etc owner and group"
/bin/ls -1d /etc/* | grep -v '^/etc/home' | xargs chown -R root:wheel
ckrc $? "set /etc owner and group"

if [ -d /etc/uucp ]; then
  chown -R uucp:uucp /etc/uucp
  ckrc $? "set /etc owner and group in /etc/uucp"
fi

if [ -d /etc/ssh ]; then
  chmod 600 /etc/ssh/ssh_host*_key
fi

#
# Now that floppy overrides have been provided, re-source the rc.conf
# files to pick up any changes.
#
if [ -r /etc/defaults/rc.conf ]; then
	. /etc/defaults/rc.conf
	source_rc_confs
elif [ -r /etc/rc.conf ]; then
	. /etc/rc.conf
fi

#
# Allow a local hook to run before we actually run the system rc file
#
rc_hook=${rc_hook:=/etc/rc.presystemrc}
echo "Looking for local rc hook ${rc_hook}"
if [ -f "${rc_hook}" ]; then
  echo "Running ${rc_hook}"
  /bin/sh "${rc_hook}"
fi

#
# Now, run the original /etc/rc file that was provided by the system
#
echo "Running system supplied /etc/rc file"
mv /etc/rc /etc/rc.replacement
mv /etc/rc.system /etc/rc
exec /bin/sh /etc/rc



syntax highlighted by Code2HTML, v. 0.9.1