#!/sbin/runscript
# Copyright 1999-2005 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: /var/cvsroot/gentoo-src/rc-scripts/init.d/bootmisc,v 1.45.4.1 2005/01/30 21:23:10 vapier Exp $

depend() {
	use clock hostname
	need localmount
	before logger
}

start() {
	if [[ ${BOOT} == "yes" ]] ; then
		#
		# Put a nologin file in /etc to prevent people from logging in before
		# system startup is complete.
		#
		if [[ ${DELAYLOGIN} == "yes" ]] ; then
			echo "System bootup in progress - please wait" > /etc/nologin
			cp /etc/nologin /etc/nologin.boot &> /dev/null
		fi

		if [[ -e /etc/sysctl.conf ]] ; then
			ebegin "Configuring kernel parameters"
			/sbin/sysctl -p /etc/sysctl.conf &> /dev/null
			eend 0
		fi

		if [[ -z ${CDBOOT} ]] && ! touch /var/run/.keep 2> /dev/null ; then
			ewarn "Skipping /var and /tmp initialization (ro root?)"
			return 0
		fi

		if [[ -x /sbin/env-update.sh ]] ; then
			ebegin "Updating environment"
			/sbin/env-update.sh -u > /dev/null
			eend 0
		fi

		#
		# Take care of random stuff [ /var/lock | /var/run | pam ]
		#
		ebegin "Cleaning /var/lock, /var/run"
		rm -rf /var/run/console.lock /var/run/console/*
		if [[ -z ${CDBOOT} ]] ; then
			#
			# Clean up any stale locks.
			#
			find /var/lock -type f -print0 | xargs -0 rm -f --
			#
			# Clean up /var/run and create /var/run/utmp so that we can login.
			#
			for x in $(find /var/run/ ! -type d ! -name utmp ! -name innd.pid ! -name random-seed) ; do
				local daemon=${x##*/}
				daemon=${daemon%*.pid}
				# Do not remove pidfiles of already running daemons
				if [[ -z $(ps --no-heading -C "${daemon}") ]] ; then
					if [[ -f ${x} || -L ${x} ]] ; then
						rm -f "${x}"
					fi
				fi
			done
		fi
		# Reset pam_console permissions
		if [[ -x /sbin/pam_console_apply ]] && [[ ! -c /dev/.devfsd ]] ; then
			/sbin/pam_console_apply -r
		fi
		# Create the .keep to stop portage from removing /var/lock
		> /var/lock/.keep
		eend 0

		#
		# Clean up /tmp directory
		#
		if [[ -z ${CDBOOT} ]] ; then
			ebegin "Cleaning /tmp directory"
			rm -f /tmp/.X*-lock /tmp/esrv* /tmp/kio* /tmp/jpsock.* /tmp/.fam* &> /dev/null
			rm -rf /tmp/.esd* /tmp/orbit-* /tmp/ssh-* /tmp/ksocket-* /tmp/.*-unix &> /dev/null
			# Make sure our X11 stuff have the correct permissions
			mkdir -p /tmp/.{ICE,X11}-unix &> /dev/null
#			chown root.root /tmp/.{ICE,X11}-unix &> /dev/null
			chmod 1777 /tmp/.{ICE,X11}-unix &> /dev/null
			eend 0
		fi

		#
		# Create an 'after-boot' dmesg log
		#
		touch /var/log/dmesg
		chmod 640 /var/log/dmesg
		dmesg > /var/log/dmesg
	fi

	#
	# Check for /etc/resolv.conf, and create if missing
	#
	[[ -f /etc/resolv.conf ]] || touch /etc/resolv.conf &> /dev/null
}


# vim:ts=4
