#!/bin/sh

# First, determine our slackware kernel name:
for ELEMENT in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ; do
  if $(cat /proc/cmdline | cut -f $ELEMENT -d ' ' | grep -q SLACK_KERNEL) ; then
    SLACK_KERNEL=$(cat /proc/cmdline | cut -f $ELEMENT -d ' ' | cut -f 2 -d =)
  fi
done
unset ELEMENT

# Next, find the kernel's release version:
VERSION=$(uname -r | tr - _)

# If someone tries to install kernels from a CD that doesn't contain any,
# we'll give them one chance to find a disc that does.
swapdisks() {
  CDDEVICE=$(cat $TMP/SeTCDdev)
  umount $CDDEVICE 1> /dev/null 2> /dev/null
  eject $CDDEVICE
  dialog --title "REINSERT KERNEL DISC" --msgbox \
"Please reinsert the Slackware disc containing the collection \
of Linux kernels.  Usually this is disc number one (the disc \
that you boot from).  Once you've inserted the disc, hit ENTER \
to continue." \
8 61
  mount $CDDEVICE /var/log/mount 1> /dev/null 2> /dev/null
}

( cd boot
  if [ "$SLACK_KERNEL" = "generic.s" ]; then
    if [ -r vmlinuz-generic-$VERSION ]; then
      ln -sf vmlinuz-generic-$VERSION vmlinuz
      ln -sf config-generic-$VERSION config
      ln -sf System.map-huge-$VERSION System.map
    fi
  elif [ "$SLACK_KERNEL" = "huge.s" ]; then
    if [ -r vmlinuz-huge-$VERSION ]; then
      ln -sf vmlinuz-huge-$VERSION vmlinuz
      ln -sf config-huge-$VERSION config
      ln -sf System.map-huge-$VERSION System.map
    fi
  elif [ "$SLACK_KERNEL" = "hugesmp.s" ]; then
    if [ -r vmlinuz-huge-smp-$VERSION ]; then
      ln -sf vmlinuz-huge-smp-$VERSION vmlinuz
      ln -sf config-huge-smp-$VERSION config
      ln -sf System.map-huge-smp-$VERSION System.map
    fi
  elif [ "$SLACK_KERNEL" = "gensmp.s" ]; then
    if [ -r vmlinuz-generic-smp-$VERSION ]; then
      ln -sf vmlinuz-generic-smp-$VERSION vmlinuz
      ln -sf config-generic-smp-$VERSION config
      ln -sf System.map-generic-smp-$VERSION System.map
    fi
  elif [ "$SLACK_KERNEL" = "speakup.s" ]; then
    # This assumes symlinks /nfs and /cdrom both pointing to /var/log/mount:
    if $(mount | grep -q "type nfs") ; then
      PLINK=nfs
    else
      PLINK=cdrom
    fi
    if [ ! -d /$PLINK/kernels -a ! -r $TMP/SeTCDdev ]; then
      dialog --title "ERROR" --msgbox "Sorry, but the directory /$PLINK/kernels \
was not found.  Are you installing from a Slackware CD?" \
0 0
      break;
    else
      if [ $PLINK = cdrom -a ! -d /$PLINK/kernels ]; then
        swapdisks
      fi
    fi
    # flunk
    if [ ! -d /$PLINK/kernels ]; then
      break; # we've done all we can do -- grab a body bag.
    fi
    rm -f $T_PX/boot/vmlinuz $T_PX/boot/config $T_PX/boot/System.map
    cp -a /$PLINK/kernels/$SLACK_KERNEL/bzImage $T_PX/boot/vmlinuz-$SLACK_KERNEL-$VERSION
    cp -a /$PLINK/kernels/$SLACK_KERNEL/config $T_PX/boot/config-$SLACK_KERNEL-$VERSION
    cp -a /$PLINK/kernels/$SLACK_KERNEL/System.map.gz $T_PX/boot
    ( cd $T_PX/boot
      gzip -d System.map.gz
      mv System.map System.map-$SLACK_KERNEL-$VERSION
      ln -sf vmlinuz-$SLACK_KERNEL-$VERSION vmlinuz
      ln -sf config-$SLACK_KERNEL-$VERSION config
      ln -sf System.map-$SLACK_KERNEL-$VERSION System.map
    )
  fi
)

# and after all that hard work
