#!/bin/sh

# $MTPT に chroot してから実行される部分

expand_mes () {
    source /sbin/grubconfig.mes
}

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

ROOT_DEVICE=`cat /tmp/grub_ROOT_DEVICE`

expand_mes
/bin/dialog --title "grub install (1)" --infobox "$install_mes_1" 6 70

grub-install --target=i386-pc --no-floppy --grub-setup=/bin/true $ROOT_DEVICE
if [ $? -ne 0 ]; then
    /bin/dialog --title "grub install error" --msgbox "$mbr_error_mes1" 6 70
    exit 1
fi
sleep 3

grubcfg_no_overwrite=0
if [ -f /boot/grub/grub.cfg ]; then
    # overwrite grub.cfg
    /bin/dialog --title "overwrite grub.cfg ?" --yesno "$grub_cfg_mes1" 6 70
    if [ $? -eq 0 ]; then
	echo "making grub.cfg..."
	mv /boot/grub/grub.cfg /boot/grub/grub.cfg.org
	grub-mkconfig -o /boot/grub/grub.cfg
	if [ $? -ne 0 ]; then
	    /bin/dialog --title "grub.cfg install error" --msgbox "$mbr_error_mes2" 6 70
	    exit 1
	fi
	sleep 3
    else
	/bin/dialog --title "don't overwrite grub.cfg" --infobox "$grub_cfg_mes2" 6 70
	grubcfg_no_overwrite=1
    fi
else
    # create grub.cfg
    echo "making grub.cfg..."
    grub-mkconfig -o /boot/grub/grub.cfg
    if [ $? -ne 0 ]; then
	/bin/dialog --title "grub.cfg install error" --msgbox "$mbr_error_mes2" 6 70
	exit 1
    fi
    sleep 3
fi

    
if [ $grubcfg_no_overwrite -ne 1 ] ; then
    cat <<'EOF' > /tmp/grub.cfg.tmp
This is the generated grub.cfg.
-----------------------------------------------------------------
EOF
    cat /boot/grub/grub.cfg >> /tmp/grub.cfg.tmp
    cat <<'EOF' >> /tmp/grub.cfg.tmp
-----------------------------------------------------------------
EOF

    /bin/dialog --title "contents of grub.cfg" --textbox /tmp/grub.cfg.tmp  40 76

    # NG だった場合、grub.cfg を削除して exit するしかないのだけれど、
    # 何か救済策は必要だろうか？
    /bin/dialog --title "continue?" --yesno "$continue_mes"  6 60
    if [ $? != 0 ]; then
        rm -f /tmp/grub.cfg.tmp /boot/grub/grub.cfg
        if [ -f /boot/grub/grub.cfg.org ]; then
    	    mv /boot/grub/grub.cfg.org /boot/grub/grub.cfg
        fi
        /bin/dialog --title "User exit" --msgbox "$exit_mes" 6 70
        exit 1
    fi
fi

cat <<EOF > /tmp/tmpscript
#!/bin/sh
/bin/dialog --title "select HDD for grub" --menu \
"$tgt_mes" 20 74 4 \\
EOF

# これをしないとfdiskの出力が日本語になる
export LANG=C
$PROBE -l | grep -v ram | grep "^Disk " | grep -v "identifier" > /tmp/SeTdlist
export LANG=ja_JP.UTF-8
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`

expand_mes
dialog --title "install grub into MBR" --yesno \
"$mbr_mes" 10 70
if [ $? = 0 ]; then
    dialog --title "installing grub" --infobox \
	"$install_mes1" 10 70
    grub-install --target=i386-pc $grub_HDD
    sleep 3
    if [ $? = 0 ]; then
	dialog --title "install succeeded" --msgbox \
	    "$install_mes2" 10 70
    else
	dialog --title "install failed" --msgbox \
	    "$install_mes3" 10 70
	exit 1
    fi
else
    dialog --title "Quit grub install" --infobox \
	"$quit_mes" 10 70
    exit 1
fi

if [ -x /sbin/grub_partuuid.sh ]; then
    dialog --title "convert root device name?" --yesno \
	   "$uuid_mes1" 10 70
    if [ $? -eq 0 ]; then
	/sbin/grub_partuuid.sh
	if [ $? -eq 0 ]; then
	    dialog --title "grub.conf converted" --msgbox \
		   "$uuid_mes2" 10 70
	fi
    fi
fi
