#!/bin/sh
#
# devfsd		This script handles the devfs startup, so that
#			permissions are set correctly and device symlinks
#			are available.
#

[ -x /sbin/devfsd ] || exit 0

. /etc/default/devfsd

if [ "$MOUNT" != "no" ]; then
	if [ "$1" = "start" -a ! -e $MOUNTPOINT/.devfsd ]; then
		echo "Mounting devfs on $MOUNTPOINT"
		mount -t devfs none $MOUNTPOINT
	fi
fi

[ -e $MOUNTPOINT/.devfsd ] || exit 0

[ -d /proc/1 ] || mount -n /proc

PATH=/bin:/usr/bin:/sbin:/usr/sbin
export PATH
# skip files ending in ~ (backups) and files containing ".dpkg-" (files created
# by dpkg in the process of upgrades).
if [ -f /etc/devfs/symlinks.list ]; then
  LINKFILES=`grep -v ^# /etc/devfs/symlinks.list 2>/dev/null`
fi
if [ -f /etc/devfs/devices.list ]; then
  DEVFILES=`grep -v ^# /etc/devfs/devices.list 2>/dev/null`
fi

case "$1" in
  make_links)
	## set up symlinks
	cd $MOUNTPOINT
	if [ "$LINKFILES" != "" ]; then
		echo ""
		echo "/etc/devfs/symlinks and /etc/devfs/symlinks.d/* use is deprecated."
		echo "It will be removed in a future version."
		echo "See devfsd(8) for how to achieve the same objectives more effectively"
		echo "through /etc/devfs/devfsd.conf, or ask russell@coker.com.au for advice."
		echo ""
		echo -n "Setting up symlinks in $MOUNTPOINT..."
		OLDIFS="$IFS"
		IFS='\
'
		for i in `sed -e '/^#/d' $LINKFILES 2>/dev/null`; do
			IFS="$OLDIFS"
			eval set -- $i
			[ -e $2 ] && continue
			ln -sf $1 $2
		done
		echo "done.    "
	fi
	if [ "$DEVFILES" != "" ]; then
		echo -n "Creating extra device nodes..."
		OLDIFS="$IFS"
		IFS='\
'
		for i in `sed -e '/^#/d' $DEVFILES 2>/dev/null`; do
			IFS="$OLDIFS"
			eval set -- $i
			if [ ! -e $1 ]; then
				if [ "$2" = "d" ]; then
					mkdir $1
				else
					mknod $1 $2 $3 $4
				fi
			fi
			if [ "$5.$6" != "." ]; then
				chown $5.$6 $1
			fi
			if [ "$7" != "" ]; then
				chmod $7 $1
			fi
		done
		echo "done.  "
	fi
	;;
  start)
	$0 make_links
	start-stop-daemon --start --exec /sbin/devfsd -- $MOUNTPOINT
	;;
  stop)
	PID=`pidof /sbin/devfsd`
	echo -n "Stopping devfsd: "
	if [ $PID ]; then
		start-stop-daemon --stop --quiet --exec /sbin/devfsd || kill $PID
		echo -n "done."
	else
		echo -n "devfsd not running."
	fi
	echo ""
	;;
  restart|force-reload)
	$0 stop
	sleep 1
	$0 start
	;;
  reload)
	$0 make_links
	PID=`pidof /sbin/devfsd`
	echo -n "Reloading devfsd: "
	if [ $PID ]; then
		kill -1 $PID
		echo "done."
	else
		echo "devfsd not running."
	fi
	;;
  *)
	echo "Usage: /etc/init.d/devfsd {start|stop|restart|reload|force-reload}" >&2
	exit 1
	;;
esac

exit 0
