#!/bin/sh

# $MTPT  chroot Ƥ¹Ԥʬ

PROBE=/sbin/fdisk
crunch () { # remove extra whitespace
    read STRING;
    echo $STRING
}

ROOT_DEVICE=`cat /tmp/grub_ROOT_DEVICE`

/bin/dialog --title "grubΥ󥹥ȡ(1)" --infobox \
    "grub$ROOT_DEVICE/boot/grub˥󥹥ȡ뤷Ƥޤ" 6 70

grub-install --no-floppy --grub-setup=/bin/true $ROOT_DEVICE
sleep 3
grub-mkconfig -o /boot/grub/grub.cfg
sleep 3

if [ ! -f /boot/grub/grub.cfg ]; then
    /bin/dialog --title "grub.cfg build error" --msgbox \
	"/boot/grub/grub.cfg˼Ԥޤ \
꤬褦ʤΤǽǤޤ" 6 70
    exit 1
fi
    
cat <<'EOF' > /tmp/grub.cfg.tmp
줿 grub.cfg ϰʲ̤Ǥ 

-----------------------------------------------------------------
EOF
cat $MTPT/boot/grub/grub.cfg >> /tmp/grub.cfg.tmp
cat <<'EOF' >> /tmp/grub.cfg.tmp
-----------------------------------------------------------------
Ƥ˲꤬ꤽʤС󥹥ȡߤƤ
EOF

/bin/dialog --title "grub.cfg" --textbox /tmp/grub.cfg.tmp  38 74

/bin/dialog --title "continue?" --yesno \
    "Ƥgrub򥤥󥹥ȡ뤷ޤ"  6 60
if [ $? != 0 ]; then
    rm -f /tmp/grub.cfg.tmp
    /bin/dialog --title "User exit" --msgbox \
	"Ǥޤ" 6 70
    exit 1
fi

/bin/dialog --title "grubΥ󥹥ȡ(1)" --menu \
"grubHDDƬʬ(MBR)ƥ򥤥󥹥ȡ뤷 \
ѡƥƬʬ˥󥹥ȡ뤹뤳ȤǤޤ \
MBR˥󥹥ȡ뤹硤¸ߤƤ֡ȥ \
񤭤Τǡ¸Ķ˥֡ȤǤʤʤǽ \
ޤѡƥƬ˥󥹥ȡ뤹 \
MBR餽Υѡƥư褦ɲä \
ɬפޤ" 15 74 4 \
"M"  "HDDƬ(MBR)" \
"P"  "ѡƥƬ" \
2> /tmp/return

if [ $? = 1 -o $? = 255 ]; then
    exit 255 # user abort
fi

grub_target=`cat /tmp/return`

if [ "$grub_target" = "M" ]; then  # install to MBR
  cat <<EOF > /tmp/tmpscript
/bin/dialog --title "grubΥ󥹥ȡ(2)" --menu \
"grubHDDƬ(MBR)˥󥹥ȡ뤷ޤ \
grub.cfgǤƤWindowsĶdual boot \
ȤǽǤǰΤͽΥ֡ȼʤѰդƤƤ \n\
BIOS֡ȥǥХǤ硤\
(Windows֡Ȥ뤿κǽHDDMBRѹ)2ܰʹߤHDD \
MBR˥󥹥ȡ뤷ƤơLinuxưݤˤϤHDD򤹤롤 \
ȤȤǽǤ \n\
grub򥤥󥹥ȡ뤹HDD򤷤Ƥ" 20 74 4 \\
EOF

  # 򤷤ʤfdiskνϤܸˤʤ
  export LANG=C
  $PROBE -l | grep "^Disk " | grep -v "identifier" > /tmp/SeTdlist
  cat /tmp/SeTdlist | while [ 0 ]; do
      read DISK;
      if [ "$DISK" = "" ]; then
  	break;
      fi
      NAME=`echo $DISK | crunch | cut -f2 -d' ' | sed "s/://"`
      SIZE=`echo $DISK | crunch | cut -f3 -d' ' `
      UNIT=`echo $DISK | crunch | cut -f4 -d' ' | sed "s/,//"`
      echo "\"$NAME\" \"$SIZE $UNIT\" \\" >> /tmp/tmpscript
  done
  echo "2> /tmp/return" >> /tmp/tmpscript

  . /tmp/tmpscript
  if [ $? = 1 -o $? = 255 ]; then
      rm /tmp/tmpscript
      exit 255 # user abort
  fi
  grub_HDD=`cat /tmp/return`

  dialog --title "grubMBR˥󥹥ȡ" --yesno \
  "$grub_HDD򤵤ޤΥǥƬʬ(MBR)grub \
󥹥ȡ뤷פǤ" 10 70
  if [ $? = 0 ]; then
      dialog --title "grubΥ󥹥ȡ" --infobox \
  	"grub$grub_HDDMBR˥󥹥ȡ뤷Ƥޤ" 10 70
      grub-install $grub_HDD
      sleep 3
      if [ $? = 0 ]; then
  	dialog --title "󥹥ȡ봰λ" --msgbox \
  	    "grub$grub_HDDMBR˥󥹥ȡ뤷ޤ" 10 70
  	exit 0
      else
  	dialog --title "󥹥ȡ뼺" --msgbox \
  	    "grub$grub_HDDMBR˥󥹥ȡǤޤǤ" 10 70
  	exit 1
      fi
  else
      dialog --title "grubΥ󥹥ȡ" --infobox \
  	"grubMBR˥󥹥ȡ뤻˽λޤ \
¾Υ֡ȥ󥹥ȡ뤵ƤʤȡHDD鵯ưǤʤ \
ʤޤΤǡrescueCDŬƤ" 10 70
      exit 1
  fi
else  # install to partition
  CURRENT_ROOT_DEVICE=`grep " / " /etc/fstab | cut -f1 -d' '`
  /bin/dialog --title "grubѡƥƬإ󥹥ȡ" --msgbox \
"grub򥷥ƥ򥤥󥹥ȡ뤷ѡƥ($CURRENT_ROOT_DEVICE) \
Ƭʬ˥󥹥ȡ뤷ޤ \
Υ֡ȥˤѹäʤΤǡPlamo Linuxư \
ˤϤΥѡƥư뤿ưɲä \
ɬפޤ" 10 70
      dialog --title "grubΥ󥹥ȡ" --infobox \
  	"grub$CURRENT_ROOT_DEVICEƬ˥󥹥ȡ뤷Ƥޤ" 10 70
      grub-install --force $CURRENT_ROOT_DEVICE
      sleep 3
      if [ $? = 0 ]; then
  	dialog --title "󥹥ȡ봰λ" --msgbox \
  	    "grub$CURRENT_ROOT_DEVICE˥󥹥ȡ뤷ޤ" 10 70
  	exit 0
      else
  	dialog --title "󥹥ȡ뼺" --msgbox \
  	    "餫Υ顼grub$CURRENT_ROOT_DEVICE˥󥹥ȡǤޤǤ" 10 70
  	exit 1
      fi

fi

