#!/bin/sh
#
# * List of changes
#
# Timestamp                            Author                                                   Description
# --
# Mon Jul 10 17:53:21 2017 +0200       Luigi Scagnet - Exorint <luigi.scagnet@exorint.com>      BSP-685 merge Fastboot from unstable to stable
# Fri Jul 14 10:58:18 2017 +0200       Nicolò Ongaro <nicolo.ongaro@exorint.it>                 BSP-694 Avoid unsetting touch calibration after X is started
# Thu Dec 21 09:43:34 2017 +0100       Luigi Scagnet Exor Int <luigi.scagnet@exorint.com>       BSP-941 Udev bug, start fails in this new version.

### BEGIN INIT INFO
# Provides:          udev
# Required-Start:    mountvirtfs
# Required-Stop:     
# Default-Start:     S
# Default-Stop:
# Short-Description: Start udevd, populate /dev and load drivers.
### END INIT INFO

export TZ=/etc/localtime
export TMPDIR=/mnt/.psplash

[ -d /sys/class ] || exit 1
[ -r /proc/mounts ] || exit 1
[ -x /sbin/udevd ] || exit 1
[ -f /etc/default/udev-cache ] && . /etc/default/udev-cache
[ -f /etc/udev/udev.conf ] && . /etc/udev/udev.conf
[ -f /etc/default/rcS ] && . /etc/default/rcS

readfiles () {
   READDATA=""
   for filename in $@; do
	   if [ -r $filename ]; then
		   while read line; do
			   READDATA="$READDATA$line"
		   done < $filename
	   fi
   done
}

kill_udevd () {
    pid=`pidof -x udevd`
    [ -n "$pid" ] && kill $pid
    sleep 1
}

case "$1" in
  start)

    echo "Starting udev"

    # Cache handling.
    # A list of files which are used as a criteria to judge whether the udev cache could be reused.
    CMP_FILE_LIST="/proc/version /proc/cmdline /proc/devices /proc/atags"
    if [ "$DEVCACHE" != "" ]; then
            if [ -e $DEVCACHE ]; then
		    readfiles $CMP_FILE_LIST
		    NEWDATA="$READDATA"
		    readfiles /etc/udev/cache.data
		    OLDDATA="$READDATA"
		    if [ "$OLDDATA" = "$NEWDATA" ]; then
                            (cd /; tar xf $DEVCACHE > /dev/null 2>&1)
                            not_first_boot=1
                            [ "$VERBOSE" != "no" ] && echo "udev: using cache file $DEVCACHE"
                            [ -e /dev/shm/udev.cache ] && rm -f /dev/shm/udev.cache
                    else
			    # Output detailed reason why the cached /dev is not used
			    if [ "$VERBOSE" != "no" ]; then
				    echo "udev: udev cache not used"
				    echo "udev: we use $CMP_FILE_LIST as criteria to judge whether the cache /dev could be resued"
				    echo "udev: olddata: $OLDDATA"
				    echo "udev: newdata: $NEWDATA"
			    fi
			    echo "$NEWDATA" > /dev/shm/udev.cache
                    fi
	    else
		    if [ "$ETC_READ_ONLY" != "yes" ]; then
			    # If rootfs is not read-only, it's possible that a new udev cache would be generated;
			    # otherwise, we do not bother to read files.
			    readfiles $CMP_FILE_LIST
			    echo "$READDATA" > /dev/shm/udev.cache
		    fi
            fi
    fi

    if ( ! pidof udevd ); then
        # trigger the sorted events
        echo -e '\000\000\000\000' > /proc/sys/kernel/hotplug
        /sbin/udevd -d

        udevadm control --env=STARTUP=1
    fi

    # BSP-2592 do not restart video4linux because gstreamer may crash
    # JM-17711 do not restart input: physical button may stop responding
    if [ -z "$FASTBOOT" ]; then
        udevadm trigger --action=add --subsystem-match=input
        udevadm settle
    fi

    # Leave all other triggers in background
    udevadm trigger --action=add \
        --subsystem-match=usb \
        --subsystem-match=i2c \
        --subsystem-match=spi \
        --subsystem-match=iio \
        --subsystem-match=hwmon \
        --subsystem-match=block \
        --subsystem-match=leds \
        --subsystem-match=tty \
        --subsystem-match=mem \
        --subsystem-match=graphics \
        --subsystem-match=graphics_class \
        --subsystem-match=video \
        --subsystem-match=drm \
        --subsystem-match=gpu_class \
        --subsystem-match=misc \
        --subsystem-match=gpio \
        --subsystem-match=rtc \
        --subsystem-match=platform

    # Since video4linux triggers are disabled we need to manually
    # fix permissions of related devices
    for v4ldev in /dev/video[0-9]*; do
        [ -e "$v4ldev" ] || continue
        chgrp video $v4ldev
        chmod 660 $v4ldev
    done

    ;;
  stop)
    echo "Stopping udevd"
    start-stop-daemon --stop --name udevd --quiet
    ;;
  restart)
    $0 stop
    sleep 1
    $0 start
    ;;
  status)
    pid=`pidof -x udevd`
    if [ -n "$pid" ]; then
	echo "udevd (pid $pid) is running ..."
    else
	echo "udevd is stopped"
    fi
    ;;
  *)
    echo "Usage: $0 {start|stop|status|restart}"
    exit 1
esac
exit 0

