#!/bin/sh
#
# Tiny GTKdialog interface to SliTaz Live USB tool aka TazUSB.
#
# (c) 2010 SliTaz GNU/Linux - GNU gpl v3
#

# TazUSBbox is only for root.
if test $(id -u) != 0 ; then
	exec subox tazusbbox
	exit 0
fi

# Include gettext helper script.
. /usr/bin/gettext.sh

# Export package name for gettext.
TEXTDOMAIN='tazusbbox'
export TEXTDOMAIN

# $BIN for functions.
export BIN=$0

# Functions

gen_live()
{
	[ -z "$DEVICE" ] && exit 0 
	if [ -n "$ISO_IMAGE" ]; then
		xterm -T "Tazusb gen-iso2usb" \
			-geometry 80x16 \
			-e "tazusb gen-iso2usb $ISO_IMAGE $DEVICE; exit 0"
	else
		xterm -T "Tazusb gen-liveusb" \
			-geometry 80x16 \
			-e "tazusb gen-liveusb $DEVICE; exit 0"
	fi
}

box()
{
	MAIN_DIALOG='
<window title="'`gettext "TazUSB Box"`'" icon-name="media-flash">
<vbox>
	
	<text width-chars="60">
		<label>"'`gettext "
Generate SliTaz LiveUSB media and boot in RAM! Insert a LiveCD into
the cdrom drive, select the correct device and press Generate.
"`'"</label>
	</text>

	<frame '`gettext "ISO to USB (Optional default is CDROM)"`'>
		<hbox>
			<text use-markup="true">
				<label>"'`gettext "<b>ISO image:</b>"`'"</label>
			</text>
			<entry accept="filename">
				<variable>ISO_IMAGE</variable>
			</entry>
			<button>
				<input file icon="tazlito"></input>
				<action type="fileselect">ISO_IMAGE</action>
			</button>
		</hbox>
	</frame>
	
	<hbox>
		<text use-markup="true">
			<label>"'`gettext "<b>USB Media:</b>"`'"</label>
		</text>
		<combobox>
			<variable>DEVICE</variable>'
			if [ -d /proc/scsi/usb-storage ]; then
				MAIN_DIALOG=${MAIN_DIALOG}"
				<item></item>"
				for i in `blkid | cut -d ":" -f 1`; do
					MAIN_DIALOG=${MAIN_DIALOG}"
					<item>$i</item>"
				done
			else
				MAIN_DIALOG=${MAIN_DIALOG}'
				<item>'`gettext "Not found"`'</item>'
			fi
			export MAIN_DIALOG=${MAIN_DIALOG}'
		</combobox>
	</hbox>
	<hbox>
		<button>
			<label>'`gettext "Generate"`'</label>
			<input file icon="forward"></input>	
			<action>$BIN gen_live</action>
			<action>clear:ISO_IMAGE</action>
		</button>
		<button>
			<label>'`gettext "Exit"`'</label>
			<input file icon="exit"></input>
			<action type="exit">exit</action>
		</button>
	</hbox>
	
</vbox>
</window>'
	gtkdialog --center --program=MAIN_DIALOG #>/dev/null
}

if [ -n "$1" ]; then
	$1
else
	box
fi

exit 0
