#! /bin/sh
# 
# Gtkdialog box for the mount command. Part of SliTaz tools.
#
VERSION=20080618

# Check if user is root.
check_root()
{
	if test $(id -u) != 0 ; then
		echo -e "
You must be root to run `basename $0`. Please type 'su' and 
root password to become super-user.\n"
		exit 0
	fi
}

# This function is used after each screen to contine or abort install.
check_retval()
{
	case $retval in
		1)
			rm -f /tmp/floppybox.grub.menu
			echo -e "\nVoluntary exit.\n" && exit 0 ;;
		255)
			rm -f /tmp/floppybox.grub.menu
			echo -e "ESC pressed.\n" && exit 0 ;;
	esac
}

select_floppy()
{
	exec 3>&1
	DEVICE=`$DIALOG --title " Floppy device " \
    		--backtitle "Boot Floppy Creation" --clear \
		--extra-button --extra-label "Format" \
		--colors --inputbox "
Enter floppy device (default /dev/fd0)
		" 18 70 2>&1 1>&3`
	retval=$?
	exec 3>&-
	check_retval
	case "$DEVICE" in
	/dev/fd*);;
	*) DEVICE=/dev/fd0;;
	esac
	if [ "$retval" = "3" ]; then
		fdformat -n $DEVICE
    	fi
}

if [ "$1" == "call" ]; then
	case "$2" in
	setup-grub)
		DEVICE=$3
		mke2fs $DEVICE
		mkdir /media/floppy
		mount $DEVICE /media/floppy
		mkdir -p /media/floppy/boot/grub
		cp /usr/lib/grub/i386-pc/stage? /media/floppy/boot/grub
		cp /tmp/floppybox.grub.menu /media/floppy/boot/grub/menu.lst
		[ -f /usr/share/boot/btmgr -a -f /usr/share/boot/memdisk.lzma ]\
			&& cp /usr/share/boot/btmgr /media/floppy/boot \
			&& unlzma -c /usr/share/boot/memdisk.lzma > \
				/media/floppy/boot/memdisk
		[ -f /usr/share/boot/etherboot ]\
			&& cp /usr/share/boot/etherboot /media/floppy/boot
		[ -f /usr/share/boot/memtest.lzma ]\
			&& unlzma -c /usr/share/boot/memtest.lzma > \
				/media/floppy/boot/memtest
		[ -f /usr/share/boot/grub.exe.lzma ]\
			&& unlzma -c /usr/share/boot/grub.exe.lzma > \
				/media/floppy/boot/grub.exe
		grub-set-default --root-directory=/media/floppy 0
		umount $DEVICE
		grub --batch <<EOT
root (${DEVICE#/dev/})
setup (${DEVICE#/dev/})
quit
EOT
		;;
	*)	echo "Invalid command $0 $@" 1>&2
		exit 1;;
	esac
	exit 0
fi

cat > /tmp/floppybox.grub.menu <<EOT
default saved
timeout 10

title Windows (example on /dev/hda1)
	rootnoverify (hd0,0)
	chainloader +1
	save default

title Slitaz Live (example on /dev/hda1)
	root (hd0,0)
	kernel /boot/bzImage rw root=/dev/null vga=normal
	initrd /boot/rootfs.gz
	save default

title Slitaz Installed (example on /dev/hda2)
	root (hd0,1)
	kernel /boot/bzImage ro root=/dev/hda2 vga=normal
	save default

EOT
[ -f /usr/share/boot/btmgr -a -f /usr/share/boot/memdisk.lzma ]\
	&& cat >> /tmp/floppybox.grub.menu <<EOT
title Smart Boot Manager
	kernel /boot/memtest
	initrd /boot/btmgr

EOT
[ -f /usr/share/boot/etherboot ] && cat >> /tmp/floppybox.grub.menu <<EOT
title Etherboot
	kernel /boot/etherboot

EOT
[ -f /usr/share/boot/memtest.lzma ] && cat >> /tmp/floppybox.grub.menu <<EOT
title Memtest86+
	kernel /boot/memtest

EOT
[ -f /usr/share/boot/grub.exe.lzma ] && cat >> /tmp/floppybox.grub.menu <<EOT
title Grub4Dos
	kernel /boot/grub/grub.exe --config-file="configfile (fd0)/boot/grub/menu4dos.lst"

EOT

if [ -z "$XAUTHORITY" ]; then

    : ${DIALOG=dialog}
    
    DEVICE=/dev/fd0
    while true; do
	exec 3>&1
	ID_SOURCE=`$DIALOG --title " Choose a boot floppy " \
		--backtitle "Boot Floppy Creation on $DEVICE" --clear \
		--extra-button --extra-label "Change floppy" \
		--yes-label "Install" \
		--no-label "Quit" \
		--colors --radiolist "
Create a floppy to boot a LiveCD, in a PXE network...
Need a floppy disk in drive. Erase the whole floppy disk.
		" 18 70 50\
		SmartBtmgr "	Boot any partition or ATAPI CD-ROM." on \
		Etherboot "	Replacement for proprietary PXE ROMs." off \
		Memtest86+ "	Memory failures detection tool." off \
		Grub	"	Boot loader with command shell." off  2>&1 1>&3`
	retval=$?
	exec 3>&-
	check_retval
	if [ "$retval" = "3" ]; then
		select_floppy
		continue;
	fi
	while read key file pkg; do
		[ "$key" = "$ID_SOURCE" ] || continue
		if [ ! -f "$file" ]; then
			$DIALOG --title " Install package " --colors \
				--backtitle "Boot Floppy Creation" --clear \
				--yes-label "Install" \
				--no-label "Quit" \
				--yesno "The package $pkg is not yet installed. Install it ?" 18 70
			retval=$?
			tazpkg get-install $pkg
		fi
		if [ "$pkg" = "grub" ]; then
			bootfloppybox call setup-grub $DEVICE
		else
    			case "$file" in
    			*.lzma) action="unlzma -c";;
    			*.gz)   action="zcat";;
    			*)      action="cat";;
    			esac
			$action $file > $DEVICE
		fi
		exit 0
	done <<EOT
SmartBtmgr /usr/share/boot/btmgr btmgr
Etherboot /usr/share/boot/etherboot etherboot
Memtest86+ /usr/share/boot/memtest.lzma memtest
Grub /usr/sbin/grub grub
EOT
    done
fi
# Write bootfloppy image to floppy device.
#
BOOT_DIALOG='
<window title="bootfloppybox" icon-name="gtk-floppy">
  <vbox>
 
    <text use-markup="true">
      <label>
"
<b>SliTaz - Bootfloppybox</b>"
      </label>
    </text>
    <text wrap="false" width-chars="44" use-markup="true">
      <label>
"
Create a floppy to boot a LiveCD, in a PXE network...
Need a floppy disk in drive. Erase the whole floppy disk.
"
      </label>
    </text>
  
    <frame Floppy disk drive>
      <hbox>
        <text use-markup="true">
          <label>"<b>Device : </b>"</label>
        </text>
        <entry>
          <default>/dev/fd0</default>
          <variable>DEVICE</variable>
        </entry>
        <button>
          <label>Format floppy</label>
          <input file icon="forward"></input>
	  <action>fdformat -n $DEVICE</action>
        </button>
      </hbox>
    </frame>
    <notebook labels="LiveCD|PXE Network|Memory Test|Expert">
'  
while read name file pkg desc; do
    tmp="<frame $name>
      <hbox>
    <text wrap=\"true\" width-chars=\"44\" use-markup=\"true\">
      <label>
\"
$(echo -e $desc)
\"
      </label>
    </text>
      </hbox>
"
  if [ -f $file ]; then
    case "$file" in
    *.lzma) action="unlzma -c";;
    *.gz)   action="zcat";;
    *)      action="cat";;
    esac
    if [ "$pkg" = "grub" ]; then
      tmp="$tmp
      <hbox>
      <button>
        <label>Grub menu</label>
        <input file icon=\"accessories-text-editor\"></input>
        <action type=\"lauch\">leafpad /tmp/floppybox.grub.menu</action>
      </button>
      <button>
        <label>Write floppy</label>
        <input file icon=\"forward\"></input>
	<action>bootfloppybox call setup-grub \$DEVICE</action>
      </button>
      </hbox>
    </frame>
"
    else
      tmp="$tmp
      <hbox>
      <button>
        <label>Write floppy</label>
        <input file icon=\"forward\"></input>
	<action>$action $file > \$DEVICE</action>
      </button>
      </hbox>
    </frame>
"
    fi
  else
    tmp="$tmp
      <hbox>
      <text wrap=\"true\" width-chars=\"34\" use-markup=\"true\">
        <label>
\"<i>The package <b>$pkg</b> is not yet installed</i>\"
        </label>
      </text>
      <button>
        <input file icon=\"go-jump\"></input>
	<label>Install</label>
        <action>[ -f /var/lib/tazpkg/packages.list ] || tazpkg recharge</action>
	<action>xterm -fa MiscFixed -fs 11 -bg gray93 -fg black -geometry 80x17 -title \"$pkg install\" -e \"tazpkg get-install $pkg ; echo -e \\\"----\\n\\nENTER to continue...\\\" && read close\"</action>
        <action type=\"exit\">Exit</action>
      </button>
      </hbox>
    </frame>
"
  fi
  BOOT_DIALOG="$BOOT_DIALOG$tmp"
done <<EOT
SmartBtmgr /usr/share/boot/btmgr btmgr This OS independent Smart Boot Manager can boot any          partition or ATAPI CD-ROM.
Etherboot /usr/share/boot/etherboot etherboot This network bootloader provides a replacement for proprietary PXE or NBI ROMs.
Memtest86+ /usr/share/boot/memtest.lzma memtest Memory failures detection tool.
Grub /usr/sbin/grub grub General purpose boot loader with command shell
EOT
tmp='
    </notebook>
    <hbox>
      <button>
        <input file icon="exit"></input>
	<label>Exit</label>
        <action type="exit">Exit</action>
      </button>
    </hbox>
  
  </vbox>
</window>
'
BOOT_DIALOG="$BOOT_DIALOG$tmp"
export BOOT_DIALOG

# Only root can create floppy.
check_root
gtkdialog --program=BOOT_DIALOG
rm -f /tmp/floppybox.grub.menu

exit 0
