#!/bin/sh
TMP=/var/log/setup/tmp
if [ ! -d $TMP ]; then
  mkdir -p $TMP
fi
T_PX="`cat $TMP/SeTT_PX`"
ROOT_DEVICE="`cat $TMP/SeTrootdev`"
if [ ! -d $T_PX/bin -a ! -d $T_PX/etc ]; then # if there no Linux here, exit
 exit
fi
# These next lines are intended to clean up any permissions
# problems that could be caused by a bad package.  We don't
# intend to make any bad packages here, but it never hurts
# to be safe. :^)
( cd $T_PX ; chmod 755 ./ )
( cd $T_PX ; chmod 755 ./var )
if [ -d $T_PX/usr/src/linux ]; then
 chmod 755 $T_PX/usr/src/linux
fi
if [ ! -d $T_PX/proc ]; then
 mkdir $T_PX/proc
 chown root.root $T_PX/proc
fi
chmod 1777 $T_PX/tmp
if [ ! -d $T_PX/var/spool/mail ]; then
 mkdir -p $T_PX/var/spool/mail
 chmod 755 $T_PX/var/spool
 chown root.mail $T_PX/var/spool/mail
 chmod 1777 $T_PX/var/spool/mail
fi 

# Load keyboard map (if any) when booting
if [ -r $TMP/SeTkeymap ]; then
 MAPNAME="`cat $TMP/SeTkeymap`"
 echo "#!/bin/sh" > $T_PX/etc/rc.d/rc.keymap
 echo "# Load the keyboard map.  More maps are in /usr/lib/kbd/keytables." \
 >> $T_PX/etc/rc.d/rc.keymap
 echo "if [ -x /usr/bin/loadkeys ]; then" >> $T_PX/etc/rc.d/rc.keymap
 echo " /usr/bin/loadkeys $MAPNAME" >> $T_PX/etc/rc.d/rc.keymap
 echo "fi" >> $T_PX/etc/rc.d/rc.keymap
 chmod 755 $T_PX/etc/rc.d/rc.keymap
fi

# Only ask if we want to skip configuring if we suspect the user should
# skip the step:
if [ -r $T_PX/etc/fstab ]; then
 dialog --title "CONFIGURE THE SYSTEM" --yesno "Now we can configure your \
 Linux system. If this is a new installation, you MUST configure it now or it \
 will not boot correctly. However, if you are just adding software to an \
 existing system, you can back out to the main menu and skip this step. \
 However (important exception) if you've installed a new kernel image, it's \
 important to reconfigure your system so that you can install LILO (the Linux \
 loader) or create a bootdisk using the new kernel. You want to CONFIGURE \
 your system, right?" 13 60 
 if [ $? = 1 -o $? = 255 ]; then
  exit
 fi
fi

# Install the bootdisk's Linux kernel:
. /usr/lib/setup/SeTkernel

while [ 0 ]; do # the bootdisk menu loop
 dialog --title "MAKE BOOTDISK" --menu "It is highly recommended that you make\
 a bootdisk (or two) for your system at this time.  There are two types of \
 bootdisks that you can make: a LILO bootdisk (which is more flexible) or \
 a simple bootdisk (which is just a kernel image written directly to disk). \
 Note that the simple bootdisk may no longer work for larger recent kernels, \
 and the LILO bootdisk is now the recommended choice. \
 Which option would you like?" 17 74 4 \
 "format" "format floppy disk in /dev/fd0" \
 "lilo" "make lilo bootdisk (recommended)" \
 "simple" "make simple vmlinuz > /dev/fd0 bootdisk (obsolete)" \
 "continue" "leave bootdisk menu and continue with the configuration" 2> $TMP/return
 if [ ! $? = 0 ]; then
  break;
 fi
 REPLY=`cat $TMP/return`
 rm -f $TMP/return
 if [ "$REPLY" = "format" ]; then
  dialog --title "SPECIFY FLOPPY SIZE" --menu "What size is your /dev/fd0 (a:)\
  drive?" 9 60 2 \
  "1.44" "1.44 megabytes" \
  "1.2" "1.2 megabytes" 2> $TMP/return
  if [ "`cat $TMP/return`" = "1.44" ]; then
   dialog --title "Formatting /dev/fd0u1440" --infobox "Formatting /dev/fd0, \
   1.44 megabytes." 3 50 
   fdformat -n /dev/fd0u1440 1> /dev/null 2> /dev/null
  elif [ "`cat $TMP/return`" = "1.2" ]; then
   dialog --title "Formatting /dev/fd0h1200" --infobox "Formatting /dev/fd0, \
   1.2 megabytes." 3 50
   fdformat -n /dev/fd0h1200 1> /dev/null 2> /dev/null
  fi
  rm -f $TMP/return
 elif [ "$REPLY" = "simple" ]; then # make simple bootdisk
  dialog --title "BOOT DISK CREATION" --yesno \
"\n\
Now put a formatted floppy in your boot drive. \n\
This will be made into your Linux boot disk. Use this to\n\
boot Linux until LILO has been configured to boot from\n\
the hard drive.\n\n\
Any data on the target disk will be destroyed.\n\n\
YES creates the disk, NO aborts.\n" 14 62
  if [ $? = 0 ]; then
   dialog --title "CREATING DISK" --infobox "Creating boot disk from 'vmlinuz'..." 5 50
   KERNEL="/vmlinuz"
   dd if=$T_PX$KERNEL of=/dev/fd0 2> /dev/null
   rdev /dev/fd0 $ROOT_DEVICE
   rdev -v /dev/fd0 -1
   rdev -R /dev/fd0 1
   sync
  fi
 elif [ "$REPLY" = "lilo" ]; then # make lilo bootdisk
  dialog --title "CREATING LILO BOOTDISK IN /dev/fd0" --yesno "Now put a \
  formatted floppy in your boot drive.  This will be made into a LILO \
  bootdisk that you can use to start your Linux system.  Any data on the \
  target disk will be destroyed.  YES creates the disk, NO aborts." 8 62
  if [ $? = 0 ]; then # make the disk
   dialog --title "SPECIFY FLOPPY SIZE" --menu "What size is your /dev/fd0 (a:)\
   drive?" 9 60 2 \
   "1.44" "1.44 megabytes" \
   "1.2" "1.2 megabytes" 2> $TMP/return
   if [ "`cat $TMP/return`" = "1.44" ]; then
    DEV=/dev/fd0u1440
   else
    DEV=/dev/fd0h1200
   fi
   dialog --infobox "Creating LILO bootdisk for $ROOT_DEVICE..." 3 60
   mke2fs -i 1024 $DEV 1> /dev/null 2> /dev/null
   if [ ! -d $TMP/lilo ]; then
    mkdir $TMP/lilo
   fi
   mount -t ext2 /dev/fd0 $TMP/lilo 1> /dev/null 2> /dev/null
   cp $T_PX/vmlinuz $TMP/lilo/vmlinuz
   mkdir $TMP/lilo/etc
   cat << EOF > $TMP/lilo/etc/lilo.conf
boot = /dev/fd0
message=/boot/message
prompt
image = /vmlinuz
        label = mount
        ramdisk = 0
        root = $ROOT_DEVICE
        vga = normal
        read-only
EOF
   mkdir -p $TMP/lilo/dev
   cat /proc/partitions | grep d | while read line ; do
     SMASHED_LINE=$line
     MAJOR=`echo $SMASHED_LINE | cut -f 1 -d ' '`
     MINOR=`echo $SMASHED_LINE | cut -f 2 -d ' '`
     DEVNAME=`echo $SMASHED_LINE | cut -f 4 -d ' '`
     if [ ! -b $TMP/lilo/dev/$DEVNAME ]; then
       mknod $TMP/lilo/dev/$DEVNAME b $MAJOR $MINOR
       chown root.disk $TMP/lilo/dev/$DEVNAME
       chmod 640 $TMP/lilo/dev/$DEVNAME
     fi
   done
   cp -a /dev/fd0h1* /dev/fd0u1* /dev/fd1h1* /dev/fd1u1* $TMP/lilo/dev
   mkdir -p $TMP/lilo/boot
   cp -a $T_PX/boot/*.b $TMP/lilo/boot
   cat << EOF > $TMP/lilo/boot/message

Welcome to the Slackware Linux custom LILO bootdisk!  

By default, this disk boots a root Linux partition on $ROOT_DEVICE when
you hit ENTER.  If you'd like to boot some other partition, use a command
like this on the LILO prompt below:

    mount root=/dev/sda1 ro

Where "/dev/sda1" is the partition you want to boot, and "ro" specifies that
the partition should be initially mounted as read-only.  If you wish to mount
the partition read-write, use "rw" instead.  You may also add any other kernel
parameters you might need depending on your hardware, and which drivers are
included in your kernel.

EOF
   lilo -r $TMP/lilo 1> /dev/null 2> /dev/null
   umount $TMP/lilo
   rm -rf $TMP/lilo
  fi
 elif [ "$REPLY" = "continue" ]; then
  break;
 fi
done

dialog --title "MODEM CONFIGURATION" --menu "This part of the configuration \
process will create a /dev/modem link pointing to the serial device (ttyS0, \
ttyS1, ttyS2, ttyS3) representing your default modem.  You \
can change this link \
later if you move your modem to a different port. \
Please select the serial \
device which you would like to use for your modem:" 16 70 5 \
"/dev/ttyS0" "(COM1: under DOS)" \
"/dev/ttyS1" "(COM2: under DOS)" \
"/dev/ttyS2" "(COM3: under DOS)" \
"/dev/ttyS3" "(COM4: under DOS)" \
"no modem" "I don't have a modem!" 2> $TMP/callout
if [ $? = 0 ]; then
 MODEM_DEVICE="`cat $TMP/callout`"
 if [ ! "$MODEM_DEVICE" = "no modem" ]; then
  MODEM_DEVICE=`basename $MODEM_DEVICE`
  (cd $T_PX/dev; ln -sf $MODEM_DEVICE modem)
 fi
fi
rm -f $TMP/callout

# Post installation and setup scripts added by packages.
if [ -d $T_PX/var/log/setup ]; then
 for INSTALL_SCRIPTS in $T_PX/var/log/setup/setup.*
 do
  SCRIPT=`basename $INSTALL_SCRIPTS`
  # Here, we call each script in /var/log/setup. Two arguments are provided:
  # 1 -- the target prefix (normally /, but /mnt from the bootdisk)
  # 2 -- the name of the root device.
  ( cd $T_PX ; sh var/log/setup/$SCRIPT $T_PX $ROOT_DEVICE )
  if echo $SCRIPT | fgrep onlyonce 1> /dev/null 2> /dev/null; then # only run after first install
   if [ ! -d $T_PX/var/log/setup/install ]; then
    mkdir $T_PX/var/log/setup/install
   fi
   mv $INSTALL_SCRIPTS $T_PX/var/log/setup/install
  fi
 done
fi
