#!/bin/sh

##
# Multi-user startup script.
#
# Copyright 1997-2002 Apple Computer, Inc.
#
# Customize system startup by adding scripts to the startup 
# directory, rather than editing this file.
##

##
# Set shell to ignore Control-C, etc.
# Prevent inadvertent problems caused by interrupting the shell during boot.
##

stty intr  undef
stty kill  undef
stty quit  undef
stty susp  undef
stty start undef
stty stop  undef
stty dsusp undef

. /etc/rc.common

stty status '^T'

##
# Handle arguments passed from init.
##

BootType=$1; shift;

if [ -z "${BootType}" ]; then BootType="multiuser"; fi

##
# Handle options
##

VerboseFlag=""
   SafeBoot=""

args=$(getopt vx $*)
set -- ${args};
for option; do
    case "${option}" in
      -v)
        VerboseFlag="-v"
	;;
      -x)
        SafeBoot="-x"
	;;
    esac;
done;

echo ""

case "${BootType}" in
  "autoboot")
    ConsoleMessage "Automatic reboot in progress"
    ;;
  "multiuser")
    ConsoleMessage "Multiuser startup in progress"
    ;;
  *)
    ConsoleMessage "Unknown boot request: multiuser startup in progress"
    ;;
esac

##
# Are we booting from a CD-ROM?  If so, switch over to /etc/rc.cdrom.
##

if [ -d /System/Installation ] && [ -f /etc/rc.cdrom ]; then

    # Hand off to installation script
    /etc/rc.cdrom ${BootType}

    # All done; shut down
    # We shouldn't get here; CDIS should reboot the machine when done
    ConsoleMessage "CD-ROM boot procedure complete"
    halt
    exit 0

fi

netboot=$(/usr/sbin/sysctl kern.netboot | /usr/bin/sed -e 's/^[^0-9]*//')

##
# Mount essential local filesystems (according to /etc/fstab).
##

ConsoleMessage "Mounting local filesystems"

mount -vat hfs
mount -vat ufs
mount -t fdesc -o union stdin /dev
mkdir -p -m 0555 /.vol && chmod 0555 /.vol && mount_volfs /.vol

##
# Does the installer need to do some early boot cleanup?
##

if [ -f /etc/rc.installer_cleanup ]; then
    /etc/rc.installer_cleanup ${BootType}
fi

##
# Start BootCache
##
BootCacheControl=/System/Library/Extensions/BootCache.kext/Contents/Resources/BootCacheControl
if [  "${SafeBoot}" != "-x" -a -x "${BootCacheControl}" ]; then
    ${BootCacheControl} start
fi

##
# Create mach symbol file
##

rm -f /mach.sym

sysctl -n kern.symfile

if [ -f /mach.sym ]; then
  rm -f /mach
  ln -s /mach.sym /mach
else
  rm -f /mach
  ln -s /mach_kernel /mach
fi

sync

##
# Start kextd
##

if [ "${SafeBoot}" = "-x" ]; then
    ConsoleMessage "Configuring kernel extensions for safe boot"
    kextd -x
else
    ConsoleMessage "Configuring kernel extensions"
    kextd
fi

##
# Create or update the mkext cache, which will make subsequent
# boots slightly faster.
##

if [ ! "${netboot}" = "1" ] ; then
    if [ ! -f /System/Library/Extensions.mkext -o \
     /System/Library/Extensions.mkext -ot /System/Library/Extensions ]; then

	if [ "${SafeBoot}" != "-x" ]; then
	    ConsoleMessage "Updating kernel extensions cache"
	    rm -f /System/Library/Extensions.mkext
	    /usr/sbin/kextcache -elF -a `arch`
	fi
    fi
fi

##
# update flushes the cached blocks from the filesystem using
# the sync system call every 30 seconds.  This ensures the
# disk is reasonably up-to-date in the event of a system crash.
##

update

##
# Start the virtual memory system.
##

ConsoleMessage "Starting virtual memory"

swapdir=/private/var/vm
if [ "${netboot}" = "1" ]; then
    sh /etc/rc.netboot setup_vm ${swapdir}
fi

# Make sure the swapfile exists
if [ ! -d ${swapdir} ]; then
    ConsoleMessage "Creating default swap directory"
    mount -uw /
    mkdir -p -m 755 ${swapdir}
    chown root:wheel ${swapdir}
else
    rm -rf ${swapdir}/swap*
fi
		    
dynamic_pager -F ${swapdir}/swapfile


##
# Start daemon to fix incorrectly-prebound binaries
##
    if [ -x /usr/libexec/fix_prebinding ]; then
        /usr/libexec/fix_prebinding
    fi


##
# Clean up and reset files and devices.
##
. /etc/rc.cleanup


##
# Early start for any startup items for performance reasons
##
configd


##
# pre-heat support for working set profiles
##
appprofiledir=/private/var/vm/app_profile

if [ ! -d ${appprofiledir} ]; then
	if [ -f  ${appprofiledir} ]; then
		mv -f ${appprofiledir} "${appprofiledir}_" 
	fi
	mkdir -p -m 711 ${appprofiledir}
	chown root:wheel ${appprofiledir}
fi


##
# Insert BootCache prefetch tag
##
if [  "${SafeBoot}" != "-x" -a -x "${BootCacheControl}" ]; then
    ${BootCacheControl} tag
fi



##
# Start System Services
##

# Set language from CDIS.custom - assumes this is parse-able by sh
. /var/log/CDIS.custom 
export LANGUAGE

SystemStarter -g ${VerboseFlag} ${SafeBoot}

exit 0
