#!/bin/sh
# Add DOS partitions.
rm -f /tmp/SeTDOS
DOSLIST="`probe -l | cut -b-70 | fgrep "DOS" | fgrep -v "FDISK" 2> /dev/null`"
WINLIST="`probe -l | cut -b-70 | fgrep "Win" | fgrep -v "FDISK" 2> /dev/null`"
HPFSLIST="`probe -l | cut -b-70 | fgrep "HPFS" 2> /dev/null`" 

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

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

if [ ! "$DOSLIST" = "" ]; then # there are DOS or OS/2 partitions:
 cat << EOF > /tmp/tmpmsg

ʲDOS뤤OS/2 HPFSѡƥ󤬸Ĥޤ
(Windows NT ѤΥѡƥ OS/2 ѤɽޤNT Ѥ
ѡƥϥޥȤǤޤΤǤ餺)ޤDOSѡƥ
ȤƥޥȤΤǡɬפ˱ vfat ǥޥȤ
褦ѹƤ
   Device Boot   Begin    Start      End   Blocks   Id  System
$DOSLIST

ΥѡƥΤLinux鸫褦ˤѡƥ
Ϥޤ
EOF
 dialog --title "DOS, OS/2ѡƥ" --yesno "`cat /tmp/tmpmsg`" 22 78
 ADDDOS=$?
 rm -f /tmp/tmpmsg
 if [ "$ADDDOS" = "0" ]; then
  cat /dev/null > /tmp/SeTDOS
  while [ 0 ]; do
   USED_DOS_PARTITIONS="`cat /tmp/SeTDOS | fgrep msdos`"
   USED_HPFS_PARTITIONS="`cat /tmp/SeTDOS | fgrep hpfs`"
   if [ ! "$USED_HPFS_PARTITIONS" = "" ]; then
    USED_DOS_PARTITIONS="`echo -e "$USED_DOS_PARTITIONS\n$USED_HPFS_PARTITIONS"`"
   fi
   rm -f /tmp/tmpmsg
   if [ ! "$USED_DOS_PARTITIONS" = "" ]; then
    echo "DOS뤤OS/2ѡƥϤǤ˻ꤵƤޤ" > /tmp/tmpmsg
    echo "$USED_DOS_PARTITIONS" >> /tmp/tmpmsg
   fi
   echo "DOS뤤OS/2ѡƥϥޥȤλ꤬ǽǤ" >> /tmp/tmpmsg
   echo "$DOSLIST"  >> /tmp/tmpmsg
   echo "LinuxΥǽˤѡƥꤷƤ" >> /tmp/tmpmsg
   echo "Ϥλˤ[q]ϤƤ"  >> /tmp/tmpmsg
   dialog --title "ѡƥ" --inputbox "`cat /tmp/tmpmsg`" 22 75 2> /tmp/nextp
   if [ $? = 1 ]; then
    rm -f /tmp/tmpmsg /tmp/nextp /tmp/SeTDOS
    exit
   fi
   NEXT_PARTITION="`cat /tmp/nextp`"
   rm -f /tmp/nextp /tmp/tmpmsg
   if [ "$NEXT_PARTITION" = "q" ]; then
    break;
   fi
   if [ ! -b $NEXT_PARTITION ]; then
    dialog --title "ʥǥХ" --msgbox "ǥХ̾ǤϤľƤ㤨С \n\
/dev/hda3 ʤɤʤ" 8 70 
    continue;
   else
    if echo "$DOSLIST" | fgrep $NEXT_PARTITION 1> /dev/null 2> /dev/null ; then
     # Here we should test to be sure the partition has not been used
     if fgrep $NEXT_PARTITION /tmp/SeTDOS 1> /dev/null 2> /dev/null ; then
      # We have already added this partition.
      cat << EOF > /tmp/tmpmsg
ޤ$NEXT_PARTITIONϤǤ˥֡Ȼ˥ޥȤ
ǥХΥꥹȤ˴ޤޤƤޤޤꤷƤʤѡƥ
򤹤뤫[q]Ϥ³Ƥ
EOF
      dialog --title "顼$NEXT_PARTITIONϤǤ˻ȤƤޤ" --msgbox "`cat /tmp/tmpmsg`" 8 70
      rm -f /tmp/tmpmsg
      continue;
     fi
     cat << EOF > /tmp/tmpmsg
ơοѡƥ򡢥ǥ쥯ȥĥ꡼
ɤ˥ޥȤʤФʤޤ󡣥ޥȤ
ǥ쥯ȥ̾ϤƤ㤨С/dosc/dosd
ʤɤʤǤ⹽ޤ
Υѡƥϡºݤˤϥ֡Ȥľޤ
ޥȤޤ

$NEXT_PARTITIONɤ˥ޥȤޤ
EOF
     dialog --title "ޥȥݥȤ" --inputbox "`cat /tmp/tmpmsg`" 14 65 2> /tmp/newdir
     if [ $? = 1 ]; then
      rm -f /tmp/tmpmsg /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 probe -l | fgrep "DOS" | fgrep $NEXT_PARTITION 1> /dev/null 2> /dev/null ; then
      echo "DOSѡƥ/etc/fstabɲäޤ" >> /tmp/tmpmsg
      echo >> /tmp/tmpmsg
      echo "$NEXT_PARTITION        $NEW_DIR        msdos        defaults   1   1" >> /tmp/tmpmsg
      echo "$NEXT_PARTITION        $NEW_DIR        msdos        defaults   1   1" >> /tmp/SeTDOS
     elif probe -l | fgrep "Win" | fgrep $NEXT_PARTITION 1> /dev/null 2> /dev/null ; then
      echo "Windowѡƥ/etc/fstabɲäޤ" >> /tmp/tmpmsg
      echo >> /tmp/tmpmsg
      echo "$NEXT_PARTITION        $NEW_DIR        vfat        defaults   1   1" >> /tmp/tmpmsg
      echo "$NEXT_PARTITION        $NEW_DIR        vfat        defaults   1   1" >> /tmp/SeTDOS
     else
      echo "HPFSѡƥɤ߹ѡˤ/etc/fstabɲäޤ" >> /tmp/tmpmsg
      echo >> /tmp/tmpmsg
      echo "$NEXT_PARTITION        $NEW_DIR        hpfs         ro   1   1" >> /tmp/tmpmsg
      echo "$NEXT_PARTITION        $NEW_DIR        hpfs         ro   1   1" >> /tmp/SeTDOS
     fi
     dialog --title "DOS/HPFSѡƥθߤξ" \
--msgbox "`cat /tmp/tmpmsg`" 9 75
    else # device not tagged as DOS
     dialog --title "ե륷ƥΥפ" --msgbox "
򤵤줿ѡƥΥפ \n\
DOSOS/2ȤϰۤʤޤٻꤷƤ" 7 52
     continue;
    fi
   fi
  done # mounting DOS/HPFS partitions
 fi
fi
