#!/bin/bash

PROGRAM=/bin/false
RUNLEVEL=DEV
ESSENTIAL=yes

. /etc/init.d/smgl_init
. /etc/sysconfig/devices

start_devfs()
{
  if ! [ -e /dev/.devfsd ]
  then echo "Mounting devfs on /dev"
       mount -n -t devfs devfs /dev
       evaluate_retval
  fi

  echo "Telling init to reopen file descriptors"
  kill -USR1 1

  if [ ! -x "/sbin/devfsd" ] ; then
    ln -sf /proc/self/fd /dev/fd
    ln -sf /dev/fd/0 /dev/stdin
    ln -sf /dev/fd/1 /dev/stdout
    ln -sf /dev/fd/2 /dev/stderr
  fi
}

start_udev()
{ (
  . /etc/udev/udev.conf
  echo "Mounting ramfs at $udev_root"
  mount -n -t ramfs none $udev_root
  # create some needed stuff
  ln -s /proc/self/fd $udev_root/fd
  ln -s /dev/fd/0 $udev_root/stdin
  ln -s /dev/fd/1 $udev_root/stdout
  ln -s /dev/fd/2 $udev_root/stderr
  mkdir $udev_root/shm
  mkdir $udev_root/pts

# strip out kernel sublevel and patchlevel so we can check against them
# since we don't support 1.x linux kernels then we don't have to check that
  PATCHLEVEL=`uname -r | cut -d. -f2`
  SUBLEVEL=`uname -r | cut -d. -f3`
  tmp=`uname -r | cut -d. -f3 | sed 's/^[0-9]*//g'`
  SUBLEVEL=${SUBLEVEL/$tmp/}

  # kernel 2.6.15-rc1 and higher don't use hotplug
  if [ $SUBLEVEL -ge 15 -a $PATCHLEVEL -ge 6 ]; then
    echo "don't need hotplug for this kernel"
    builtin echo > /proc/sys/kernel/hotplug
  else
      # Now uses udevsend as the hotplug multiplexer
    echo "Setting udevsend as hotplug agent"
    sysctl -w kernel.hotplug="/sbin/udevsend" > /dev/null 2>&1
    echo "Creating initial udev device nodes"
    /sbin/udevstart
  fi

  if [ -f /etc/udev/udev.missing ]; then
    echo "Processing /etc/udev/udev.missing"
    cd /dev
    while read line; do
      if [ "${line:0:1}" == "#" ]; then continue; fi
      for ((i=0; i<7; i++)); do
        val[$i]=${line%%:*}
        line=${line#*:}
      done
      if [ "${val[1]}" == "d" ]; then
        mkdir ${val[0]}
      else
        mknod ${val[0]} ${val[1]} ${val[2]} ${val[3]}
      fi
      chmod ${val[4]} ${val[0]}
      chown ${val[5]}:${val[6]} ${val[0]}
    done < /etc/udev/udev.missing
    cd /
  fi
  echo "Telling init to reopen file descriptors"
  kill -USR1 1
) }

start_static_udev()
{ (
  . /etc/udev/udev.conf
  # create some needed stuff - and insist on it
  if [ ! -d $udev_root/fd ]; then
    ln -fs /proc/self/fd $udev_root/fd
  fi
  ln -fs /dev/fd/0 $udev_root/stdin
  ln -fs /dev/fd/1 $udev_root/stdout
  ln -fs /dev/fd/2 $udev_root/stderr
  mkdir -p $udev_root/shm
  mkdir -p $udev_root/pts

  # strip out kernel sublevel and patchlevel so we can check against them
  # since we don't support 1.x linux kernels then we don't have to check that
  PATCHLEVEL=$(uname -r | cut -d. -f2)
  SUBLEVEL=$(uname -r | cut -d. -f3)
  tmp=$(uname -r | cut -d. -f3 | sed 's/^[0-9]*//g')
  SUBLEVEL=${SUBLEVEL/$tmp/}

  # kernel 2.6.15-rc1 and higher don't use hotplug
  if [ $SUBLEVEL -ge 15 -a $PATCHLEVEL -ge 6 ]; then
    echo "don't need hotplug for this kernel"
    builtin echo > /proc/sys/kernel/hotplug
  else
      # Now uses udevsend as the hotplug multiplexer
    echo "Setting udevsend as hotplug agent"
    sysctl -w kernel.hotplug="/sbin/udevsend" > /dev/null 2>&1
    echo "Creating initial udev device nodes"
    /sbin/udevstart
  fi

  if [ -f /etc/udev/udev.missing ]; then
    echo "Processing /etc/udev/udev.missing"
    cd /dev
    while read line; do
      if [ "${line:0:1}" == "#" ]; then continue; fi
      for ((i=0; i<7; i++)); do
        val[$i]=${line%%:*}
        line=${line#*:}
      done
      if [ "${val[1]}" == "d" ]; then
        mkdir -p ${val[0]}
      else
        mknod ${val[0]} ${val[1]} ${val[2]} ${val[3]}
      fi
      chmod ${val[4]} ${val[0]}
      chown ${val[5]}:${val[6]} ${val[0]}
    done < /etc/udev/udev.missing
    cd /
  fi
) }

start_static()
{
   exit 0
}

start()
{
  # mount proc
  echo "Mounting /proc"
  mount -n -t proc proc /proc
  # mount sys
  echo "Mounting /sys"
  mount -n -t sysfs sysfs /sys
  eval "start_$DEVICES"
}

stop() { exit 0; }
restart() { exit 3; }
reload() { exit 3; }
force_reload() { exit 3; }
status() { exit 3; }

usage()
{
  echo "Usage: $0 {start|stop}"
  echo "Warning: Do not run this script manually!"
}
