check_static_dev () {
  echo  "checking if your static /dev is ok..."  &&
  mkdir  /tmp/$$.bindmount                       &&
  mount  --bind  ${INSTALL_ROOT}/  /tmp/$$.bindmount            &&
  cd  /tmp/$$.bindmount/dev                      &&
  if [ ! -c console -o ! -c null ]; then
    message "Ok, going to create!" &&
  # asked in CONFIGURE
  # if query "You don't seem to have a static /dev tree, should i create it ?" y
  # then
    create_group kmem &&
    create_group tty &&
    create_group ppp &&
    create_group lp &&
    create_group disk &&
    create_group floppy &&
    create_group audio &&
    # existing nodes should not make me fail
    $SCRIPT_DIRECTORY/MAKEDEV generic-nopty md || true &&
    mkdir  -p  shm  pts  &&
    mknod  initctl  p || true
  # fi
  else
  echo "seems ok"
  fi                         &&
  cd  /                      &&
  umount  /tmp/$$.bindmount  &&
  rmdir  /tmp/$$.bindmount
}

mkdep()
{
  local file="$RD/%$1/runlevel.config"  &&

  rm -f $file  &&

  if [ "$2" ] ; then
    echo "Making runlevel $1 depend on runlevel $2"
    echo "NEEDS=%$2" | cp /dev/stdin $file
  else
    echo "NEEDS=" | cp /dev/stdin $file
  fi
}

pinfo() {
  echo -e "\n${SPELL_COLOR}${*}${DEFAULT_COLOR}\n"
}

FD=$SCRIPT_DIRECTORY/files    # Files Directory
ID=${INSTALL_ROOT}/etc/init.d                # Init Dir
RD=${INSTALL_ROOT}/etc/init.d/runlevels      # Runlevels Dir

if  [[ "$DEVICES" != "devfs" ]] && [[ "$CREATE_DEV_TREE" == y ]]; then
  check_static_dev
fi  &&

pinfo "Creating directories..."  &&

mkdir  -pv  $RD/%DEV $RD/%S  $RD/%1  $RD/%2  $RD/%3  $RD/%4  $RD/%5  &&
mkdir  -pv  ${INSTALL_ROOT}/etc/sysconfig                            &&

pinfo "Installing files..."                                 &&

install  -v  -m 644  $FD/auto_init        $ID/              &&
install  -v  -m 754  $FD/rc               $ID/              &&
install  -v  -m 644  $FD/smgl_functions   $ID/              &&
install  -v  -m 644  $FD/smgl_init        $ID/              &&
install  -v  -m 644  $FD/smgl_telinit     $ID/              &&
install  -v  -m 644  $FD/smgl_runlevel    $ID/              &&

# Some non-directory based runlevels
# note, these are stored as _<script-name> and 
#  copied to %<script-name>
install  -v  -m 754  $FD/_default         $RD/%default      &&
install  -v  -m 754  $FD/_rescue          $RD/%rescue       &&
install  -v  -m 754  $FD/_single          $RD/%single       &&

pinfo "Installing facility, shutdown, and init configfiles..."  &&

_init_install_conf_files facilities shutdown init           &&

pinfo "Creating runlevel dependency files..."               &&

mkdep DEV
mkdep S DEV                                                 &&
mkdep 1 S                                                   &&
mkdep 2 1                                                   &&
mkdep 3 2                                                   &&
mkdep 4 3                                                   &&
mkdep 5 4                                                   &&

if ! grep -sq runlevels ${INSTALL_ROOT}/etc/inittab ; then
    pinfo "Installing ${INSTALL_ROOT}/etc/inittab..."                      &&
    install -v -m 644 $FD/inittab ${INSTALL_ROOT}/etc/inittab
fi                                                          &&

pinfo "Other stuff..."                                      &&

# Install MAKEDEV script
install -vm 755 "$SPELL_DIRECTORY/MAKEDEV" "$INSTALL_ROOT/sbin" &&

# Proper shutdown procedure
# TRACK_ROOT???
# ln -fsv ${TRACK_ROOT}/etc/sysconfig/shutdown ${INSTALL_ROOT}/etc/shutdown.conf  &&
ln -fsv sysconfig/shutdown ${INSTALL_ROOT}/etc/shutdown.conf  &&

# Yeah, I know
ln  -sfv  $TRACK_ROOT/bin/true  $RD/rc  &&

# LILO uses the auto runlevel when it doesn't encounter any user input
# path relative to be safer for chroots
ln  -sfv  %default              \
          ${INSTALL_ROOT}/etc/init.d/runlevels/%auto &&

# Creating /run
install -m 755 -d "$INSTALL_ROOT/run"
