#!/bin/sh
#
# Adapted for Slackel from Slackware, see below:
# Copyright 1993,1994,1999  Patrick Volkerding, Moorhead, Minnesota USA
# Copyright 2001, 2003, 2004  Slackware Linux, Inc., Concord, CA
# Copyright 2006  Patrick Volkerding, Sebeka, Minnesota 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@slackware.com
#
TMP=/var/log/setup/tmp
if [ ! -d $TMP ]; then
  mkdir -p $TMP
fi
# Use /tmp on floppy for now:
/usr/lib/setup/unmigrate.sh
rm -f $TMP/SeT*
rm -f $TMP/isAuto
# If a keymap was set up, restore that data:
if [ -r $TMP/Pkeymap ]; then
  cp $TMP/Pkeymap $TMP/SeTkeymap
fi
echo "on" > $TMP/SeTcolor # turn on color menus
PATH="$PATH:/usr/lib/setup"
export PATH;
export COLOR=on

dialog --title "WELCOME TO SLACKEL INSTALLATION" \
--yes-label "Use US keymap" \
--no-label "Select another keymap" \
--yesno "Welcome to the installation of Slackel, The Bonsai OS!\n\nBefore you proceed \
with the rest of the installation, you might want to \
select your keyboard map (keymap). The default keyboard map is the US keyboard map." 0 0
[ $? -eq 1 ] && SeTkeymap

while [ 0 ]; do

dialog --title "Slackel Installation" \
--ok-label "Select" \
--cancel-label "Exit installation" \
--menu \
"You may select an option below using the UP/DOWN keys and SPACE or \
ENTER. Alternate keys may also be used: '+', '-', and TAB.\n\n\
If you want to exit the installation and drop to a linux console, \
select 'Exit installation'" 14 80 3 \
"INSTALL" "Install Slackel using a step-by-step guide" \
"AUTOINSTALL" "Autopartition and install with few options (wipes your HD)" \
2> $TMP/hdset
 if [ ! $? = 0 ]; then
  rm -f $TMP/hdset $TMP/SeT*
  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" = "AUTOINSTALL" ]; then
  rm -f $TMP/isAuto
  . autoinstall 2>/dev/null
 fi

 if [ "$MAINSELECT" = "INSTALL" ]; then
	MAINSELECT="PARTITION"
 fi	

 if [ "$MAINSELECT" = "PARTITION" ]; then
	  SeTdisk
	  # Before probing, activate any LVM partitions
	  # that may exist from before the boot:
	  vgchange -ay 1> /dev/null 2> /dev/null
	  if probe -l 2> /dev/null | egrep 'Linux$' 1> /dev/null 2> /dev/null ; then
		probe -l 2> /dev/null | egrep 'Linux$' | sort 1> $TMP/SeTplist 2> /dev/null
		
		
		MAINSELECT="ADDSWAP"
	  else
		dialog --title "NO LINUX PARTITIONS DETECTED" \
		--msgbox "There don't seem to be any Linux partitions on this machine.\n\nYou'll need to make at least one of these to install Linux. Either select AUTOINSTALL from the menu, or select INSTALL and use the partition editor to create one." 12 60
		MAINSELECT=""
	  fi
 fi

###### I think this can be safely placed here... ######
T_PX="/mnt"
echo "$T_PX" > $TMP/SeTT_PX

if [ -f $TMP/isAuto ]; then
	ROOT_DEVICE="`mount | grep "on /mnt " | cut -f 1 -d ' '`"
	echo "$ROOT_DEVICE" > $TMP/SeTrootdev
else
	ROOT_DEVICE="`mount | grep "on / " | cut -f 1 -d ' '`"
	echo "$ROOT_DEVICE" > $TMP/SeTrootdev
fi


if mount | grep /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 | grep /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
#########################################################

 if [ "$MAINSELECT" = "ADDSWAP" ]; then
  SeTswap
  if [ -r $TMP/SeTswap ]; then
   MAINSELECT="TARGET"
  elif [ -r $TMP/SeTswapskip ]; then
   # Go ahead to TARGET without swap space:
   MAINSELECT="TARGET"
  fi
 fi

 if [ "$MAINSELECT" = "TARGET" ]; then
  SeTpartitions
  if [ $? -ne 0 ]; then
	continue
  fi
  SeTDOS
  if [ -r $TMP/SeTnative ]; then
   MAINSELECT="SOURCE"
  fi
 fi

 if [ "$MAINSELECT" = "SOURCE" ]; then
	SeTmedia 
	if [ ! -r $TMP/SeTsource -a ! -r $TMP/SeTgateway -a ! -r $TMP/SeTmount ]; then
   		dialog --title "No Source Selected" --msgbox "\nInstaller does not know where to find the source packages...\n\n    Press ENTER to return to the main menu." 10 68
   		continue
	else
	  MAINSELECT="INSTALLSOFT"
	fi
 fi
 
 if [ "$MAINSELECT" = "INSTALLSOFT" ]; then
  if [ ! -r $TMP/SeTsource -o ! -r $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. Set up your target Linux partition(s).\n\
\n\
You may also optionally remap your keyboard and set up your\n\
swap partition(s). \n\
\n\
Press ENTER to return to the main menu." 16 68
   continue
  fi
  SERIES="#CORE##BASIC##FULL##SETTINGS#"
  SOURCE_DEVICE="`cat $TMP/SeTsource`"
  IGNORE_TAGFILES="-ignore_tagfiles"
  
  dialog --title "SELECT INSTALLATION MODE" \
  	--no-cancel --menu \
"Please select the installation mode you prefer \
from the following:\n\n
FULL (default) will install everything. That includes \
one application per task, such as an office \
suite, a multimedia player, a CD/DVD burner etc.\n\n
BASIC will only install a minimal graphical environment \
and a web browser.\n\n
CORE will install only the minimum essentials for \
your system to start in console mode (no graphical \
environment included) and is ideal if you are an \
experienced user and want to fully customize your \
installation for any specific purpose.\n " 23 70 3 \
"FULL" "Install everything" \
"BASIC" "Install a minimal graphical environment" \
"CORE" "Install a minimal console system" 2> $TMP/SeTpmode
  MODE=`cat $TMP/SeTpmode`
  
  if [ -r $TMP/SeTCDdev ]; then # only try to remount media if it's a CD/DVD
    slackinstall --device `cat $TMP/SeTCDdev` --promptmode $MODE --srcpath `cat $TMP/SeTDS` --mountpoint /var/log/mount --target $T_PX --series $SERIES
  elif [ -r $TMP/SeTremotesvr ]; then
    slackinstall --device noremount --promptmode $MODE --srcpath `cat $TMP/SeTDS` --mountpoint /var/log/mount --target $T_PX --series $SERIES --net `cat $TMP/SeTremotesvr`
  else
    slackinstall --device noremount --promptmode $MODE --srcpath `cat $TMP/SeTDS` --mountpoint /var/log/mount --target $T_PX --series $SERIES
  fi
  MAINSELECT="CONFIGURE"
 fi

 if [ "$MAINSELECT" = "CONFIGURE" ]; then
  SeTconfig
  REPLACE_FSTAB=Y
  if [ -r $TMP/SeTnative ]; then
   if [ -r $T_PX/etc/fstab ]; then
    dialog --title "REPLACE /etc/fstab?" --yesno "You already have an \
/etc/fstab on your install partition.  If you were just adding software, \
you should probably keep your old /etc/fstab.  If you've changed your \
partitioning scheme, you should use the new /etc/fstab.  Do you want \
to replace your old /etc/fstab with the new one?" 10 58
    if [ ! $? = 0 ]; then
     REPLACE_FSTAB=N
    fi
   fi
   if [ "$REPLACE_FSTAB" = "Y" ]; then
    cat /dev/null > $T_PX/etc/fstab
    if [ -r $TMP/SeTswap ]; then
     cat $TMP/SeTswap > $T_PX/etc/fstab
    fi
    cat $TMP/SeTnative >> $T_PX/etc/fstab
    if [ -r $TMP/SeTDOS ]; then
     cat $TMP/SeTDOS >> $T_PX/etc/fstab
    fi
    printf "%-16s %-16s %-11s %-16s %-3s %s\n" "#/dev/cdrom" "/mnt/cdrom" "auto" "noauto,owner,ro,comment=x-gvfs-show" "0" "0" >> $T_PX/etc/fstab
    printf "%-16s %-16s %-11s %-16s %-3s %s\n" "/dev/fd0" "/mnt/floppy" "auto" "noauto,users,rw,umask=00" "0" "0" >> $T_PX/etc/fstab
    printf "%-16s %-16s %-11s %-16s %-3s %s\n" "devpts" "/dev/pts" "devpts" "gid=5,mode=620" "0" "0" >> $T_PX/etc/fstab
    printf "%-16s %-16s %-11s %-16s %-3s %s\n" "proc" "/proc" "proc" "defaults" "0" "0" >> $T_PX/etc/fstab
    printf "%-16s %-16s %-11s %-16s %-3s %s\n" "tmpfs" "/dev/shm" "tmpfs" "defaults" "0" "0" >> $T_PX/etc/fstab
   fi
  fi
  MAINSELECT="EXIT"
 fi

 if [ "$MAINSELECT" = "EXIT" ]; then
   break
 fi

done # end of main loop
sync

chmod 755 $T_PX
if [ -d $T_PX/tmp ]; then
 chmod 1777 $T_PX/tmp
fi
if mount | grep /var/log/mntiso 1> /dev/null 2> /dev/null ; then
 umount -f /var/log/mntiso
fi
if mount | grep /var/log/mount 1> /dev/null 2> /dev/null ; then
 umount /var/log/mount
fi
# Anything mounted on /var/log/mount now is a fatal error:
if mount | grep /var/log/mount 1> /dev/null 2> /dev/null ; then
  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
  exit
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 [ -f /mnt/etc/fstab ]; then
  # umount CD:
  if [ -r $TMP/SeTCDdev ]; then
    if mount | grep iso9660 > /dev/null 2> /dev/null ; then
      umount `mount | grep iso9660 | cut -f 1 -d ' '`
    fi
    eject -s `cat $TMP/SeTCDdev`
       dialog --title "SLACKEL INSTALLATION IS COMPLETE" \
       --yes-label "Reboot now" \
       --no-label "Exit to command line" \
       --yesno "System configuration and installation is complete. Please remove the installation disc. You can either select to reboot your system now or choose to exit the installation to the command line if you would like to do any manual tweaking." \
       10 65
       retval=$?
  else
       dialog --title "SLACKEL INSTALLATION IS COMPLETE" \
       --yes-label "Reboot now" \
       --no-label "Exit to command line" \
       --yesno "System configuration and installation is complete. You can either select to reboot your system now or choose to exit the installation to the command line if you would like to do any manual tweaking." \
       10 65
       retval=$?
  fi
  echo
fi

# Fix the date:
/sbin/fixdate

# final cleanup
rm -rf $TMP/dhcpc
rm -f $TMP/tagfile $TMP/SeT* $TMP/tar-error $TMP/PKGTOOL_REMOVED
rm -f /var/log/mount/pkgrepo
rm -f $TMP/isAuto
rmdir /var/log/mntiso 2>/dev/null
rm -rf $TMP/pkgrepo
rm -rf $TMP/pkgcache
rmdir /mnt/tmp/orbit-root 2> /dev/null
# end Slackel setup script
if [ $retval -eq 0 ]; then
	reboot
fi
