#!/bin/sh -e
export PATH=/usr/bin:/bin:/usr/sbin:/sbin
DEV_TMPFS_SIZE="10M"
DEVROOT="/dev"

# we need to unmount /dev/pts/ and remount it later over the tmpfs
unmount_devpts() {
  if grep ' /dev/pts ' /proc/mounts >/dev/null 2>&1 ; then
    umount /dev/pts/
  fi

  if grep ' /dev/shm ' /proc/mounts >/dev/null 2>&1 ; then
    umount /dev/shm/
  fi
}

# mount a tmpfs over /dev, if somebody did not already do it
mount_tmpfs() {
  if grep ' /dev ' /proc/mounts >/dev/null 2>&1 ; then
    return
  fi

  mkdir -p /usr/lib/udev/devices

  cp -rf /dev/* /usr/lib/udev/devices/ 2>/dev/null || true

  if ! mount -n -o size=$DEV_TMPFS_SIZE,mode=0755 -t tmpfs tmpfs /dev; then
    echo "udev requires tmpfs support, not started."
    exit 1
  fi

  cp -rf /usr/lib/udev/devices/* /dev/

  # relabel the new tmpfs accordingly
  [ -x /sbin/restorecon ] && /sbin/restorecon /dev

  return 0
}

create_dev_makedev() {
  if [ -e /sbin/MAKEDEV ]; then
    ln -sf /sbin/MAKEDEV /dev/MAKEDEV
  else
    ln -sf /bin/true /dev/MAKEDEV
  fi
}

# shell version of /usr/bin/tty
my_tty() {
  [ -x /bin/readlink ] || return 0
  [ -e /proc/self/fd/0 ] || return 0
  readlink --silent /proc/self/fd/0 || true
}

warn_if_interactive() {
  if [ "$RUNLEVEL" = "S" -a "$PREVLEVEL" = "N" ]; then
    return
  fi

  TTY=$(my_tty)
  if [ -z "$TTY" -o "$TTY" = "/dev/console" -o "$TTY" = "/dev/null" ]; then
    return
  fi

  printf "\n\n\nIt has been detected that the command\n\n\t$0 $*\n\n"
  printf "has been run from an interactive shell.\n"
  printf "It will probably not do what you expect, so this script will wait\n"
  printf "60 seconds before continuing. Press ^C to stop it.\n"
  printf "RUNNING THIS COMMAND IS HIGHLY DISCOURAGED!\n\n\n\n"
  sleep 60
}

create_dev_root_rule() {
  local udevroot="$1"
  [ -e $udevroot/rules.d/61-dev-root-link.rules ] && return 0

  eval $(udevadm info --export --export-prefix=ROOT_ --device-id-of-file=/ \
    || true)
  [ "$ROOT_MAJOR" -a "$ROOT_MINOR" ] || return 0

  echo 'ACTION=="add", SUBSYSTEM=="block", ENV{MAJOR}=="'$ROOT_MAJOR'", ENV{MINOR}=="'$ROOT_MINOR'", SYMLINK+="root"' \
    > $udevroot/rules.d/61-dev-root-link.rules
}

[ -x /usr/sbin/udevd ] || exit 0

if [ -e /etc/udev/udev.conf ]; then
  . /etc/udev/udev.conf
fi

if [ ! -e /proc/filesystems ]; then
  echo "udev requires a mounted procfs, not started."
  exit 1
fi

if [ ! -d /sys/class/ ]; then
  echo "udev requires a mounted sysfs, not started."
  exit 1
fi

if [ ! -e /sys/kernel/uevent_helper ]; then
  echo "udev requires hotplug support, not started."
  exit 1
fi


case "$1" in
    start)
    if [ -e "$DEVROOT/.udev/" ]; then
      if grep " $DEVROOT " /proc/mounts >/dev/null 2>&1 ; then
       TMPFS_MOUNTED=1
      else
       echo ".udev/ already exists on the static $DEVROOT!"
      fi
    else
      warn_if_interactive
    fi

    echo > /sys/kernel/uevent_helper

    if [ -z "$TMPFS_MOUNTED" ]; then
      [ -d /proc/1 ] || mount -n /proc
      unmount_devpts
      mount_tmpfs
    else
      # set the SELinux context for devices created in the initramfs
      [ -x /sbin/restorecon ] && /sbin/restorecon -R /dev
      # and clean up the database of the initramfs udev
      rm -rf /dev/.udev/
    fi

    # if this directory is not present /dev will not be updated by udev
    mkdir -p /dev/.udev/db/

    printf "Starting the hotplug events dispatcher "
    if ! udevd --daemon; then
      echo "Failed"
      exit $?
    else
      echo
    fi

    mkdir -p /dev/.udev/queue/ /dev/.udev/rules.d/
    create_dev_root_rule /dev/.udev/

    printf "Synthesizing the initial hotplug events"
    if ! udevadm trigger; then
      echo "Failed"
      exit $?
    else
      echo 
    fi

    create_dev_makedev

    # wait for the udevd childs to finish
    printf "Waiting for /dev to be fully populated"
    if ! udevadm settle; then
      echo 'timeout'
    else
      echo
    fi

    # make sure all is mounted
    mount -a
    ;;

    stop)
    printf "Stopping the hotplug events dispatcher "
    if [ -n "$(pidof udevd 2>/dev/null)" ] ; then
        if ! kill $(pidof udevd 2>/dev/null); then
            echo "Failed"
            exit 1
        else
            echo
        fi
    else
        echo "Is udev running?"
    fi
    ;;

    restart)
      $0 stop
      $0 start
    ;;

    reload|force-reload)
      udevadm control --reload_rules
    ;;

    *)
    echo "Usage: /etc/init.d/udev {start|stop|restart|reload|force-reload}"
    exit 1
    ;;
esac

exit 0

