#!/bin/sh
TMP=/var/log/setup/tmp
if [ ! -d $TMP ]; then
  mkdir -p $TMP
fi
REDIR=/dev/tty4
NDIR=/dev/null
crunch () { # remove extra whitespace
 read STRING;
 echo $STRING
}
T_PX=/
if [ ! -r $TMP/SeTplist ]; then
 # Give warning?
 exit
fi
cat << EOF > $TMP/tempscript
dialog --title "Select Linux installation partition:" --menu \\
"Please select a partition from the following list to use for your \\
root (/) Linux partition." 13 70 5 \\
EOF
export COUNT=0
cat $TMP/SeTplist | while [ 0 ]; do
  read PARTITION;
  if [ "$PARTITION" = "" ]; then
    break;
  fi
  NAME=`echo $PARTITION | crunch | cut -f 1 -d ' '`
  SIZE=`echo "$PARTITION" | tr -d "*" | tr -d "+" | crunch | cut -f 4 -d ' '`
  echo "\"$NAME\" \"Linux native ${SIZE}K\" \\" >> $TMP/tempscript
done
echo "\"---\" \"(add none, continue with setup)\" \\" >> $TMP/tempscript
echo "\"---\" \"(add none, continue with setup)\" \\" >> $TMP/tempscript
echo "\"---\" \"(add none, continue with setup)\" \\" >> $TMP/tempscript
echo "\"---\" \"(add none, continue with setup)\" \\" >> $TMP/tempscript
echo "\"---\" \"(add none, continue with setup)\" \\" >> $TMP/tempscript
echo "2> $TMP/return" >> $TMP/tempscript
. $TMP/tempscript
if [ $? = 1 -o $? = 255 ]; then
  rm $TMP/tempscript
  exit 255 # user abort
fi
ROOT_DEVICE="`cat $TMP/return`"
rm $TMP/tempscript
if [ "$ROOT_DEVICE" = "---" ]; then
  exit 255
fi
ROOT_SYS_TYPE=ext2
ROOT_SIZE=`probe -l | fgrep "$ROOT_DEVICE " | tr -d "*" | tr -d "+" | crunch | cut -f 4 -d ' '`



sync
mount -t $ROOT_SYS_TYPE $ROOT_DEVICE $T_PX 1> $REDIR 2> $REDIR
echo "$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 --title "Select other Linux partitions for /etc/fstab" \\
--menu "You seem to have more than one partition tagged as Linux \\
native. 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 seperate 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 \\
hit <Cancel> to continue." 20 70 4 \\
EOF
    cat $TMP/SeTplist | while [ 0 ]; do
      read PARTITION;
      if [ "$PARTITION" = "" ]; then
        break;
      fi
      SIZE=`echo "$PARTITION" | tr -d "*" | tr -d "+" | crunch | cut -f 4 -d ' '`
      ALTNAME=""
      DEVICE=`echo "$PARTITION" | tr -d "*" | crunch | cut -f 1 -d ' '`
      if fgrep "$DEVICE " $TMP/SeTnative 1> $NDIR; then # it's been used
        ON=`fgrep "$DEVICE " $TMP/SeTnative | crunch | cut -f 2 -d ' '`
        ALTNAME="$DEVICE on $ON Linux native ${SIZE}K"
      fi
      NAME=`echo $PARTITION | crunch | cut -f 1 -d ' '`
      if [ "$ALTNAME" = "" ]; then
        echo "\"$NAME\" \"Linux native ${SIZE}K\" \\" >> $TMP/tempscript
        echo "false" > $TMP/SeTSKIP
      else
        echo "\"(IN USE)\" \"$ALTNAME\" \\" >> $TMP/tempscript
      fi
    done
    echo "\"---\" \"(add none, continue with setup)\" \\" >> $TMP/tempscript
    echo "\"---\" \"(add none, continue with setup)\" \\" >> $TMP/tempscript
    echo "\"---\" \"(add none, continue with setup)\" \\" >> $TMP/tempscript
    echo "\"---\" \"(add none, continue with setup)\" \\" >> $TMP/tempscript
    echo "2> $TMP/return" >> $TMP/tempscript
    if [ "`cat $TMP/SeTSKIP`" = "true" ]; then
      break;
    fi
    rm -f $TMP/return
    . $TMP/tempscript
    if [ $? = 1 -o $? = 255 ]; then
      break;
    fi
    NEXT_PARTITION=`cat $TMP/return`
    if [ "$NEXT_PARTITION" = "---" ]; then
      break;
    elif [ "$NEXT_PARTITION" = "(IN USE)" ]; then
      continue;
    fi 
    SIZE=`probe -l | fgrep "$NEXT_PARTITION " | tr -d "*" | tr -d "+" | crunch | cut -f 4 -d ' '`
    dialog --title "FORMAT PARTITION $NEXT_PARTITION" --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/format
    if [ $? = 1 -o $? = 255 ]; then
      rm -f $TMP/format
      continue
    fi
    DOFORMAT="`cat $TMP/format`"
    rm -f $TMP/format
    if [ ! "$DOFORMAT" = "No" ]; then
      dialog --title "SELECT INODE DENSITY FOR $NEXT_PARTITION" --menu \
"Ext2fs defaults to one inode per 4096 bytes of drive space. If you're \
going to have many small files on your drive, then you may need more \
inodes (one is used for each file entry). You can change the density to \
one inode per 2048 bytes, or even per 1024 bytes.  Select '2048' or \
'1024', or just hit enter to accept the default of 4096 bytes.  NOTE: \
If you are going to run from CD using a small (<60MB) partition, use \
1024 to be safe.  Each link uses an inode and it's easy to \
run out of space." 18 63 3 \
"4096" "1 inode per 4096 bytes. (default)" \
"2048" "1 inode per 2048 bytes." \
"1024" "1 inode per 1024 bytes." 2> $TMP/density
      if [ $? = 1 -o $? = 255 ]; then
        rm -f $TMP/density
        continue
      fi
      DENSITY="`cat $TMP/density`"
      rm -f $TMP/density
      if [ ! "$DENSITY" = "2048" -a ! "$DENSITY" = "1024" ]; then
        DENSITY=4096
      fi
      INODE_DENSITY="Inode density: 1 inode per $DENSITY bytes."
      dialog --title "FORMATTING" --infobox "Formatting $NEXT_PARTITION  \n\
  Size in 1K blocks: $SIZE \n\
  Filesystem type: ext2 \n\
  $INODE_DENSITY " 6 45
      if mount | fgrep "$NEXT_PARTITION " 1> $NDIR 2> $NDIR ; then
        umount $ROOT_DEVICE 2> $NDIR
      fi
      if [ "$DOFORMAT" = "Check" ]; then
        mke2fs -c -i $DENSITY $NEXT_PARTITION 1> $REDIR 2> $REDIR
      else
        mke2fs -i $DENSITY $NEXT_PARTITION 1> $REDIR 2> $REDIR
      fi
    fi # doformat?
    dialog --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 Where would you like to mount $NEXT_PARTITION?" 11 54 2> $TMP/return
    if [ $? = 255 -o $? = 1 ]; 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
    # Now, we need to mount the newly selected device:
    sync
    if [ ! -d /mnt/$MTPT ]; then
      mkdir -p /mnt/$MTPT
    fi
    mount -t ext2 $NEXT_PARTITION /mnt/$MTPT 1> $REDIR 2> $REDIR
    echo "$NEXT_PARTITION       $MTPT        ext2        defaults   1   1" >> $TMP/SeTnative
  done # next partition loop
fi # more than one Linux partition
cat << EOF > $TMP/tempmsg

Adding this information to your /etc/fstab:

EOF
cat $TMP/SeTnative >> $TMP/tempmsg
dialog --title "DONE ADDING LINUX PARTITIONS TO /etc/fstab" \
--textbox $TMP/tempmsg 15 70

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