#! /bin/sh
#
# Author: Rolf Eike Beer <eike-hotplug@sf-tec.de>
#   derived from original RedHat udev init script
#   based on the SuSE 9.0 template (c) 1995-2002 SuSE Linux AG
#
# /etc/init.d/udev
#   and its symbolic link
# /(usr/)sbin/rcudev
#
# System startup script for udev
#
# LSB compatible service control script; see http://www.linuxbase.org/spec/
# 
### BEGIN INIT INFO
# Provides:          udev
# Required-Start:    
# Required-Stop:     
# Default-Start:     1 2 3 5
# Default-Stop:      0 6
# Short-Description: manage user-space device nodes in /udev
# Description:       Start udev to create the device files for all
#		     devices already present in system when script is
#		     called. All other devices files will be automatically
#		     created when udev is called via /sbin/hotplug.
#		     Requires at least a kernel 2.6 to work properly.
### END INIT INFO
# 
# Note on script names:
# http://www.linuxbase.org/spec/refspecs/LSB_1.2.0/gLSB/scrptnames.html
# A registry has been set up to manage the init script namespace.
# http://www.lanana.org/
# Please use the names already registered or register one or use a
# vendor prefix.


# Check for missing binaries (stale symlinks should not happen)
UDEV_PROG=systemd-udevd
UDEV_BIN=/lib/systemd/$UDEV_PROG
UDEV_STATIC_ARCHIVE=/lib/udev/devices/static.tar.gz
test -x $UDEV_BIN || exit 5

# Check for existence of needed config file and read it
UDEV_CONFIG=/etc/udev/udev.conf
test -r $UDEV_CONFIG || exit 6
. $UDEV_CONFIG

# Directory where sysfs is mounted
SYSFS_DIR=/sys

# Source LSB init functions
#. /lib/lsb/init-functions
. /etc/sysconfig/rc
. $rc_functions

case "$1" in
    start)
	if [ ! -d $SYSFS_DIR ]; then
		log_failure_msg "${0}: SYSFS_DIR \"$SYSFS_DIR\" not found"
		exit 1
	fi
	if [ ! -d $udev_root ]; then
		mkdir $udev_root || exit 4
	fi
	echo "" > /proc/sys/kernel/hotplug

	echo -n "Starting udev: "
        if [ ! "`grep ' /dev ' /proc/mounts`" ]; then
           [ "`grep devtmpfs /proc/filesystems`" ] && \
              mount -n -t devtmpfs udev /dev || \
              mount -n -t tmpfs udev /dev
        fi
        if [ ! "`grep ' /run ' /proc/mounts`" ]; then
              mount -n -t tmpfs tmpfs /run
        fi
	[ -e /dev/shm ] || {
	   mkdir /dev/shm /dev/pts
	   chmod 1777 /dev/shm
	}
	[ -e $UDEV_STATIC_ARCHIVE ] && {
	   STATIC_INIT=1
	   (cd /
	    tar xzf $UDEV_STATIC_ARCHIVE)
	} || [ -x /lib/udev/devices/MAKEDEV ] && /lib/udev/devices/MAKEDEV

	$UDEV_BIN --daemon &>/dev/null
        evaluate_retval
	echo
	
	[ "$STATIC_INIT" ] || {

	echo -n "Plugging devices: "
        udevadm trigger
	udevadm settle
	evaluate_retval
        echo
	}
	;;
    stop)
	echo -n "Stopping udev: "
	killall $UDEV_PROG
        success
        echo
	;;
    restart|force-reload)
	$0 stop  &&  $0 start
	exit $?
	;;
    reload)
	exit 3
	;;
    status)
	echo -n "Checking for udev root directory: "
  	if [ -d $udev_root ]; then
		#log_success_msg "found"
                echo_success found
                echo
	else
		#log_warning_msg "not found"
                warning "not found"
                echo
		exit 3
	fi
	;;
    savedevices)
        echo -n "Creating static devices archive: "
	(cd /
	 tar czf $UDEV_STATIC_ARCHIVE /dev)
        evaluate_retval
	echo
        ;;
    *)
	echo "Usage: $0 {start|stop|status|restart|force-reload|reload|savedevices}"
	exit 1
	;;
esac