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

# 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
}

# 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>Write floppy</label>
        <input file icon=\"forward\"></input>
	<action>mke2fs \$DEVICE</action>
	<action>mkdir /media/floppy ; mount \$DEVICE /media/floppy</action>
	<action>mkdir -p /media/floppy/boot/grub</action>
	<action>cp /usr/lib/grub/i386-pc/stage? /media/floppy/boot/grub</action>
	<action>[ -f /usr/share/boot/etherboot ] && cp /usr/share/boot/etherboot /media/floppy/boot</action>
	<action>[ -f /usr/share/boot/memtest.lzma ] && unlzma -c /usr/share/boot/memtest.lzma > /media/floppy/boot/memtest</action>
	<action>umount \$DEVICE</action>
	<action>echo -e \"root (\${DEVICE#/dev/})\\nsetup (\${DEVICE#/dev/})\\nquit\" | grub --batch </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

exit 0
