#!/bin/sh

# This is a function to unload the USB mouse/keyboard modules:
usb_kbd_down() {
  modprobe -r keybdev mousedev usbmouse hid usbkbd input uhci usb-uhci usb-ohci uhci-hcd ohci-hcd ehci-hcd 2> /dev/null
  umount usbfs 2> /dev/null
  modprobe -r usbcore 2> /dev/null
}

# This is a function to attempt to enable a USB keyboard.
# If this causes problems for you, use "nousb" as a kernel
# command line option at boot time.
usb_kbd_up() {
  # If nousb was given at boot, skip.
  if ! cat /proc/cmdline | grep nousb 1> /dev/null 2> /dev/null ; then
    # If there aren't even any modules for this kernel, skip.
    if [ -d /lib/modules/`uname -r` ]; then
      # If usbcore is already loaded, skip.
      if ! grep usbcore /proc/modules 1> /dev/null 2> /dev/null ; then
        modprobe -q usbcore >/dev/null 2>&1
        # Try to mount usbfs:
        if [ -d /proc/bus/usb -a ! -f /proc/bus/usb/devices ]; then
          mount -t usbfs usbfs /proc/bus/usb
        fi
        # Try to load all the hub modules:
        modprobe -q ehci-hcd >/dev/null 2>&1
        modprobe -q ohci-hcd >/dev/null 2>&1
        modprobe -q uhci-hcd >/dev/null 2>&1
        modprobe -q usb-ohci >/dev/null 2>&1
        # NOTE: this prefers "usb-uhci"; you may prefer "uhci".
        modprobe -q usb-uhci >/dev/null 2>&1 || modprobe -q uhci >/dev/null 2>&1
        # modprobe -q uhci >/dev/null 2>&1 || modprobe -q usb-uhci >/dev/null 2>&1
        # Load input core:
        modprobe -q input >/dev/null 2>&1
        # Load USB keyboard:
        modprobe -q usbkbd >/dev/null 2>&1
        # Load Human Interface Device (HID) USB module:
        modprobe -q hid >/dev/null 2>&1
        # Load mouse (just in case (TM)) and keyboard USB input modules:
        modprobe -q mousedev >/dev/null 2>&1
        modprobe -q keybdev >/dev/null 2>&1
        # Give these some time to register:
        sleep 2
        # Next, take a look to see if there are devices detected.
        # If not, unload the modules.
        if [ ! -r /proc/bus/usb/devices ]; then
          usb_kbd_down
          break
        elif cat /proc/bus/usb/devices | grep -i Driver=usb_mouse 1> /dev/null 2> /dev/null ; then
          # Looks like a mouse.
          break
        elif cat /proc/bus/usb/devices | grep -i Driver=hid 1> /dev/null 2> /dev/null ; then
          # Probably a keyboard (but could be a game pad :)
          break
        elif [ -d /proc/bus/usb/devices/001 ]; then # we're REALLY not picky here...
          break
        else
          # Nothing?  Fine, we'll give up.
          echo "No USB devices found.  Unloading USB subsystem..."
          usb_kbd_down
        fi
      fi
    fi
  fi
}

# remove /etc/mtab* so mount creates the /etc/mtab file
/bin/rm -f /etc/mtab* /etc/nologin

/bin/mount -av -t nonfs

/sbin/swapon -a 2> /dev/null

if [ -x /sbin/ldconfig ]; then
  /sbin/ldconfig -v
fi

## Detect serial console from kernel command line:
#if cat /proc/cmdline | grep console=ttyS 1> /dev/null 2> /dev/null ; then
#  SERIAL_CONSOLE="true"
#fi

# USB keyboard?
usb_kbd_up

# Load additional install floppies:
for NEWDISK in 2 ; do
  if [ ! -r /etc/disk${NEWDISK} ]; then
    while [ 0 ]; do
      echo
      echo -n "Insert install.${NEWDISK} floppy disk to be loaded into RAM disk and press ENTER"
      read readfoo;
      if [ "$readfoo" = "Q" -o "$readfoo" = "q" ]; then
        break;
      fi
      echo -n "Loading install.${NEWDISK} floppy into RAM disk...  "
      ( cd / ; cat /dev/fd0 | zcat 2> /dev/null | tar xf - )
      if [ -r /etc/disk${NEWDISK} ]; then
        echo "done."
        echo
        break;
      else
        echo "Error. (reload or enter Q)"
        echo
        continue;
      fi
    done
  fi
done

## USB keyboard (in case we missed it :)?
#usb_kbd_up

# Replace some startup binaries with better versions:
( cd /bin
  rm -f grep gzip tar gunzip zcat df
  ln -sf df.bin df
  ln -sf grep.bin grep
  ln -sf gzip.bin gzip
  ln -sf tar-1.13 tar
  ln -sf gzip gunzip
  ln -sf gzip zcat
)

# Make detected partitions:
/dev/makedevs.sh

if [ -x /etc/rc.d/rc.inet1 ]; then
  /bin/sh /etc/rc.d/rc.inet1
fi

/sbin/syslogd 2> /dev/null
sleep 1
/sbin/klogd -c 3 1> /dev/null

/bin/setterm -blank 0

echo
echo "<OPTION TO LOAD SUPPORT FOR NON-US KEYBOARD>"
echo
echo "If you are not using a US keyboard, you may now load a different"
echo "keyboard map.  To select a different keyboard map, please enter 1"
echo "now.  To continue using the US map, just hit enter."
echo
echo -n "Enter 1 to select a keyboard map: "
read ONE
if [ "$ONE" = "1" ]; then
  /usr/lib/setup/SeTkeymap
fi
clear

echo > /etc/motd
echo "`/bin/uname -a | /bin/cut -d\  -f1,3`." >> /etc/motd
echo >> /etc/motd
echo "If you're upgrading an existing Slackware system, you might want to" >> /etc/motd
echo "remove old packages before you run 'setup' to install the new ones. If" >> /etc/motd
echo "you don't, your system will still work but there might be some old files" >> /etc/motd
echo "left laying around on your drive." >> /etc/motd
echo >> /etc/motd
echo "Just mount your Linux partitions under /mnt and type 'pkgtool'. If you" >> /etc/motd
echo "don't know how to mount your partitions, type 'pkgtool' and it will tell" >> /etc/motd
echo "you how it's done." >> /etc/motd
echo >> /etc/motd
echo "To partition your hard drive(s), use 'cfdisk' or 'fdisk'." >> /etc/motd
echo "To activate PCMCIA/Cardbus devices needed for installation, type 'pcmcia'." >> /etc/motd
echo "To activate network devices needed for installation, type 'network'." >> /etc/motd
echo "To start the main installation, type 'setup'." >> /etc/motd
echo >> /etc/motd

# If possible, figure out what kernel we just booted with:
unset SLACK_KERNEL
for ARG in `cat /proc/cmdline` ; do
  if [ "`echo $ARG | cut -f 1 -d =`" = "SLACK_KERNEL" ]; then
    IMAGE="`echo $ARG | cut -f 2 -d =`"
    SLACK_KERNEL=$IMAGE
  fi 
done
export SLACK_KERNEL

# Fake login:  (fooled ya! ;^)

cat /etc/issue
echo -n "slackware login: "
read BOGUS_LOGIN
cat /etc/motd
