#!/bin/sh

copy_bootparms() {
    if [ -f /mnt/boot/boot.ini ]; then
	if mount | grep /fd 1> /dev/null 2> /dev/null ; then
	    cp /mnt/boot/boot.ini /mnt/boot/*.pbr /fd
	fi
    fi
}

dialog --title "쥹塼ǥ" --menu \
    "Ǥ FD 饷ƥư뤿Υ쥹塼ǥ \n\
ޤ쥹塼ǥκϡޥܡɤľ \n\
³뽾 FD ɥ饤(/dev/fd0) USB ³ FD ɥ \n\
(/dev/sda)Ǥ¿ۤʤޤ \n\
Ȥ FD ϤɤΥפǤ" 20 72 6 \
"FD" "跿 FD ɥ饤"  \
"USB" "USB פ FD ɥ饤" \
"EXIT" "쥹塼ǥκλ" 2> /tmp/return
if [ ! $? = 0 ]; then
    break;
fi
FDTYPE=`cat /tmp/return`
rm -f /tmp/return

if [ "$FDTYPE" =  "EXIT" ]; then
    break
elif [ "$FDTYPE" = "FD" ]; then  
    while [ 0 ]; do # the bootdisk menu loop
	dialog --title "FDD γǧ" --menu \
	    "쥹塼ǥFDD ˤ FD ǧ \n\
쥹塼ǥ FD ϴ˾񤭤졤 \n\
FD Υǡõޤ\n\
쥹塼ǥϻѤ֡ȥ˱ \n\
grub Ǥ syslinux Ǥ 2 郎ޤ \n\
֡ȥǥˤFDեޥåȤƤʤ硤\n\
ޤեޥåȤƤɬפޤ" 22 78 6 \
	    "format" "/dev/fd0(a:)ǥեåԡեޥåȤ" \
	    "grub" "grubΥ֡ȥǥ" \
	    "syslinux" "syslinuxΥ֡ȥǥ" \
	    "continue" "֡ȥǥλ" 2> /tmp/return
	if [ ! $? = 0 ]; then
	    break;
	fi
	REPLY=`cat /tmp/return`
	rm -f /tmp/return
	if [ "$REPLY" = "format" ]; then
	    dialog --title "եåԡγǧ" --msgbox \
		"/dev/fd0 Υǥ򥤥󥹥ȡεưǥؤ \n\
뤳ȤǧƤ" 6 72
	    if [ $? = 0 ]; then
		dialog --title "/dev/fd0Υեޥå" --infobox "/dev/fd01.44ᥬ\
ХȤǥեޥåȤƤޤ" 3 70 
		fdformat /dev/fd0u1440
		dialog --title "formatting.." --infobox "/dev/fd0 ˥ե륷ƥ" 3 70 
		mkdosfs /dev/fd0u1440
	    fi
	    rm -f /tmp/return
	elif [ "$REPLY" = "grub" ]; then # make grub bootdisk
	    dialog --title "grub ֡ȥǥ" --yesno \
		"\n\
֡ȲǽFDɥ饤֤˥եޥåȺѤߤΥեåԡ \n\
ƤΥǥ Plamo Linux Υ֡ȥǥ \n\
ˤޤϡɥǥ֡ȤǤ褦ˤʤ \n\
ޤǤϡΥեåԡѤ \n\n\
ΥեåԡǥƤϴ˾񤭤ޤ\n\n\
YESǥǥNOߤޤ\n" 14 74
	    if [ "$?" = "0" ]; then
		dialog --title "ǥ" --infobox "'vmlinuz'򥳥ԡƤޤ..." 5 70
		if [ ! -d /fd ]; then
		    mkdir /fd
		fi
		mount /dev/fd0 /fd -t vfat
		if [ $? = 0 ]; then
		    cp $T_PX/vmlinuz /fd
		    cp $T_PX/boot/grub/stage1 /fd
		    cp $T_PX/boot/grub/stage2 /fd

		    cat <<EOF > /fd/grub.cfg
# GRUB configuration file
# generated by 'grubconfig'
#
# Start GRUB global section
default 0
timeout 30
fallback 1

title Plamo Linux
kernel (fd0)/vmlinuz root=$ROOT_DEVICE ro
EOF
		    dialog --infobox "\nGRUB򥤥󥹥ȡ..." 5 40
		    $T_PX/usr/sbin/grub --batch <<EOF
root (fd0)
install (fd0)/stage1 d (fd0) (fd0)/stage2 0x8000 p (fd0)/grub.cfg
quit
EOF
		    copy_bootparms
		    sync
		    sync
		    umount /fd
		else
		    dialog --title "FDޥԲ" --msgbox "FD ˥ޥȤǤʤ \n\
FD  grub 򥤥󥹥ȡǤޤǤ" 5 70
		fi
	    fi
	elif [ "$REPLY" = "syslinux" ]; then # make syslinux bootdisk
	    dialog --title "/dev/fd0syslinuxΥ֡ȥǥ" --yesno \
		"֡ȲǽFDɥ饤֤˥եޥåȺѤߤΥեåԡƤ\n\
줬 Plamo Linuxƥư֡ȥǥȤʤޤ\n\
եåԡƤϴ˾񤭤ޤYESǥǥ \n\
NOߤޤ" 10 74
	    if [ $? = 0 ]; then # make the disk
		DEV=/dev/fd0
		dialog --infobox "$ROOT_DEVICEΤsyslinux֡ȥǥ..." 3 70
		$T_PX/sbin/syslinux -s  $DEV 1> /dev/null 2> /dev/null
		mount -t vfat /dev/fd0 /fd 1> /dev/null 2> /dev/null
		if [ $? = 0 ]; then
		    cp $T_PX/vmlinuz /fd/vmlinuz
		    cat << EOF > /fd/syslinux.cfg
DEFAULT vmlinuz root=$ROOT_DEVICE ro
DISPLAY plamo.msg
EOF
		    cat << EOF > /fd/plamo.msg
plamo2.lss
Welcome to Plamo Linux !!
We hope you'll enjoy the simple linux environment via Plamo Linux :-)

EOF

      # cp -a /usr/lib/setup/plamo2.lss /fd/plamo2.lss

		    copy_bootparms
		    sync
		    sync
		    umount /fd
  	        else
		    dialog --title "FDޥԲ" --msgbox "FD ˥ޥȤǤʤ \n\
FD  grub 򥤥󥹥ȡǤޤǤ" 5 70
	        fi
	    fi
       elif [ "$REPLY" = "continue" ] ; then
	    break
       fi
    done
else   # FDTYPE=USB
    dialog --title "USB FDD " --yesno \
	"ȤΥޥ USB FDD ʳ USB ǥХ(դUSB HDD \n\
 USB )³Ƥ硤USB FDD ȸ򤵤Ƥ \n\
õǽޤȤΥޥ USB FDD ʳ USB \n\
ǥХ(USB CD-ROM ³ƤƤ⹽ޤ)³ \n\
硤ʲκȤϼ»ܤǤޤΤ NO ǽλƤ\n\
Τޤ޺Ȥʤƹޤ󤫡 \n" 12 72
    if [ $? != 0 ]; then
	exit
    fi
    dialog --title "FDD γǧ" --msgbox \
	"쥹塼ǥFDD ˤ FD ǧƤ \n\
쥹塼ǥ FD ϴ˾񤭤졤FD Υǡõ \n\
ޤޤUSB FDD ʪեޥåȤ뤳ȤǤʤᡤ \n\
쥹塼ǥ ʪեޥåȤƤƤ" 10 74

    dialog --title "USB ѤΥ֡ȥǥ" --yesno \
        "USB  FDɥ饤֤˥եޥåȺѤߤΥեåԡƤ\n\
줬 Plamo Linuxƥư֡ȥǥȤʤޤ\n\
եåԡƤϴ˾񤭤ޤYESǥǥ \n\
NOߤޤ" 9 72 
    if [ $? = 0 ]; then # make FD
	DEV=`fdisk -l | grep "1 MB" | cut -d" " -f2 | tr -d ":"`
	if [ -f /var/adm/mount/boot/syslinux.img ]; then
	    FDIMG=/var/adm/mount/boot/syslinux.img
	elif [ -f /var/adm/mount/plamo/boot/syslinux.img ]; then
	    FDIMG=/var/adm/mount/plamo/boot/syslinux.img
	else
	    dialog --msgbox "USB֡ȥǥѤΥ᡼դޤ. \n\
ưUSB֡ȥǥϡplamo/boot ǥ쥯ȥˤ \n\
mkbootdsk.sh ޥɤ¹ԤƤ" 10 72
	    exit
	fi
	dialog --infobox "$ROOT_DEVICEΤ USB ֡ȥǥ..." 3 70
	cat $FDIMG > $DEV
	$T_PX/sbin/syslinux  $DEV 1> /dev/null 2> /dev/null
	mount -t vfat $DEV /fd 1> /dev/null 2> /dev/null
	if [ $? = 0 ]; then
	    cat << EOF > /fd/syslinux.cfg
DEFAULT vmlinuz root=$ROOT_DEVICE ro
DISPLAY plamo.msg
EOF
	    cat << EOF > /fd/plamo.msg
Welcome to Plamo Linux !!
We hope you'll enjoy the simple linux environment via Plamo Linux :-)

EOF
	    copy_bootparms
	    sync
	    sync
	    umount /fd
	else
	    dialog --title "FDޥԲ" --msgbox "FD ˥ޥȤǤʤ \n\
FD  grub 򥤥󥹥ȡǤޤǤ" 5 70
	fi
   fi  # end of make FD
fi  # FDTYPE=USB end
