#!/bin/sh
TMP=/var/log/setup/tmp
if [ ! -d $TMP ]; then
  mkdir -p $TMP
fi
while [ 0 ]; do
rm -f $TMP/SeTDS $TMP/SeTmount
# OK, at this point /var/log/mount should not have anything mounted on it,
# but we will umount just in case.
umount /var/log/mount 2> /dev/null
# Anything mounted on /var/log/mount now is a fatal error:
if mount | fgrep /var/log/mount 1> /dev/null 2> /dev/null ; then
  echo "Can't umount /var/log/mount.  Reboot machine and run setup again."
  exit
fi
# If the mount table is corrupt, the above might not do it, so we will
# try to detect Linux and FAT32 partitions that have slipped by:
if [ -d /var/log/mount/lost+found -o -d /var/log/mount/recycled \
     -o -r /var/log/mount/io.sys ]; then
  echo "Mount table corrupt.  Reboot machine and run setup again."
  exit
fi

while [ 0 ]; do
 cat << EOF > $TMP/tempmsg

In order to install directly from the hard disk you must
have a partition with a directory containing the Slackware
distribution such that each disk other than the boot disk
is contained in a subdirectory. For example, if the 
distribution is in /stuff/slack, then you have to have
directories named /stuff/slack/a1, /stuff/slack/a2, and so
on each containing the files that would be on that disk.
You may install from DOS, HPFS, or Linux partitions.
Please enter the partition where the Slackware sources can 
be found, or [enter] to see a partition list: \n
EOF
 dialog --title "INSTALLING FROM HARD DISK" --inputbox \
"`cat $TMP/tempmsg`" 20 70 2> $TMP/source.part
 if [ $? = 1 -o $? = 255 ]; then
  rm -f $TMP/source.part $TMP/tempmsg
  exit
 fi
 rm -f $TMP/tempmsg
 SLACK_DEVICE="`cat $TMP/source.part`"
 rm -f $TMP/source.part
 if [ "$SLACK_DEVICE" = "" ]; then
  dialog --title "PARTITION LIST" --msgbox "`probe -l | fgrep -v cylind | fgrep dev 2> /dev/null`" 22 75
  continue;
 fi
 break;
done

dialog --title "SELECT SOURCE DIRECTORY" --inputbox \
"\n\
Now we need to know what directory on this partition \n\
the Slackware sources can be found in. (The directory \n\
in which the subdirectories for each disk is found) \n\
NOTE: You must give the directory name relative to the \n\
top of the partition.  So, for example, if you're going \n\
to mount this partition under /usr, don't include the \n\
'/usr'at the beginning of the pathname. \n\n\
What directory are the Slackware sources in? \n" \
19 65 2> $TMP/source.dir
if [ $? = 1 -o $? = 255 ]; then
 rm -f $TMP/source.dir
 exit
fi
SLACK_SOURCE_LOCATION="`cat $TMP/source.dir`"
rm -f $TMP/source.dir
if mount | fgrep $SLACK_DEVICE 1> /dev/null 2> /dev/null ; then
 # This partition is already mounted, so we will have to
 # tweak things funny.
 rm -f /var/log/mount 2> /dev/null
 rmdir /var/log/mount 2> /dev/null
 PREFIX="`mount | fgrep $SLACK_DEVICE`"
 PREFIX="`echo "$PREFIX" | cut -b14-`"
 end_of_line="1"
 while [ 0 ]; do
  end_of_line="`expr $end_of_line + 1`"
  if [ "`echo "$PREFIX" | cut -b$end_of_line`" = " " ]; then  # Found it!
   end_of_line="`expr $end_of_line - 1`"
   break;
  fi
 done
 PREFIX="`echo "$PREFIX" | cut -b1-$end_of_line`"
 ln -sf $PREFIX /var/log/mount 
else
 SUCCESS=false
 for type in ext2 hpfs msdos ; do
   mount -r -t $type $SLACK_DEVICE /var/log/mount 1> /dev/null 2> /dev/null
   if [ $? = 0 ]; then # mounted successfully 
     SUCCESS=true
     break;
   fi
 done
 if [ ! $SUCCESS = true ]; then # there was a mount error
  cat << EOF > $TMP/tempmsg
There was a problem mounting your partition. Would you like to:

EOF
  dialog --title "MOUNT ERROR" --menu "`cat $TMP/tempmsg`" 10 68 2 \
"Restart" "Start over" \
"Ignore " "Ignore the error and continue" 2> $TMP/dowhat
  if [ $? = 1 -o $? = 255 ]; then
   rm -f $TMP/dowhat
   exit
  fi
  DOWHAT="`cat $TMP/dowhat`"
  rm -f $TMP/dowhat
  if [ "$DOWHAT" = "Restart" ]; then
   umount /var/log/mount 2> /dev/null
   continue;
  fi
  echo
 fi # mount error
fi

if [ -d /var/log/mount/$SLACK_SOURCE_LOCATION ]; then
 echo "/var/log/mount/$SLACK_SOURCE_LOCATION" > $TMP/SeTDS
 echo "-source_mounted" > $TMP/SeTmount
 echo "/dev/null" > $TMP/SeTsource
 exit
else
 cat << EOF > $TMP/tempmsg

Sorry, but the directory $SLACK_SOURCE_LOCATION does not exist
on partition $SLACK_DEVICE.

Would you like to try again?
EOF
 dialog --title "SOURCE DIRECTORY NOT FOUND" --yesno "`cat $TMP/tempmsg`" 10 70
 if [ $? = 1 -o $? = 255 ]; then
  rm -f $TMP/tempmsg
  exit
 fi
 rm -r $TMP/tempmsg
fi

done
