#!/bin/bash
# Copyright 2001 Daniel Robbins <drobbins@gentoo.org>, Gentoo Technologies, Inc.
# Distributed under the GNU General Public License, version 2.0 or later.

trap ":" INT QUIT TSTP	
export PATH=/sbin:/bin:/usr/sbin:/usr/bin
umask 022

if [ $$ -ne 1 ]
then
	exec /sbin/init.system $*
fi

mount -n /proc
devfs="yes"
for copt in `cat /proc/cmdline`
do
	if [ "${copt%=*}" = "wrapper" ]
	then
		parms=${copt##*=}
		#parse wrapper option
		if [ "${parms/nodevfs//}" != "${parms}" ]
		then
			devfs="no"
		fi
	fi
done

if [ "$devfs" = "yes" ]
then
	if [ -e /dev/.devfsd ] 
	then
		clear
		echo
		echo "The init wrapper has detected that /dev has been automatically mounted by"
		echo "the kernel. This will prevent devfs from automatically saving and"
		echo "restoring device permissions. While not optimal, your system will still"
		echo "be able to boot, but any perm/ownership changes or creation of new compat."
		echo "device nodes will not be persistent across reboots until you fix this"
		echo "problem."
		echo
		echo "Fortunately, the fix for this problem is quite simple; all you need to"
		echo "do is pass the \"devfs=nomount\" boot option to the kernel (via GRUB"
		echo "or LILO) the next time you boot.  Then /dev will not be auto-mounted."
		echo "The next time you compile your kernel, be sure that you do not"
		echo "enable the \"Automatically mount filesystem at boot\" devfs kernel"
		echo "configuration option.  Then the \"devfs=nomount\" hack will no longer be"
		echo "needed."
		echo
		read -t 15 -p "(hit Enter to continue or wait 15 seconds...)" 
	else	
		mount -n /dev /dev-state -o bind
		mount -n -t devfs none /dev
		if [ -d /dev-state/compat ]
		then
			echo Copying devices from /dev-state/compat to /dev
			cp -ax /dev-state/compat/* /dev
		fi
	fi
	/sbin/devfsd /dev > /dev/null 2>&1
fi

exec /sbin/init.system $*
