#!/bin/sh
#
# Copyright 1993, 1994, 1995, 1996,
#   1997, 1998, 1999  Patrick Volkerding, Moorhead, MN USA
#   All rights reserved.
#
# Redistribution and use of this script, with or without modification, is 
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# As always, bug reports, suggestions, etc: volkerdi@mhd1.moorhead.msus.edu
# or bf703@cleveland.freenet.edu, or (if you absolutely must) (218) 233-4349.
# 
#
reset
TMP=/var/log/setup/tmp
rm -f $TMP/SeT*
touch $TMP/SeTQUICK # use quick menus by default
touch $TMP/SeTcolor
PATH="$PATH:/usr/lib/setup"
export PATH;
PARTLIST="`fdisk -l | fgrep "Linux native"`" 2> /dev/null
echo "$PARTLIST" > $TMP/SeTplist
T_PX="/"
echo "$T_PX" > $TMP/SeTT_PX
#DISTRIBUTION_SOURCE="/var/log/mount"
#echo "$DISTRIBUTION_SOURCE" > $TMP/SeTDS
#SOURCE_DEVICE="/dev/fd0"
# We may want to force the issue...
# echo "$SOURCE_DEVICE" > $TMP/SeTsource
ROOT_DEVICE="`mount | fgrep "on / " | cut -b-10`"
echo "$ROOT_DEVICE" > $TMP/SeTrootdev
if mount | fgrep /var/log/mount 1> /dev/null 2> /dev/null ; then # clear source
 umount /var/log/mount                                           # location
fi
# 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
rm -f /var/log/mount 2> /dev/null
rmdir /var/log/mount 2> /dev/null
mkdir /var/log/mount 2> /dev/null

while [ 0 ]; do
 if [ -r $TMP/SeTQUICK ]; then
  QUICK="QUICK"
 else
  QUICK="VERBOSE"
 fi

 dialog --title "Slackware Linux Setup (version HD-4.0.0)" --menu "\nWelcome\
 to Slackware Linux Setup.\n\
\nHint: If you have trouble using the arrow keys on your keyboard,\n\
you can use '+', '-', and TAB instead. Which option would you like?" 21 74 10 \
"HELP" "Read the Slackware Setup HELP file" \
"KEYMAP" "Remap your keyboard" \
"MAKE TAGS" "Tagfile customization program" \
"TARGET" "Select target directory [now: $T_PX]" \
"SOURCE" "Select source media" \
"SELECT" "Select which software sets to install" \
"INSTALL" "Install selected software sets" \
"CONFIGURE" "Reconfigure your Linux system" \
"PKGTOOL" "Install or remove packages with Pkgtool" \
"EXIT" "Exit Slackware Linux Setup" 2> $TMP/hdset
 if [ $? = 1 -o $? = 255 ]; then
  rm -f $TMP/hdset $TMP/SeT*
  reset
  exit
 fi
 MAINSELECT="`cat $TMP/hdset`"
 rm $TMP/hdset

 # Start checking what to do. Some modules may reset MAINSELECT to run the
 # next item in line.

 if [ "$MAINSELECT" = "HELP" ]; then
  SeTHELP
 fi
 
 if [ "$MAINSELECT" = "QUICK" ]; then
  SeTquick
 fi

 if [ "$MAINSELECT" = "MAKE TAGS" ]; then
  SeTmaketag
 fi

 if [ "$MAINSELECT" = "KEYMAP" ]; then
  SeTkeymap
  if [ -r $TMP/SeTkeymap ]; then
   dialog --title "CONTINUE?" --yesno "We're done with the KEYMAP section, \
would you like to \
go on to the SOURCE section?" 7 40
   if [ $? = 0 ]; then
    MAINSELECT="SOURCE" 
   fi
  fi
 fi

 if [ "$MAINSELECT" = "SOURCE" ]; then
  SeTmedia 
  if [ -r $TMP/SeTsource -a ! -r $TMP/SeTSERIES ]; then
   dialog --title "CONTINUE?" --yesno "Now that you've set up your source \
location, would you like \
to go on to the SELECT section and decide which catagories of software \
 you wish to \
install?" 9 40
   if [ $? = 0 ]; then
    MAINSELECT="SELECT"
   fi
  elif [ -r $TMP/SeTsource -a -r $TMP/SeTSERIES ]; then
   dialog --title "CONTINUE?" --yesno "Now that you've indicated \
your source location and selected \
the disk sets you want, you may begin the installation process, or you may \
return to the main menu. Would you like to begin the installation?" 9 50
   if [ $? = 0 ]; then
    MAINSELECT="INSTALL"
   fi
  fi
 fi

 if [ "$MAINSELECT" = "TARGET" ]; then
  dialog --title "CHANGE TARGET DIRECTORY?" --yesno "The target \
directory is the one the packages will be untarred \
from. In most cases, there is no need to change this from the default \
value of '/', but if you are installing Slackware Linux into a subdirectory \
for some reason, then you'll want to change it. It's unlikely that the system \
will run in a directory other \
than '/', though. Are you sure you want to change \
the target directory? " 11 70
  if [ $? = 1 ]; then
   continue;
  fi
  dialog --inputbox "What directory would you like \
to install Slackware into?" \
  9 40 2> $TMP/slackdir
  if [ $? = 0 ]; then
   T_PX="`cat $TMP/slackdir`"
   mkdir -p $T_PX
   echo "$T_PX" > $TMP/SeTT_PX
  else
   rm -f $TMP/slackdir 
   continue
  fi 
  rm -f $TMP/slackdir
 fi

 if [ "$MAINSELECT" = "SELECT" ]; then
  SeTPKG
  if [ -r $TMP/SeTSERIES -a -r $TMP/SeTsource ]; then
   dialog --title "CONTINUE?" --yesno "Now that you've set up \
the source location and decided \
what you want to install, you can go ahead and begin the installation \
process, or you can go back to the main menu. Do you want to start the INSTALL \
section and begin installing software?" 10 55
   if [ $? = 0 ]; then
    MAINSELECT="INSTALL"
   fi
  fi
 fi

 if [ "$MAINSELECT" = "INSTALL" ]; then
  if [ ! -r $TMP/SeTSERIES -o ! -r $TMP/SeTsource -o ! $TMP/SeTnative ]; then
   dialog --title "CANNOT INSTALL SOFTWARE YET" --msgbox "\
\n\
Before you can install software, complete the following tasks:\n\
\n\
1. Select your source media.\n\
2. Select which software sets to install.\n\
\n\
You may also optionally remap your keyboard or select a\n\
different target directory than '/'.\n\
\n\
Press ENTER to return to the main menu." 15 68
   continue
  fi
  SERIES="-sets `cat $TMP/SeTSERIES`"
  SOURCE_DEVICE="`cat $TMP/SeTsource`"
  IGNORE_TAGFILES=""
  while [ 0 ]; do
   dialog --title "SELECT PROMPTING MODE" --menu \
   "Now you must select the type of prompts you'd like to see during the \
   installation process. If you have the drive space, the 'full' option \
   is quick, easy, and by far the most foolproof choice. The 'newbie' \
   mode provides the most information but is much more time-consuming \
   (presenting the packages one by one) than the menu-based choices. \
   Otherwise, you can pick packages from menus \
   using 'expert' or 'menu' mode. Which type of prompting would you like \
   to use?" \
   21 70 7 \
   "full" "Install everything (up to 386 MB of software)" \
   "newbie" "Use verbose prompting (and follow tagfiles)" \
   "menu" "Choose groups of packages from interactive menus" \
   "expert" "Choose individual packages from interactive menus" \
   "custom" "Use custom tagfiles in the package directories" \
   "tagpath" "Use tagfiles in the subdirectories of a custom path" \
   "help" "Read the prompt mode help file" 2> $TMP/SeTpmode
   if [ $? = 1 -o $? = 255 ]; then
    rm -f $TMP/SeTpmode
    exit
   fi
   MODE="`cat $TMP/SeTpmode`"
   rm -f $TMP/SeTtagext

   if [ "$MODE" = "help" ]; then
    dialog --title "PROMPT MODE HELP" --textbox "/usr/lib/setup/PROMPThelp" 19 65
   fi

   if [ "$MODE" = "tagpath" ]; then
    dialog --title "PROVIDE A CUSTOM PATH TO YOUR TAGFILES" --inputbox "If \
you're installing from CD-ROM, it's impossible to edit the tagfiles that are \
in the package directories. In this case, you might want to provide a path to \
your custom tagfiles. As an example, you could create a /tagfiles directory \
and mount a floppy disk containing the tagfiles on that directory. Then you'd \
enter '/tagfiles' at the prompt below. The setup program will look for your \
tagfile in SUBDIRECTORIES of the path you provide, such as /tagfiles/a1, \
/tagfiles/ap1, /tagfiles/d1, and so on. You only need to provide a \
subdirectory and tagfile for the first disk of each series to be installed. \
If a custom tagfile is not found at the path you provide, setup will revert to \the default tagfiles. Please enter the path to your custom tagfiles:" \
20 70 2> $TMP/SeTtagpath
    if [ $? = 1 -o $? = 255 ]; then
     continue
    fi
    if [ -r $TMP/SeTtagpath ]; then
     if [ "`cat $TMP/SeTtagpath`" = "" ]; then
      rm -f $TMP/SeTtagpath
     fi
    fi
    break;
   fi

   if [ "$MODE" = "newbie" ]; then
    dialog --infobox "'newbie' prompt mode selected. Using default tagfiles \
and verbose package prompting." 4 50
    break;
   fi

   if [ "$MODE" = "custom" ]; then
    dialog --title "ENTER CUSTOM EXTENSION" --inputbox "Now, enter the custom \
extension you have used for your tagfiles. This must be a valid MS-DOS format \
file extension consisting of a period followed by three characters. For \
example, I use '.pat'. You might see my tagfiles on your disks. :^)" \
13 60 2> $TMP/SeTtagext
    if [ $? = 1 -o $? = 255 ]; then
     continue
    fi
    if [ -r $TMP/SeTtagext ]; then
     if [ "`cat $TMP/SeTtagext`" = "" ]; then
      rm -f $TMP/SeTtagext
     fi
    fi
    dialog --infobox "CUSTOM prompt mode selected. Using prompting defaults \
found in custom tagfiles." 4 50
    break;
   fi

   if [ "$MODE" = "full" ]; then
    IGNORE_TAGFILES="-ignore_tagfiles"
    dialog --infobox "'full' mode selected. Installing all the packages \
in the selected software sets." 4 45
    break;
   fi

   if [ "$MODE" = "menu" ]; then
    > $TMP/SeTQUICK
    MAKETAG="maketag.ez"
    dialog --infobox "'menu' prompt mode selected. Using interactive menus \
to choose subsystems of related packages." 4 60
    break;
   fi

   if [ "$MODE" = "expert" ]; then
    > $TMP/SeTQUICK
    MAKETAG="maketag"
    dialog --infobox "'expert' prompt mode selected. Using interactive menus \
to choose packages individually." 4 55
    break;
   fi
  done
  export MAKETAG;
  sleep 3
  SOURCE_MOUNTED="`cat $TMP/SeTmount`"
  DISTRIBUTION_SOURCE="`cat $TMP/SeTDS`"
  if [ "$DISTRIBUTION_SOURCE" != "" ]; then
   cpkgtool $SOURCE_MOUNTED $IGNORE_TAGFILES -source_device $SOURCE_DEVICE -source_dir $DISTRIBUTION_SOURCE -target_dir $T_PX $SERIES
  else
   cpkgtool $SOURCE_MOUNTED $IGNORE_TAGFILES -source_device $SOURCE_DEVICE -target_dir $T_PX $SERIES
  fi
  MAINSELECT="CONFIGURE"
 fi

 if [ "$MAINSELECT" = "CONFIGURE" ]; then
  SeTconfig
 fi

 if [ "$MAINSELECT" = "PKGTOOL" ]; then
  cpkgtool
 fi

 if [ "$MAINSELECT" = "EXIT" ]; then
  rm -f $TMP/SeT*
  reset
  break
 fi

done # end of main loop

rm -f $TMP/tagfile $TMP/SeT*
 
sync
if mount | fgrep /var/log/mount 1> /dev/null 2> /dev/null ; then
 umount /var/log/mount 
fi
rm -f /var/log/mount 2> /dev/null
rmdir /var/log/mount 2> /dev/null
mkdir /var/log/mount 2> /dev/null
chmod 755 /var/log/mount
if [ -r /tagfile ]; then
 rm -f /tagfile
fi
chmod 755 $T_PX
if [ -d $T_PX/tmp ]; then
 chmod 1777 $T_PX/tmp
fi
# end slackware setup script
