#!bin/sh
#
# start_udev
#
# script to initialize /dev by using udev.
#
# Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com>
#
# Released under the GPL v2 only.
#
# This needs to be run at the earliest possible point in the boot 
# process.
#
# Based on the udev init.d script
#
# Thanks go out to the Gentoo developers for proving 
# that this is possible to do.
#
# Yes, it's very verbose, feel free to turn off all of the echo calls,
# they were there to make me feel better that everything was working
# properly during development...
#
# don't use udev if sysfs is not mounted.

sysfs_dir=/sys

export TZ=/etc/localtime

[ -d $sysfs_dir/class ] || exit 1
[ -r /proc/mounts ] || exit 1
[ -x /sbin/udevd ] || exit 1
[ -f /etc/udev/udev.conf ] && . /etc/udev/udev.conf
udev_root=${udev_root-/dev}

if [ -f /dev/.in_sysinit ]; then
	exec >/dev/console 2>/dev/console </dev/console
fi


#. /etc/init.d/functions
#. /etc/sysconfig/udev

#some conf :)
# generates cache shell files of what MAKEDEV would do.
UDEV_USE_MAKEDEV_CACHE="no"

# Generates /dev/disk/by* symlinks.
UDEV_PERSISTENT_STORAGE="no"

prog=udev
bin=/sbin/udev
udevd=/sbin/udevd
MAKEDEV="/sbin/MAKEDEV"
DEVICETABLE_TXT="/devicetable.txt"
DEVICETABLE_MODE=0644

# Check SELinux status
#selinuxfs="$(fstab_decode_str `LC_ALL=C awk '/ selinuxfs / { print $2 }' /proc/mounts`)"
#SELINUX_STATE=
#if [ -n "$selinuxfs" ] && [ "`cat /proc/self/attr/current`" != "kernel" ]; then
#        if [ -r "$selinuxfs/enforce" ] ; then
#                SELINUX_STATE=`cat "$selinuxfs/enforce"`
#        else
#                # assume enforcing if you can't read it
#                SELINUX_STATE=1
#        fi
#fi



xargs_simple () {
	if [ "$1" = "-n" ]; then
		shift
		MAXNR="$1"
		shift
	else
		MAXNR=100
	fi
	NR=$MAXNR
	ARGS=""
	[ -z "$1" ] && set echo

	while read line; do
		if [ $NR -gt 0 ]; then
        		ARGS="$ARGS $line"
	        	NR=$(expr $NR - 1)
		else
        		"$@" $ARGS
	        	NR=$MAXNR
		        ARGS="$line"
		fi
	done
	if [ -n "$ARGS" ]; then
		"$@" $ARGS
	fi 
}

make_extra_nodes () {
	ln -snf /proc/self/fd $udev_root/fd
	ln -snf /proc/self/fd/0 $udev_root/stdin
	ln -snf /proc/self/fd/1 $udev_root/stdout
	ln -snf /proc/self/fd/2 $udev_root/stderr
	ln -snf /proc/kcore $udev_root/core

	[ -d $udev_root/pts ] || mkdir -m 0755 $udev_root/pts
	[ -d $udev_root/shm ] || mkdir -m 0755 $udev_root/shm
	[ -f /dev/MAKEDEV ] || ln -s $MAKEDEV /dev/MAKEDEV;

	[ -d /dev/.udev/makedev.d ] || mkdir -p /dev/.udev/makedev.d
	USE_MD5="false"
	[ -x /usr/bin/md5sum -a "$UDEV_USE_MAKEDEV_CACHE" == "yes" ] && USE_MD5="true"

#
#	#echo "iCreate Static Nodes:"
#	if [ -f "$STATIC_LIST" ]
#	then
#
#	(cat $STATIC_LIST) | while read type major minor name
#	do
#        	case "$type" in
#                	\#*|"") continue ;;
#        	esac
#        
#        	if [ $type == "d" ]
#        	then
#                	mkdir /dev/$major
#        	else
#                	mknod /dev/$name $type $major $minor -m 664
#        	fi
#	done
#
#	else
#		echo "No Static Device List found on $STATIC_LIST"
#	fi

#	if [ -x "$MAKEDEV" ]; then
#		for i in /etc/udev/makedev.d/*.nodes; do
#			if [ -f "$i" ]; then 			   
#				# use a little caching to speedup things
#				if [ "$USE_MD5" == "true" ]; then
#					# fix for MAKEDEV shell scripts
#					[ -d /dev/net ] || mkdir -p /dev/net 
#					md5=$(/usr/bin/md5sum "$i"|(read a b; echo $a))-se$SELINUX_STATE
#					if [ -f "/var/lib/udev/makedev.d/${md5}.sh" ];then
#						md5file="/var/lib/udev/makedev.d/${md5}.sh"
#					else
#						md5file="/dev/.udev/makedev.d/${md5}.sh"
#					fi
#					if [ ! -f "$md5file" ]; then
#						( sed -e 's,#.*,,g' "$i" | \
#							xargs_simple -n 100 $MAKEDEV -x -a -S ) \
#							> "$md5file"
#					fi
#					. "$md5file" >/dev/null 2>&1
#				else
#						( sed -e 's,#.*,,g' "$i" | \
#							xargs_simple -n 100 $MAKEDEV -x )
#				fi
#			fi
#		done 
#	fi

	#
	# We dont need this for the moment :)
	# No specific devices added
	
	#for devdir in /etc/udev/devices /lib/udev/devices; do
	#	[ -d "$devdir" ] || continue
	#	pushd $devdir &> "$udev_root/null"
	#	set *
	#	if [ "$1" != "*" ]; then
	#		#echo "Warning: $devdir is deprecated. Please use /etc/udev/makedev.d/."
        #		cp -ar "$@" $udev_root/ 
	#		pushd "$udev_root" &> "$udev_root/null"
	#		[ -x /sbin/restorecon ] && /sbin/restorecon "$@" 
	#		popd &> "$udev_root/null"
	#	fi
	#	popd &> "$udev_root/null"
	#done
}

kill_udevd() {
	if [ -x /sbin/pidof ]; then
		pid=`/sbin/pidof -x udevd`
        	[ -n "$pid" ] && kill $pid
	fi
}

findalias () {
	local n
	for n in "$1"/* ; do
		[ -h "$n" ] && continue
		[ -d "$n" ] && { findalias "$n" ; continue; }
		[ "${n##*/}" == "modalias" ] && echo $(cat $n)
	done
}

wait_for_queue() {
        local timeout=${1:-0}
	local ret=0
	if [ $timeout -gt 0 ]; then
	    /sbin/udevsettle --timeout=$timeout
        else
	    /sbin/udevsettle
	fi
	ret=$?
	if [ $ret -ne 0 ]; then
		echo -n "Wait timeout. Will continue in the background."
	fi
	return $ret;
}

create_device() {
    # this function is based on interpret_table_entry from devtable.c of ubifs
    local -
    local line path name type mode uid gid major minor start inc count
    # type can be (from devtable.c of mkfs.ubifs):
    #   f	a regular file
    #   d	a directory
    #   c	a character device file
    #   b	a block special device file
    #   p	a fifo named pipe

    NEWDEVROOT=${1:-$NEWDEVROOT}
    while read line; do
        : "${attr}line: $line${default}"
        # disregard comments and blank lines
        #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        case "$line" in
            /|[^/]*|'#'*) : "${attr}comment line${default}"
            continue
            ;;
        esac
        if [[ -z "${line//[[:blank:]]}" ]]; then
            : "${attr}blank line${default}"
            continue
        fi
        # parse  & check line items
        #~~~~~~~~~~~~~~~~~~~~~~~~~~
        set $line
        name="$1" shift
        set ${*//-/0}
        type="$1" mode=$((0$2)) uid="$3" gid="$4" major="$5" minor="$6" start="$7" inc="$8" count="$9"

        # ignore line where name does not start with "/"
        path=${name%/*} name=${name##*/}
        case "$path/" in
            *//*|/./|/../) : "'//' or '.' or '..' cannot be used in the path !!!"
                continue
            ;;
        esac
        case "$name" in
            */) : "cannot end with '/' !!!"
                continue
            ;;
        esac

        # count cannot be 0 if increment is not 0
        if [[ $inc -ne 0 ]] && [[ $count -eq 0 ]]; then
            : "inc!=0 but count is !!!"
            continue
        fi

        # ignore type file
        [[ "$type" == "f" ]] && continue

        # create the node
        #~~~~~~~~~~~~~~~~
        mode=$(printf "%#o" $mode)
        if [[ $type == d ]]; then
            : "directory"
            mkdir -m $mode $NEWDEVROOT/$path/$name
            continue
        fi
        [[ ! -d $NEWDEVROOT/$path ]] && mkdir -m 0755 -p $NEWDEVROOT/$path
        [[ $count -eq 0 ]] && count=1 && start=
        while [[ $count -gt 0 ]]; do
            mknod $NEWDEVROOT/$path/$name$start $type $major $minor -m $mode
            start=$((start+inc)) minor=$((minor+inc)) count=$((count-1))
        done
    done
}

export ACTION=add
prog=udev
ret=0
STRING="Starting $prog: "
# propagate $udev_root from /sys
echo "$STRING"

# update /dev on the file system with the content of devicetable.txt
if [ -s $DEVICETABLE_TXT ]; then
    NEWDEVROOT=${NEWDEVROOT:-/tmp}
    DEV=${DEV:-/dev}
    # devicetable.txt does exist and is not null !!!
    create_device /tmp < $DEVICETABLE_TXT
    # generate list of dev from the current one and from the  new device table
    curdir=$(pwd)
    cd $DEV; find . -exec 'ls' '-ald' '{}' ';' | awk 'NF==10{dev=$NF;$7=$8=$9=$NF="";printf "%-60s %s\n",dev,$0};NF==9{dev=$NF;$5=$6=$7=$8=$NF="";printf "%-60s %s\n",dev,$0}' | sort > /tmp/devlist &
    cd $NEWDEVROOT/dev; find . -exec 'ls' '-ald' '{}' ';' | awk 'NF==10{dev=$NF;$7=$8=$9=$NF="";printf "%-60s %s\n",dev,$0};NF==9{dev=$NF;$5=$6=$7=$8=$NF="";printf "%-60s %s\n",dev,$0}' | sort > /tmp/devlist.new &
    wait
    # compare the list to retrieve the ones to and and the ones to remove
    cd /tmp
    rm -f devtoadd devtodel 2>/dev/null
    diff -bN -U0 /tmp/devlist /tmp/devlist.new > devdiff

    # merge both dirs
    while read line; do
        set -- $line
        dev=${1#[+-].}
        case "$1" in
            -.*) : "REMOVE ($dev)"
                rm -rf $DEV/$dev
            ;;
            +.*) : "ADD ($dev)"
                cp -a $NEWDEVROOT/dev/$dev $DEV/$dev
            ;;
        esac
    done < devdiff

    rm devdiff devlist devlist.new
    cd $curdir

    # empty DEVICETABLE_TXT
    cp /dev/null $DEVICETABLE_TXT
fi

# mount the tmpfs on ${udev_root%/}, if not already done
LANG=C awk "\$2 == \"${udev_root%/}\" && \$3 == \"tmpfs\" { exit 1 }" /proc/mounts && {
	if LANG=C fgrep -q "none ${udev_root%/}/pts " /proc/mounts; then
		PTSDIR=$(mktemp -d)
		mount --move $udev_root/pts "$PTSDIR"
	fi
	if LANG=C fgrep -q "none ${udev_root%/}/shm " /proc/mounts; then
		SHMDIR=$(mktemp -d)
		mount --move $udev_root/shm "$SHMDIR"
	fi
        if [ ! -d ${udev_root%/}/.new_dev ]; then
                mount -n -o mode=0755 -t tmpfs none "$udev_root"
        else
                mount -n -o mode=0755 -t tmpfs none "${udev_root%/}/.new_dev"
                cp -a ${udev_root%/}/* ${udev_root%/}/.new_dev
                mount --move ${udev_root%/}/.new_dev  $udev_root
        fi	
	mkdir -m 0755 $udev_root/pts
	mkdir -m 0755 $udev_root/shm
	if [ -n "$PTSDIR" ]; then
		mount --move "$PTSDIR" $udev_root/pts
		rmdir "$PTSDIR"
	fi
	if [ -n "$SHMDIR" ]; then
		mount --move "$SHMDIR" $udev_root/shm
		rmdir "$SHMDIR"
	fi
	
	ret=$(expr $ret + $?)
}

# returns OK if $1 contains $2
strstr() {
  [ "${1#*$2*}" = "$1" ] && return 1
  return 0
}

getval() {
    what=$1
    shift
    for arg; do 
	if strstr "$arg" "$what="; then
	    val=${arg#${what}=*}
	    echo $val
	    return 0
	fi
    done
    return 1
}

make_extra_nodes
cmdline=$(cat /proc/cmdline)
kill_udevd > "$udev_root/null" 2>&1
rm -fr $udev_root/.udev > "$udev_root/null" 2>&1
UDEV_OPTS=""
if [ -f "/sys/class/tty/console/uevent" ]; then
	# trigger the sorted events
	echo -e '\000\000\000\000' > /proc/sys/kernel/hotplug

	/sbin/udevd -d
	ret=$(expr $ret + $?)

	udevtimeout=$(getval udevtimeout $cmdline)

	if strstr "$cmdline" udevdebug; then
		/sbin/udevcontrol log_priority=debug
	fi
	if strstr "$cmdline" udevinfo; then
		/sbin/udevcontrol log_priority=info
	fi
	if strstr "$cmdline" udevtrace; then
		UDEV_OPTS="$UDEV_OPTS --debug-trace"
	fi
	if strstr "$cmdline" udevchilds; then
		/sbin/udevcontrol max_childs_running=$(getval udevchilds $cmdline)
	fi

	if [ "$UDEV_PERSISTENT_STORAGE" == "no" ]; then
            /sbin/udevcontrol env UDEV_NO_PERSISTENT_STORAGE=1
	fi	  

	if strstr "$cmdline" udevnopersist; then
            /sbin/udevcontrol env UDEV_NO_PERSISTENT_STORAGE=1
	fi	  

        /sbin/udevcontrol env STARTUP=1

	if strstr "$cmdline" modprobedebug; then
		/sbin/udevcontrol env MODPROBE_OPTIONS="-s -v -q"
		echo
		findalias /sys | while read modules ; do
			if [ -n "$modules" ]; then
				/sbin/modprobe -a -v -q $modules
				wait_for_queue $udevtimeout
			fi
		done
		echo "Triggering Rest"
	fi

	/sbin/udevtrigger
	ret=$(expr $ret + $?)
	wait_for_queue $udevtimeout
	ret=$( expr $ret + $?)
	wait
	/sbin/udevcontrol env STARTUP=
else
	echo -n " kernel too old for this udev version "
	/sbin/udevd -d $UDEV_OPTS
	ret=10

fi

ret=$(expr $ret + $?)
#[ $ret -eq 0 ] && success $"$STRING" || failure $"$STRING"

[ $ret -eq 0 ] && echo "Starting Udev Success" || echo "Starting Udev Failure"
exit 0

