#!/bin/sh

PROBE="/sbin/fdisk"

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

LANG=C $PROBE -l | grep '^/dev' > /tmp/probedlist$$

WINLIST=""
DOSLIST=`cat /tmp/probedlist$$ | tr -d '*' |grep FAT|awk '{print $1","$6""$7""$8" "$2"-"$3","$4"blocks,Id="$5}'`
HPFSLIST=`cat /tmp/probedlist$$ | tr -d '*' |grep NTFS|awk '{print $1","$6""$7""$8" "$2"-"$3","$4"blocks,Id="$5}'`
SFSLIST=`cat /tmp/probedlist$$ | tr -d '*' |grep SFS|awk '{print $1","$6""$7""$8" "$2"-"$3","$4"blocks,Id="$5}'`

rm -f /tmp/probedlist$$

if [ ! "$WINLIST" = "" ]; then
  DOSLIST="`echo -e "$DOSLIST $WINLIST"`"
fi

if [ ! "$HPFSLIST" = "" ]; then
  DOSLIST="`echo -e "$DOSLIST $HPFSLIST"`"
fi

if [ ! "$SFSLIST" = "" ]; then
  DOSLIST="`echo -e "$DOSLIST $SFSLIST"`"
fi

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

  cat /dev/null > /tmp/SeTDOS
  while [ 0 ]; do

    USED_SEDLIST=`cat /tmp/SeTDOS |awk '{print "-e s|"$1"|*DONE*"$1"("$2")| "}'`
    if [ -z "$USED_SEDLIST" ] ; then
      DOSLISTOK="$DOSLIST"
    else
      DOSLISTOK=`echo $DOSLIST|sed $USED_SEDLIST`
    fi
    dialog --title "DOS/Windows/Windows NTѡƥ" --menu "\
ΥѡƥΤLinux 鸫褦ˤѡƥ\n\
󤬤򤷤Ƥ\n\
Ƭ *DONE* ȤѡƥϡǤ˥֡Ȼ˥ޥȤ\n\
ǥХΥꥹȤ˴ޤޤƤޤ(̤ϥޥȥݥȤǤ)\n\
" 22 72 12 \
$DOSLISTOK \
"Quit" "λ" 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
      ########
      NEXT_PARTITION="`echo $REPLY|sed -e 's/\*DONE\*//' -e 's/,.*//'`"
      if echo $REPLY | grep "DONE" 1> /dev/null 2> /dev/null ; then
      # We have already added this partition.
        dialog --title "顼$NEXT_PARTITIONϤǤ˻ȤƤޤ" --msgbox "\n\
ޤ$NEXT_PARTITIONϤǤ˥֡Ȼ˥ޥȤ\n\
ǥХΥꥹȤ˴ޤޤƤޤޤꤷƤʤѡƥ\n\
򤹤뤫Quit򤷤Ƥ\n\
" 9 70
        continue;
      fi
      NEXT_FS="`echo $REPLY|sed -e 's/.*,//'`"
      dialog --title "ޥȥݥȤ" --inputbox "\
ơοѡƥ򡢥ǥ쥯ȥĥ꡼Τɤ\n\
˥ޥȤʤФʤޤ󡣥ޥȤǥ쥯ȥ̾\n\
ϤƤ 㤨С/win_c  /win_d ʤɤʤǤ⹽\n\
ޤ\n\
Υѡƥϡºݤˤϥ֡Ȥľޤǥޥ\n\
ޤ\n\
\n\
$NEXT_PARTITION($NEXT_FS)ɤ˥ޥȤޤ\n\
" 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$$
      if [ ! -z "`echo $NEXT_FS | grep FAT`" ] ; then
         echo "Windows ޤ DOSѡƥ /etc/fstab ɲäޤ" >> /tmp/tmpmsg$$
         echo >> /tmp/tmpmsg$$
         echo "$NEXT_PARTITION        $NEW_DIR        vfat        iocharset=euc-jp,codepage=932  0   0" >> /tmp/tmpmsg$$
         echo "$NEXT_PARTITION        $NEW_DIR        vfat        iocharset=euc-jp,codepage=932  0   0" >> /tmp/SeTDOS
      elif [ ! -z "`echo $NEXT_FS | grep NTFS`" ] ; then
         echo "NTFSѡƥ(ɤ߹) /etc/fstab ɲäޤ" >> /tmp/tmpmsg$$
         echo >> /tmp/tmpmsg$$
         echo "$NEXT_PARTITION        $NEW_DIR        ntfs         ro   0   0" >> /tmp/tmpmsg$$
         echo "$NEXT_PARTITION        $NEW_DIR        ntfs         ro   0   0" >> /tmp/SeTDOS
      elif [ ! -z "`echo $NEXT_FS | grep SFS`" ] ; then
         echo "SFSѡƥ(ɤ߹) /etc/fstab ɲäޤ" >> /tmp/tmpmsg$$
         echo >> /tmp/tmpmsg$$
         echo "$NEXT_PARTITION        $NEW_DIR        ntfs         ro   0   0" >> /tmp/tmpmsg$$
         echo "$NEXT_PARTITION        $NEW_DIR        ntfs         ro   0   0" >> /tmp/SeTDOS
      fi
      dialog --title "/etc/fstab ɵƤγǧ" \
             --msgbox "`cat /tmp/tmpmsg$$`" 9 75
      rm -f /tmp/tmpmsg$$
      ########
    fi
  done # mounting DOS/HPFS partitions
fi
