#!/sbin/runscript
# Copyright 1999-2002 Gentoo Technologies, Inc.
# Distributed under the terms of the GNU General Public License v2
# $Header: /home/cvsroot/gentoo-src/rc-scripts/init.d/bootmisc,v 1.24 2002/12/05 19:05:23 azarah Exp $


depend() {
	need clock localmount hostname
	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 [ -x /sbin/rc-envupdate.sh ]
		then
			ebegin "Updating environment"
			/sbin/rc-envupdate.sh >/dev/null
			eend 0
		fi

		# Reset pam_console permissions
		rm -rf /var/run/console.lock /var/run/console/*

		#
		# Clean up any stale locks.
		#
		ebegin "Cleaning /var/lock, /var/run"
		( cd /var/lock && find . -type f -exec rm -f -- {} \; 1>&2 )
		#
		# Clean up /var/run and create /var/run/utmp so that we can login.
		#
		cd /var/run && \
		for x in $(find . ! -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 [ "$(ps -A | awk '{print $4}' | egrep "${daemon}")" != "${daemon}" ]
			then
				if [ -f ${x} -o -L ${x} ]
				then
					rm -f ${x}
				fi
			fi
		done
		# Setup login records
		: > /var/run/utmp
		touch /var/log/wtmp
		chgrp utmp /var/run/utmp /var/log/wtmp
		chmod 0664 /var/run/utmp /var/log/wtmp
		# Remove /var/run/utmpx (bug from the past)
		rm -f /var/run/utmpx
		# Reset pam_console permissions
		[ -x /sbin/pam_console_apply -a ! -c /dev/.devfsd ] && \
			/sbin/pam_console_apply -r
		# Create the .keep to stop portage from removing /var/lock
		>/var/lock/.keep
		eend 0

		#
		# Clean up /tmp directory
		#
		ebegin "Cleaning /tmp directory"
		rm -f /tmp/.X*-lock &>/dev/null
		rm -f /tmp/esrv* &>/dev/null
		rm -f /tmp/kio* &>/dev/null
		rm -f /tmp/jpsock.* &>/dev/null
		rm -f /tmp/.fam*
		rm -rf /tmp/.esd* &>/dev/null
		rm -rf /tmp/orbit-* &>/dev/null
		rm -rf /tmp/ssh-* &>/dev/null
		rm -rf /tmp/ksocket-* &>/dev/null
		rm -rf /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

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

	if [ -c /dev/ttyp0 ]
	then
		chmod 666 /dev/tty[p-za-e][0-9a-f] &>/dev/null
		chown root:tty /dev/tty[p-za-e][0-9a-f] &>/dev/null
	fi
}


# vim:ts=4
