#!/bin/bash
# prepare installation environment for installing on fakeraid
# 13.37 specific

# initrd.gz is expected to be in /
VER=13.37
INITRD=/initrd.gz
PWD=pwd
PATCH_LIST="/sbin/probe /usr/lib/setup/SeTpartitions"

if [ ! -r /$INITRD ]; then
  echo "$INITRD not found, can't prepare"
  exit;
fi

cd /
chown root:root $INITRD
chmod 644 $INITRD
# extract dmraid binary into dir in $PATH
gzip -cd $INITRD | cpio -imd sbin/dmraid

# make backups of patched files
for f in $PATCH_LIST; do
  if [ ! -r $f.orig ]; then
    cp $f $f.orig;
  fi
done

# no patch utility anymore in installation environment!
# rewrite files completely
cat > /sbin/probe <<'EOF_DMRAID'
#!/bin/sh
# This is 'probe', a wrapper for using fdisk to gather drive info for
# the Slackware setup scripts.  I hate to bounce this much garbage through
# a tmpdir, but it looks like large variables can make ash crash...

# Many thanks to Vincent Rivellino for contributing the patches to support
# Mylex and Compaq RAID controllers.

TMP=/var/log/setup/tmp
rm -f $TMP/SeTfdisk

# listide major minor hd1 hd2 (2 base devs for major)
list_ide() {
  if [ "$2" = "0" ]; then
    fdisk -l /dev/$3 >> $TMP/SeTfdisk
  elif [ "$2" = "64" ]; then
    fdisk -l /dev/$4 >> $TMP/SeTfdisk
  fi
}

list_scsi() {
  # find drive # 0 - 15
  DRV=`expr $1 / 16`
  NUM=`expr $1 % 16`
  if [ ! "$NUM" = "0" ]; then
    return
  fi
  if [ "$DRV" = "0" ]; then
    fdisk -l /dev/sda >> $TMP/SeTfdisk
  elif [ "$DRV" = "1" ]; then
    fdisk -l /dev/sdb >> $TMP/SeTfdisk
  elif [ "$DRV" = "2" ]; then
    fdisk -l /dev/sdc >> $TMP/SeTfdisk
  elif [ "$DRV" = "3" ]; then
    fdisk -l /dev/sdd >> $TMP/SeTfdisk
  elif [ "$DRV" = "4" ]; then
    fdisk -l /dev/sde >> $TMP/SeTfdisk
  elif [ "$DRV" = "5" ]; then
    fdisk -l /dev/sdf >> $TMP/SeTfdisk
  elif [ "$DRV" = "6" ]; then
    fdisk -l /dev/sdg >> $TMP/SeTfdisk
  elif [ "$DRV" = "7" ]; then
    fdisk -l /dev/sdh >> $TMP/SeTfdisk
  elif [ "$DRV" = "8" ]; then
    fdisk -l /dev/sdi >> $TMP/SeTfdisk
  elif [ "$DRV" = "9" ]; then
    fdisk -l /dev/sdj >> $TMP/SeTfdisk
  elif [ "$DRV" = "10" ]; then
    fdisk -l /dev/sdk >> $TMP/SeTfdisk
  elif [ "$DRV" = "11" ]; then
    fdisk -l /dev/sdl >> $TMP/SeTfdisk
  elif [ "$DRV" = "12" ]; then
    fdisk -l /dev/sdm >> $TMP/SeTfdisk
  elif [ "$DRV" = "13" ]; then
    fdisk -l /dev/sdn >> $TMP/SeTfdisk
  elif [ "$DRV" = "14" ]; then
    fdisk -l /dev/sdo >> $TMP/SeTfdisk
  elif [ "$DRV" = "15" ]; then
    fdisk -l /dev/sdp >> $TMP/SeTfdisk
  fi
}

# List Mylex RAID device
list_rd() {
  # find drive
  DRV=`expr $2 / 8`
  NUM=`expr $2 % 8`
  if [ ! "$NUM" = "0" ]; then
    return
  fi
  fdisk -l /dev/rd/c$1d$DRV >> $TMP/SeTfdisk
  output_gpt_partitions /dev/rd/c$1d$DRV >> $TMP/SeTfdisk
}

# List Cpq SMART/2 RAID device
list_ida() {
  # find drive
  DRV=`expr $2 / 16`
  NUM=`expr $2 % 16`
  if [ ! "$NUM" = "0" ]; then
    return
  fi
  fdisk -l /dev/ida/c$1d$DRV >> $TMP/SeTfdisk
  output_gpt_partitions /dev/ida/c$1d$DRV >> $TMP/SeTfdisk
}

list_cciss() {
  # find drive
  DRV=`expr $2 / 16`
  NUM=`expr $2 % 16`
  if [ ! "$NUM" = "0" ]; then
    return
  fi
  fdisk -l /dev/cciss/c$1d$DRV >> $TMP/SeTfdisk
  output_gpt_partitions /dev/cciss/c$1d$DRV >> $TMP/SeTfdisk
}

list_ataraid() {
  # find drive
  DRV=`expr $2 / 16`
  NUM=`expr $2 % 16`
  if [ "$NUM" = "0" ]; then
     fdisk -l /dev/ataraid/d$DRV >> $TMP/SeTfdisk
     output_gpt_partitions /dev/ataraid/d$DRV >> $TMP/SeTfdisk
  else
     return
  fi
}

list_amiraid() {
  # find drive
  DRV=`expr $2 / 16`
  NUM=`expr $2 % 16`
  if [ "$NUM" = "0" ]; then
     fdisk -l /dev/amiraid/ar$DRV >> $TMP/SeTfdisk
     output_gpt_partitions /dev/amiraid/ar$DRV >> $TMP/SeTfdisk
  else
     return
  fi
}

is_swap() {
  HEADER=`dd if="$1" bs=1 skip=4086 count=10 2>/dev/null`
  if [ "$HEADER" = "SWAPSPACE2" -o "$HEADER" = "SWAP_SPACE" ]; then
    return 0
  else
    return 1
  fi
}

list_md() {
  if ( is_swap "/dev/$2" ); then TYPE="Linux swap"; else TYPE="Linux"; fi
  echo "/dev/$2  1 2 $1 kk $TYPE" >> $TMP/SeTfdisk
}

list_lvm() {
  lvscan 2>/dev/null | grep "ACTIVE" | while read line ; do
    SMASHED_LINE=$line
    if [ "$SMASHED_LINE" = "" ]; then
      break;
    fi
    DEV=`echo $SMASHED_LINE | cut -f2 -d"'"`
    SIZE=`lvdisplay $DEV -C --units k --noheadings --separator : | cut -f4 -d':'  | sed -e 's/^\([0-9]*\)[^0-9].*/\1/'`
    TYPE="Linux"
    if ( is_swap "$DEV" ); then TYPE="Linux swap"; fi
    echo "$DEV  0  0  $SIZE  lv  $TYPE" >> $TMP/SeTfdisk
  done
}

# List any volumes created by cryptsetup
list_crypt() {
  for i in $(ls /dev/mapper/); do
    if cryptsetup status $i 2>/dev/null | grep "is active" 1>/dev/null ; then
      DEV=$(cryptsetup status $i 2>/dev/null | grep "is active" | cut -f1 -d' ')
      SIZE=$(fdisk -s $(cryptsetup status $i 2>/dev/null | grep "device:" | cut -f2 -d: | tr -d ' '))
      echo "$DEV  0  0  $SIZE  lc  Linux" >> $TMP/SeTfdisk
    fi
  done
}

output_gpt_partitions() {
# First, make sure the device is GPT:
if fdisk -l $1 2> /dev/null | grep -wq GPT ; then 
  unset output
  # In the case of some RAID device like Mylex we will need to delimit the
  # partition number.  We will set a partition delimiter variable P set
  # either to an empty string (default) or the needed delimiter.
  TESTRAID="$(echo $1 | cut -f 2 -d /)"
  case "$TESTRAID" in
  'amiraid' )
    P="p"
    ;;
  'ataraid' )
    P="p"
    ;;
  'cciss' )
    P="p"
    ;;
  'ida' )
    P="p"
    ;;
  'rd' )
    P="p"
    ;;
  *)
    P=""
    ;;
  esac
  parted $1 unit b print | while read parse ; do
    if [ ! -z $output ]; then
      line=$parse
      if [ ! "$(echo $line | cut -b1)" = "" ]; then
        gptpartition=${1}${P}$(echo $line | cut -f 1 -d ' ')
        gpttype="$(echo $line | cut -f 5 -d ' ')"
        if ( is_swap $gptpartition ); then
          fdisktype="Linux swap"
        elif [ "$gpttype" = "msdos" ]; then
          fdisktype="W95 FAT32"
        elif [ "$gpttype" = "ntfs" ]; then
          fdisktype="HPFS/NTFS"
        elif echo $gpttype | grep -q hfs ; then
          fdisktype="HFS+"
        else
          fdisktype=Linux
        fi
        gptstart="$(expr $(echo $line | cut -f 2 -d ' ' | tr -d B) / 1024)"
        gptend="$(expr $(echo $line | cut -f 3 -d ' ' | tr -d B) / 1024)"
        gptsize="$(expr $(echo $line | cut -f 4 -d ' ' | tr -d B) / 1024)"
        echo $gptpartition $gptstart $gptend $gptsize gp $fdisktype
      fi
    fi
    if echo $parse | grep -q "^Number" ; then
      output=true
    fi
  done
fi
}

list_scsi_gpt() {
  # find drive # 0 - 15
  DRV=`expr $1 / 16`
  NUM=`expr $1 % 16`
  if [ ! "$NUM" = "0" ]; then
    return
  fi
  if [ "$DRV" = "0" ]; then
    output_gpt_partitions /dev/sda
  elif [ "$DRV" = "1" ]; then
    output_gpt_partitions /dev/sdb
  elif [ "$DRV" = "2" ]; then
    output_gpt_partitions /dev/sdc
  elif [ "$DRV" = "3" ]; then
    output_gpt_partitions /dev/sdd
  elif [ "$DRV" = "4" ]; then
    output_gpt_partitions /dev/sde
  elif [ "$DRV" = "5" ]; then
    output_gpt_partitions /dev/sdf
  elif [ "$DRV" = "6" ]; then
    output_gpt_partitions /dev/sdg
  elif [ "$DRV" = "7" ]; then
    output_gpt_partitions /dev/sdh
  elif [ "$DRV" = "8" ]; then
    output_gpt_partitions /dev/sdi
  elif [ "$DRV" = "9" ]; then
    output_gpt_partitions /dev/sdj
  elif [ "$DRV" = "10" ]; then
    output_gpt_partitions /dev/sdk
  elif [ "$DRV" = "11" ]; then
    output_gpt_partitions /dev/sdl
  elif [ "$DRV" = "12" ]; then
    output_gpt_partitions /dev/sdm
  elif [ "$DRV" = "13" ]; then
    output_gpt_partitions /dev/sdn
  elif [ "$DRV" = "14" ]; then
    output_gpt_partitions /dev/sdo
  elif [ "$DRV" = "15" ]; then
    output_gpt_partitions /dev/sdp
  fi
}

# List any volumes created by dmraid
list_dmraid() {
  if ! dmraid -s 2> /dev/null | grep -q "no raid" ; then
    for d in $(dmraid -sa -cr 2> /dev/null); do
      # dmraid always uses char 'p' between device name and partition number.
      # fdisk uses char 'p' only when device name ends in a digit. fixing
      if echo $d | grep -q '[0-9]$' ; then
        fdisk -l /dev/mapper/$d >> $TMP/SeTfdisk
      else # add 'p'
        fdisk -l /dev/mapper/$d | sed -e "s/^\(\/dev\/mapper\/$d\)\([0-9].*\)/\1p\2/" >> $TMP/SeTfdisk
      fi
    done
  fi
}

# List the LVM volumes:
list_lvm

# List CRYPT volumes:
list_crypt

# List dmraid volumes:
list_dmraid

# List GPT partitions:
cat /proc/partitions | while read line ; do
  SMASHED_LINE=$line
  MAJOR=`echo $SMASHED_LINE | cut -f 1 -d ' '`
  MINOR=`echo $SMASHED_LINE | cut -f 2 -d ' '`
  if [ "$MAJOR" = "8" ]; then
    list_scsi_gpt $MINOR
  fi
done

# Other partitions:
if cat /proc/partitions | grep / 1> /dev/null 2> /dev/null ; then # new
  cat /proc/partitions | grep / | while read line ; do
    SMASHED_LINE=$line
    MAJOR=`echo $SMASHED_LINE | cut -f 1 -d ' '`
    MINOR=`echo $SMASHED_LINE | cut -f 2 -d ' '`
    if [ "$MAJOR" = "3" ]; then
      list_ide $MAJOR $MINOR hda hdb
    elif [ "$MAJOR" = "8" ]; then
      list_scsi $MINOR
    elif [ "$MAJOR" = "9" ]; then
      list_md `echo $SMASHED_LINE | cut -f 3 -d ' ' | tr -d '/'` \
              `echo $SMASHED_LINE | cut -f 4 -d ' '`
    elif [ "$MAJOR" = "22" ]; then
      list_ide $MAJOR $MINOR hdc hdd
    elif [ "$MAJOR" = "33" ]; then
      list_ide $MAJOR $MINOR hde hdf
    elif [ "$MAJOR" = "34" ]; then
      list_ide $MAJOR $MINOR hdg hdh
    elif [ "$MAJOR" = "48" ]; then
      list_rd 0 $MINOR
    elif [ "$MAJOR" = "49" ]; then
      list_rd 1 $MINOR
    elif [ "$MAJOR" = "50" ]; then
      list_rd 2 $MINOR
    elif [ "$MAJOR" = "51" ]; then
      list_rd 3 $MINOR
    elif [ "$MAJOR" = "52" ]; then
      list_rd 4 $MINOR
    elif [ "$MAJOR" = "53" ]; then
      list_rd 5 $MINOR
    elif [ "$MAJOR" = "54" ]; then
      list_rd 6 $MINOR
    elif [ "$MAJOR" = "55" ]; then
      list_rd 7 $MINOR
    elif [ "$MAJOR" = "56" ]; then
      list_ide $MAJOR $MINOR hdi hdj
    elif [ "$MAJOR" = "57" ]; then
      list_ide $MAJOR $MINOR hdk hdl
    elif [ "$MAJOR" = "72" ]; then
      list_ida 0 $MINOR
    elif [ "$MAJOR" = "73" ]; then
      list_ida 1 $MINOR
    elif [ "$MAJOR" = "74" ]; then
      list_ida 2 $MINOR
    elif [ "$MAJOR" = "75" ]; then
      list_ida 3 $MINOR
    elif [ "$MAJOR" = "76" ]; then
      list_ida 4 $MINOR
    elif [ "$MAJOR" = "77" ]; then
      list_ida 5 $MINOR
    elif [ "$MAJOR" = "78" ]; then
      list_ida 6 $MINOR
    elif [ "$MAJOR" = "79" ]; then
      list_ida 7 $MINOR
    elif [ "$MAJOR" = "80" ]; then
      list_ide $MAJOR $MINOR hdm hdn
    elif [ "$MAJOR" = "89" ]; then
      list_ide $MAJOR $MINOR hdo hdp
    elif [ "$MAJOR" = "90" ]; then
      list_ide $MAJOR $MINOR hdq hdr
    elif [ "$MAJOR" = "91" ]; then
      list_ide $MAJOR $MINOR hds hdt
    elif [ "$MAJOR" = "101" ]; then
      list_amiraid $MAJOR $MINOR
    elif [ "$MAJOR" = "104" \
      -o "$MAJOR" = "105" \
      -o "$MAJOR" = "106" \
      -o "$MAJOR" = "107" \
      -o "$MAJOR" = "108" \
      -o "$MAJOR" = "109" \
      -o "$MAJOR" = "110" \
      -o "$MAJOR" = "111" ]; then
      list_cciss $(( $MAJOR - 104 )) $MINOR
    elif [ "$MAJOR" = "114" ]; then
      list_ataraid $MAJOR $MINOR
    fi
  done
else # old format and no RAID:
  if cat /proc/partitions | grep md 1> /dev/null 2> /dev/null ; then
    cat /proc/partitions | grep md | while read line ; do
      SMASHED_LINE=$line
      MAJOR=`echo $SMASHED_LINE | cut -f 1 -d ' '`
      if [ "$MAJOR" = "9" ]; then
        list_md `echo $SMASHED_LINE | cut -f 3 -d ' ' | tr -d '/'` \
                `echo $SMASHED_LINE | cut -f 4 -d ' '`
      fi
    done
  fi

  fdisk -l 2> /dev/null | grep -v "ee  GPT" > $TMP/SeTfdisk2
  # we handle dmraid volumes separately, remove references here
  if ! dmraid -s 2> /dev/null | grep -q "no raid" ; then
    for d in $(dmraid -sa -cr 2> /dev/null); do
      cat $TMP/SeTfdisk2 | grep -v "/dev/mapper/$d" > $TMP/SeTfdisk1
      cat $TMP/SeTfdisk1 > $TMP/SeTfdisk2
    done
    rm -f $TMP/SeTfdisk1
  fi
  cat $TMP/SeTfdisk2 >> $TMP/SeTfdisk
  rm -f $TMP/SeTfdisk2
fi

# remove the disks underlying active dmraid volumes
if ! dmraid -s 2> /dev/null | grep -q "no raid" ; then
  for d in $(dmraid -sa -g -cr 2> /dev/null); do
    for p in $(dmraid -r --sep : -cp:r 2> /dev/null | grep "$d" | cut -f 1 -d : ); do
      cat $TMP/SeTfdisk | grep -v "$p" > $TMP/SeTfdisk1
      cat $TMP/SeTfdisk1 > $TMP/SeTfdisk
    done
  done
  rm -f $TMP/SeTfdisk1
fi

cat $TMP/SeTfdisk
EOF_DMRAID

cat > /usr/lib/setup/SeTpartitions <<'EOF_DMRAID'
#!/bin/sh
# SeTpartition user-friendly rewrite Fri Dec 15 13:17:40 CST 1995 pjv
# Rewrite to support filesystem plugins <david@slackware.com>, 07-May-2001
# Don't use plugins, make it work, pjv, 18-May-2001.
# Generalize tempscript creation and support JFS and XFS. pjv, 30-Mar-2002

TMP=/var/log/setup/tmp
NDIR=/dev/null
REDIR=/dev/tty4
T_PX=/mnt

# FUNCTIONS

# crunch() -  remove extra whitespace
crunch () {
   read STRING;
   echo $STRING
}

# make_btrfs( dev ) - Create a new btrfs filesystem on the named dev.
# Parameters:  dev     Device node to format.
make_btrfs() {
   # get the size of the named partition
   SIZE=`get_part_size $1`
   # output a nice status message
   dialog --title "FORMATTING" \
   --backtitle "Formatting $1 with filesystem btrfs." \
   --infobox "Formatting $1  \n\
Size in 1K blocks: $SIZE \n\
Filesystem type: btrfs " 0 0
   # do the format
   if mount | grep "$1 " 1> $NDIR 2> $NDIR ; then
      umount $1 2> $NDIR
   fi
   mkfs.btrfs $1 1> $REDIR 2> $REDIR
}

# make_ext2( dev, check ) - Create a new ext2 filesystem on the named
#                                device.
# Parameters:  dev     Device node to format.
#              check   Perform fs check (y or n)
make_ext2() {
   # get the size of the named partition
   SIZE=`get_part_size $1`
   # output a nice status message
   dialog --title "FORMATTING" \
   --backtitle "Formatting $1 with filesystem ext2." \
   --infobox "Formatting $1  \n\
Size in 1K blocks: $SIZE \n\
Filesystem type: ext2" 0 0
   # do the format
   if mount | grep "$1 " 1> $NDIR 2> $NDIR ; then
      umount $1 2> $NDIR
   fi
   if [ "$2" = "y" ]; then
      mkfs.ext2 -c $1 1> $REDIR 2> $REDIR
   else
      mkfs.ext2 $1 1> $REDIR 2> $REDIR
   fi
}

# make_ext3( dev, check ) - Create a new ext3 filesystem on the named
#                                device.
# Parameters:  dev     Device node to format.
#              check   Perform fs check (y or n)
make_ext3() {
   # get the size of the named partition
   SIZE=`get_part_size $1`
   # output a nice status message
   dialog --title "FORMATTING" \
   --backtitle "Formatting $1 with filesystem ext3." \
   --infobox "Formatting $1  \n\
Size in 1K blocks: $SIZE \n\
Filesystem type: ext3" 0 0
   # do the format
   if mount | grep "$1 " 1> $NDIR 2> $NDIR ; then
      umount $1 2> $NDIR
   fi
   if [ "$2" = "y" ]; then
      mkfs.ext3 -c $1 1> $REDIR 2> $REDIR
   else
      mkfs.ext3 $1 1> $REDIR 2> $REDIR
   fi
}

# make_ext4( dev, check ) - Create a new ext4 filesystem on the named
#                                device.
# Parameters:  dev     Device node to format.
#              check   Perform fs check (y or n)
make_ext4() {
   # get the size of the named partition
   SIZE=`get_part_size $1`
   # output a nice status message
   dialog --title "FORMATTING" \
   --backtitle "Formatting $1 with filesystem ext4." \
   --infobox "Formatting $1  \n\
Size in 1K blocks: $SIZE \n\
Filesystem type: ext4" 0 0
   # do the format
   if mount | grep "$1 " 1> $NDIR 2> $NDIR ; then
      umount $1 2> $NDIR
   fi
   if [ "$2" = "y" ]; then
      mkfs.ext4 -c $1 1> $REDIR 2> $REDIR
   else
      mkfs.ext4 $1 1> $REDIR 2> $REDIR
   fi
}

# make_jfs( dev, check ) - Create a new jfs filesystem on the named
#                                device.
# Parameters:  dev     Device node to format.
#              check   Perform fs check (y or n)
make_jfs() {
   # get the size of the named partition
   SIZE=`get_part_size $1`
   # output a nice status message
   dialog --title "FORMATTING" \
   --backtitle "Formatting $1 with filesystem jfs." \
   --infobox "Formatting $1  \n\
Size in 1K blocks: $SIZE \n\
Filesystem type: jfs" 0 0
   # do the format
   if mount | grep "$1 " 1> $NDIR 2> $NDIR ; then
      umount $1 2> $NDIR
   fi
   if [ "$2" = "y" ]; then
      mkfs.jfs -c -q $1 1> $REDIR 2> $REDIR
   else
      mkfs.jfs -q $1 1> $REDIR 2> $REDIR
   fi
}

# make_reiserfs( dev ) - Create a new reiserfs filesystem on the named dev.
# Parameters:  dev     Device node to format.
make_reiserfs() {
   # get the size of the named partition
   SIZE=`get_part_size $1`
   # output a nice status message
   dialog --title "FORMATTING" \
   --backtitle "Formatting $1 with filesystem reiserfs." \
   --infobox "Formatting $1  \n\
Size in 1K blocks: $SIZE \n\
Filesystem type: reiserfs " 0 0
   # do the format
   if mount | grep "$1 " 1> $NDIR 2> $NDIR ; then
      umount $1 2> $NDIR
   fi
   echo "y" | mkreiserfs $1 1> $REDIR 2> $REDIR
}

# make_xfs( dev ) - Create a new xfs filesystem on the named dev
# Parameters:  dev     Device node to format.
make_xfs() {
   # get the size of the named partition
   SIZE=`get_part_size $1`
   # output a nice status message
   dialog --title "FORMATTING" \
   --backtitle "Formatting $1 with filesystem xfs." \
   --infobox "Formatting $1  \n\
Size in 1K blocks: $SIZE \n\
Filesystem type: xfs " 0 0
   # do the format
   if mount | grep "$1 " 1> $NDIR 2> $NDIR ; then
      umount $1 2> $NDIR
   fi
   mkfs.xfs -f $1 1> $REDIR 2> $REDIR
}

# gen_part_list() - Prints out a partition listing for the system into the
gen_part_list() {
   export COUNT=0
   cat $TMP/SeTplist | while [ 0 ]; do
      read PARTITION;
      if [ "$PARTITION" = "" ]; then
         break;
      fi
      # Variables, variables, variables
      NAME=`echo $PARTITION | crunch | cut -f 1 -d ' '`
      SIZE=`echo "$PARTITION" | tr -d "*" | tr -d "+" | crunch | cut -f 4 -d ' '`
      ALTNAME=""
      DEVICE=`echo "$PARTITION" | tr -d "*" | crunch | cut -f 1 -d ' '`
      # See if this partition is in use already
      if grep "$DEVICE " $TMP/SeTnative 1> $NDIR; then # it's been used
         ON=`grep "$DEVICE " $TMP/SeTnative | crunch | cut -f 2 -d ' '`
         ALTNAME="$DEVICE on $ON Linux ${SIZE}K"
      fi
      # Add a menu item
      if [ "$ALTNAME" = "" ]; then
         echo "\"$NAME\" \"Linux ${SIZE}K\" \\" >> $TMP/tempscript
         echo "false" > $TMP/SeTSKIP # this flag is used for non-root parts
      else
         echo "\"(IN USE)\" \"$ALTNAME\" \\" >> $TMP/tempscript
      fi
   done
   echo "\"---\" \"(done, continue with setup)\" \\" >> $TMP/tempscript
   echo "\"---\" \"(done, continue with setup)\" \\" >> $TMP/tempscript
   echo "\"---\" \"(done, continue with setup)\" \\" >> $TMP/tempscript
   echo "\"---\" \"(done, continue with setup)\" \\" >> $TMP/tempscript
   echo "\"---\" \"(done, continue with setup)\" \\" >> $TMP/tempscript
   echo "2> $TMP/return" >> $TMP/tempscript
}

# ask_format( dev ) - Asks the user if he/she wants to format the named device
ask_format() {
   dialog --backtitle "Do you want to format Linux partition ${1}?" \
   --title "FORMAT PARTITION $1" --menu "If this partition has \
not been formatted, you should format it.  NOTE: This will erase all data on \
it.  Would you like \
to format this partition?" 12 70 3 \
   "Format" "Quick format with no bad block checking" \
   "Check" "Slow format that checks for bad blocks" \
   "No" "No, do not format this partition" 2> $TMP/return
   if [ ! $? = 0 ]; then
     rm -f $TMP/return
     exit
   fi
}

# ask_fs( dev ) - Asks the user the type of filesystem to use for the named
#                 device.  Answer in $TMP/return
ask_fs() {
  unset BTRFS EXT2 EXT3 JFS REISERFS XFS
  if grep -wq ext2 /proc/filesystems 1> $NDIR 2> $NDIR ; then
    EXT2="Ext2 is the traditional Linux file system and is fast and stable. "
  fi
  if grep -wq ext3 /proc/filesystems 1> $NDIR 2> $NDIR ; then
    EXT3="Ext3 is the journaling version of the Ext2 filesystem. "
    DEFAULT=ext3
  fi
  if grep -wq ext4 /proc/filesystems 1> $NDIR 2> $NDIR ; then
    EXT4="Ext4 is the successor to the ext3 filesystem. "
    DEFAULT=ext4
  fi
  if grep -wq reiserfs /proc/filesystems 1> $NDIR 2> $NDIR ; then
    REISERFS="ReiserFS is a journaling filesystem that stores all files and filenames in a balanced tree structure. "
  fi
  if grep -wq btrfs /proc/filesystems 1> $NDIR 2> $NDIR ; then
    BTRFS="Btrfs is a B-tree copy-on-write filesystem. "
  fi
  # These last two will only be present if the user asked for a special kernel.
  # They should probably be the default in that case.
  if grep -wq jfs /proc/filesystems 1> $NDIR 2> $NDIR ; then
    JFS="JFS is IBM's Journaled Filesystem, currently used in IBM enterprise servers. "
  fi
  if grep -wq xfs /proc/filesystems 1> $NDIR 2> $NDIR ; then
    XFS="XFS is SGI's journaling filesystem that originated on IRIX. "
  fi
  cat << EOF > $TMP/tempscript
dialog --title "SELECT FILESYSTEM FOR $1" \\
--backtitle "Partition $1 will be formatted." \\
--default-item $DEFAULT --menu \\
"Please select the type of filesystem to use for the specified \\
device.  Here are descriptions of the available filesystems:  $EXT2 $EXT3 $EXT4 $JFS $REISERFS $XFS" \\
0 0 0 \\
EOF
  if [ ! "$EXT2" = "" ]; then
    echo "\"ext2\" \"Standard Linux Ext2 Filesystem\" \\" >> $TMP/tempscript
  fi
  if [ ! "$EXT3" = "" ]; then
    echo "\"ext3\" \"Ext3 Journaling Filesystem\" \\" >> $TMP/tempscript
  fi
  if [ ! "$EXT4" = "" ]; then
    echo "\"ext4\" \"Ext4 Journaling Filesystem\" \\" >> $TMP/tempscript
  fi
  if [ ! "$JFS" = "" ]; then
    echo "\"jfs\" \"IBM's Journaled Filesystem\" \\" >> $TMP/tempscript
  fi
  if [ ! "$REISERFS" = "" ]; then
    echo "\"reiserfs\" \"ReiserFS Journaling Filesystem\" \\" >> $TMP/tempscript
  fi
  if [ ! "$BTRFS" = "" ]; then
    echo "\"btrfs\" \"Btrfs Copy-on-Write B-tree Filesystem\" \\" >> $TMP/tempscript
  fi
  if [ ! "$XFS" = "" ]; then
    echo "\"xfs\" \"SGI's Journaling Filesystem\" \\" >> $TMP/tempscript
  fi
  echo "2> $TMP/return" >> $TMP/tempscript
  . $TMP/tempscript
  if [ ! $? = 0 ]; then
    rm -f $TMP/return
    exit
  fi
}

# get_part_size( dev ) - Return the size in KB of the named partition.
get_part_size() {
   Size=`probe -l 2> /dev/null | grep "$1 " | tr -d "*" | tr -d "+" | crunch | cut -f 4 -d ' '`
   echo $Size
}

# MAIN

if [ ! -d $TMP ]; then
   mkdir -p $TMP
fi

if [ ! -r $TMP/SeTplist ]; then
   # Give warning?
   exit
fi

cat /dev/null >> $TMP/SeTnative

cat << EOF > $TMP/tempscript
dialog --backtitle "Setting up root Linux partition." \\
--title "Select Linux installation partition:" --ok-label Select --cancel-label Continue --menu \\
"Please select a partition from the following list to use for your \\
root (/) Linux partition." 13 70 5 \\
EOF
gen_part_list

. $TMP/tempscript
if [ ! $? = 0 ]; then
   rm $TMP/tempscript
   exit 255 # user abort
fi

ROOT_DEVICE="`cat $TMP/return`"
rm $TMP/tempscript
if [ "$ROOT_DEVICE" = "---" ]; then
   exit 255
fi

# format root partition?
ask_format $ROOT_DEVICE
DOFORMAT="`cat $TMP/return`"
rm -f $TMP/return
if [ ! "$DOFORMAT" = "No" ]; then
  ask_fs $ROOT_DEVICE
  ROOT_SYS_TYPE="`cat $TMP/return`"
  rm -f $TMP/return
  # create the filesystem
  if [ "$ROOT_SYS_TYPE" = "ext2" ]; then
    if [ "$DOFORMAT" = "Check" ]; then
      make_ext2 $ROOT_DEVICE "y"
    else
      make_ext2 $ROOT_DEVICE "n"
    fi
  elif [ "$ROOT_SYS_TYPE" = "ext3" ]; then
    if [ "$DOFORMAT" = "Check" ]; then
      make_ext3 $ROOT_DEVICE "y"
    else
      make_ext3 $ROOT_DEVICE "n"
    fi
  elif [ "$ROOT_SYS_TYPE" = "ext4" ]; then
    if [ "$DOFORMAT" = "Check" ]; then
      make_ext4 $ROOT_DEVICE "y"
    else
      make_ext4 $ROOT_DEVICE "n"
    fi
  elif [ "$ROOT_SYS_TYPE" = "reiserfs" ]; then
    make_reiserfs $ROOT_DEVICE
  elif [ "$ROOT_SYS_TYPE" = "btrfs" ]; then
    make_btrfs $ROOT_DEVICE
  elif [ "$ROOT_SYS_TYPE" = "jfs" ]; then
    if [ "$DOFORMAT" = "Check" ]; then
      make_jfs $ROOT_DEVICE "y"
    else
      make_jfs $ROOT_DEVICE "n"
    fi
  elif [ "$ROOT_SYS_TYPE" = "xfs" ]; then
    make_xfs $ROOT_DEVICE
  fi
fi # DOFORMAT?

# Sync before identifying and mounting the root device:
sync

# Determine the filesystem type using blkid:
ROOT_SYS_TYPE=$(blkid -s TYPE $ROOT_DEVICE | cut -f 2 -d = | tr -d \")

# Mount the root filesystem:
mount $ROOT_DEVICE $T_PX -t $ROOT_SYS_TYPE 1> $REDIR 2> $REDIR

#echo "$ROOT_DEVICE       /        $ROOT_SYS_TYPE        defaults   1   1" > $TMP/SeTnative
printf "%-16s %-16s %-11s %-16s %-3s %s\n" "$ROOT_DEVICE" "/" "$ROOT_SYS_TYPE" "defaults" "1" "1" > $TMP/SeTnative
echo $ROOT_DEVICE > $TMP/SeTrootdev

# done mounting the target root partition

# More than one Linux partition
if [ ! "`cat $TMP/SeTplist | sed -n '2 p'`" = "" ]; then
   while [ 0 ]; do # next partition loop
      # OK, we will set this flag, and if we find an unused partition, we
      # change it.  If it doesn't get switched, we skip the next menu.
      rm -f $TMP/SeTSKIP
      echo "true" > $TMP/SeTSKIP

      cat << EOF > $TMP/tempscript
      dialog --backtitle "Setting up other Linux partitions." \\
--title "Select other Linux partitions for /etc/fstab" \\
--ok-label Select --cancel-label Continue \\
--menu "You seem to have more than one partition tagged as type Linux.  \\
You may use these to distribute your Linux system across more than \\
one partition.  Currently, you have $ROOT_DEVICE mounted as your / partition.  \\
You might want to mount directories such as /home or /usr/local \\
on separate partitions.  You should not try to mount /etc, /sbin, or /bin on \\
their own partitions since they contain utilities needed to bring the system \\
up and mount partitions.  Also, do not reuse a partition that you've already \\
entered before.  Please select one of the Linux partitions listed below, or \\
if you're done, hit <Continue>." 20 70 4 \\
EOF
      gen_part_list

      if [ "`cat $TMP/SeTSKIP`" = "true" ]; then
         break;
      fi
      rm -rf $TMP/return

      . $TMP/tempscript
      if [ ! $? = 0 ]; then
         break;
      fi

      NEXT_PARTITION=`cat $TMP/return`
      if [ "$NEXT_PARTITION" = "---" ]; then
         break;
      elif [ "$NEXT_PARTITION" = "(IN USE)" ]; then
         continue;
      fi

      # We now have the next partition, ask the user what to do with it:
      ask_format $NEXT_PARTITION

      DOFORMAT="`cat $TMP/return`"
      rm -f $TMP/return

      BACKT="Partition $NEXT_PARTITION will not be reformatted."
      if [ ! "$DOFORMAT" = "No" ]; then
        ask_fs $NEXT_PARTITION
        NEXT_SYS_TYPE="`cat $TMP/return`"
        rm -f $TMP/return
        BACKT="Partition $NEXT_PARTITION will be formatted with $NEXT_SYS_TYPE."
        # create the filesystem
        if [ "$NEXT_SYS_TYPE" = "ext2" ]; then
          if [ "$DOFORMAT" = "Check" ]; then
            make_ext2 $NEXT_PARTITION "y"
          else
            make_ext2 $NEXT_PARTITION "n"
          fi
        elif [ "$NEXT_SYS_TYPE" = "ext3" ]; then
          if [ "$DOFORMAT" = "Check" ]; then
            make_ext3 $NEXT_PARTITION "y"
          else
            make_ext3 $NEXT_PARTITION "n"
          fi
        elif [ "$NEXT_SYS_TYPE" = "ext4" ]; then
          if [ "$DOFORMAT" = "Check" ]; then
            make_ext4 $NEXT_PARTITION "y"
          else
            make_ext4 $NEXT_PARTITION "n"
          fi
        elif [ "$NEXT_SYS_TYPE" = "reiserfs" ]; then
          make_reiserfs $NEXT_PARTITION
        elif [ "$NEXT_SYS_TYPE" = "btrfs" ]; then
          make_btrfs $NEXT_PARTITION
        elif [ "$NEXT_SYS_TYPE" = "jfs" ]; then
          if [ "$DOFORMAT" = "Check" ]; then
            make_jfs $NEXT_PARTITION "y"
          else
            make_jfs $NEXT_PARTITION "n"
          fi
        elif [ "$NEXT_SYS_TYPE" = "xfs" ]; then
          make_xfs $NEXT_PARTITION
        fi
      fi # DOFORMAT?

      # Now ask the user where to mount this new filesystem:
      dialog --backtitle "$BACKT" --title \
"SELECT MOUNT POINT FOR $NEXT_PARTITION" --inputbox \
"OK, now you need to specify where you want the new partition mounted.  \
For example, if you want to put it under /usr/local, then respond:  /usr/local\n\
Where would you like to mount $NEXT_PARTITION?" 11 59 2> $TMP/return
      if [ ! $? = 0 ]; then
         continue
      fi

      MTPT=`cat $TMP/return`
      if [ "$MTPT" = "" ]; then # abort if blank
         continue
      fi
      if [ "`echo "$MTPT" | cut -b1`" = " " ]; then # bail if 1st char is space
         continue
      fi
      if [ ! "`echo "$MTPT" | cut -b1`" = "/" ]; then # add / to start of path
         MTPT="/$MTPT"
      fi
      rm $TMP/return

      # Sync before identifying and mounting the partition:
      sync
      
      # Create the mount point if it does not exist:
      if [ ! -d /mnt/$MTPT ]; then
         mkdir -p /mnt/$MTPT
      fi

      # Determine the filesystem type using blkid:
      NEXT_SYS_TYPE=$(blkid -s TYPE $NEXT_PARTITION | cut -f 2 -d = | tr -d \")

      # Mount the partition:
      mount $NEXT_PARTITION /mnt/$MTPT -t $NEXT_SYS_TYPE 1> $REDIR 2> $REDIR

      #echo "$NEXT_PARTITION       $MTPT      $NEXT_SYS_TYPE        defaults   1   1" >> $TMP/SeTnative
      printf "%-16s %-16s %-11s %-16s %-3s %s\n" "$NEXT_PARTITION" "$MTPT" "$NEXT_SYS_TYPE" "defaults" "1" "2" >> $TMP/SeTnative
   done # next partition loop
fi # more than one Linux partition

# Done, report to the user:
cat << EOF > $TMP/tempmsg

Adding this information to your /etc/fstab:

EOF
cat $TMP/SeTnative >> $TMP/tempmsg
dialog --backtitle "Finished setting up Linux partitions." \
--title "DONE ADDING LINUX PARTITIONS TO /etc/fstab" \
--exit-label OK \
--textbox $TMP/tempmsg 15 72

# Now, move our /tmp storage onto the target partition if possible:
/usr/lib/setup/migrate.sh

EOF_DMRAID

# busybox patch removes executable bit for some reason??
for f in $PATCH_LIST; do
  chmod +x $f;
done

cd $PWD
