#!/bin/sh

expand_mes() {
    source /usr/lib/setup/SeTDOS.mes
}

PROBE="/sbin/fdisk"
TMP="/tmp"

# Add DOS partitions.
rm -f /tmp/SeTDOS

LANG=C $PROBE -l | grep '^/dev' > /tmp/probedlist$$
# cat GPT-partlist.dat NTFS-partlist.dat > /tmp/probedlist$$

WINLIST=`cat /tmp/probedlist$$ | tr -d '*' | grep NTFS | awk '{print $1":"$7, $2"-"$3"("$5")"}'`
GPTLIST=`cat /tmp/probedlist$$ | tr -d '*' | grep 'Microsoft basic data' | awk '{print $1":"$6"_"$7"_"$8, $2"-"$3"("$5")"}'`

rm -f /tmp/probedlist$$

DOSLIST="`echo -e $WINLIST $GPTLIST`"
# echo "DOSLIST:$DOSLIST"

if [ ! "$DOSLIST" = "" ]; then # there are DOS or WinNT partitions:

    cat /dev/null > /tmp/SeTDOS
    while [ 0 ]; do
	expand_mes
	USED_SEDLIST=`cat /tmp/SeTDOS |awk '{print "-e s|"$1"|*DONE*"$1"("$2")| "}'`
	# echo "USED_SEDLIST:$USED_SEDLIST"
	sleep 5
      if [ -z "$USED_SEDLIST" ] ; then
	  DOSLISTOK="$DOSLIST"
      else
	  DOSLISTOK=`echo $DOSLIST|sed $USED_SEDLIST`
      fi
      dialog --title "select DOS/Windows/Windows NT partitions" --menu "$select_mes" 22 72 12 $DOSLISTOK "Quit" "$quit_list" 2> $TMP/reply$$
      if [ $? = 1 -o $? = 255 ]; then
	  rm -f $TMP/reply$$
	  exit
      fi
      REPLY="`cat $TMP/reply$$`"
      rm -f $TMP/reply$$
      if [ "$REPLY" = "Quit" ] ; then
	  exit
      else
	  ########
	  echo "REPLY:$REPLY"
	  NEXT_PARTITION="`echo $REPLY| cut -f1 -d':' | sed -e 's/\*DONE\*//' `"
	  if echo $REPLY | grep "DONE" 1> /dev/null 2> /dev/null ; then
	      # We have already added this partition.
              dialog --title "Error: ${NEXT_PARTITION} is already registered" --msgbox "$used_mes" 9 70
              continue;
	  fi
	  NEXT_FS="`echo $REPLY| cut -f2 -d':'`"
	  # echo "NEXT_PARTITION:$NEXT_PARTITION, NEXT_FS:$NEXT_FS"
	  # sleep 10
	  expand_mes
	  dialog --title "Enter Mount Point" --inputbox "$mp_mes" 15 66 2> /tmp/newdir$$
	  if [ $? = 1 ]; then
              rm -f /tmp/newdir$$ /tmp/SeTDOS
              exit
	  fi
	  NEW_DIR="`cat /tmp/newdir$$`"
	  rm -f /tmp/newdir$$
	  if [ ! "`echo $NEW_DIR | cut -b1`" = "/" ]; then
              NEW_DIR="/$NEW_DIR"
	  fi
	  mkdir -p /mnt$NEW_DIR
	  chmod 755 /mnt$NEW_DIR
	  echo > /tmp/tmpmsg$$
          echo "$append_01" >> /tmp/tmpmsg$$
          echo >> /tmp/tmpmsg$$
          echo "$NEXT_PARTITION        $NEW_DIR        ntfs        iocharset=utf-8,codepage=932  0   0" >> /tmp/tmpmsg$$
          echo "$NEXT_PARTITION        $NEW_DIR        ntfs        iocharset=utf-8,codepage=932  0   0" >> /tmp/SeTDOS
	  dialog --title "Confirm your /etc/fstab" --msgbox "`cat /tmp/tmpmsg$$`" 9 75
	  rm -f /tmp/tmpmsg$$
	  ########
      fi
  done # mounting NTFS partitions
fi
