#!/bin/sh
# 
# Gtkdialog box for Tazlito - SliTaz Live Tool.
# Tabs are used for indent and all actions should go into functions to
# separate the GUI and commands.
#
# (C) GNU gpl v3 - SliTaz GNU/Linux 2011.
#
VERSION=4.0

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

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

export BIN=$0

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

# We need to parse flavors.list for GTK tree.
list_flavors()
{
	tazlito list-flavors | grep ^[a-z] | while read line
	do		
		info=`echo -n $line | awk '{print $1 " " $2 " " $3}'`
		printinfo=`echo -n $line | awk '{print $1 "|" $2 "|" $3}'`
		echo "$printinfo|`echo $line | sed s/\"$info\"//`"
	done
}

recharge_list()
{
	xterm -geometry 90x14 \
		-title "list-flavors --recharge" \
		-e "tazlito list-flavors --recharge | grep ^[a-z] && sleep 2"
}

# When using gen-liveflavor, gen-flavor or gen-distro, a user can put some
# addfiles copied into the rootfs.
addfiles_action()
{
	cd /home/slitaz
	mkdir -p distro/addfiles/rootfs/usr/share/images
	mkdir -p distro/addfiles/rootcd
	file-manager distro/addfiles &
}

writeiso_action()
{
	xterm -geometry 80x16 \
		-title "writeiso" \
		-e "tazlito writeiso $COMPRESSION"
}

gen_liveflavor_action()
{
	cd /home/slitaz
	sed -i "s/ISO_NAME=.*/ISO_NAME=\"slitaz-$GEN_LIVEFLAVOR_NAME\"/" \
		/etc/tazlito/tazlito.conf
	xterm -geometry 80x16 \
		-title "gen-liveflavor" \
		-e "tazlito gen-liveflavor $GEN_LIVEFLAVOR_NAME && echo -e \"----\nENTER to continue...\" && read close"
}

clean_distro_action()
{
	cd /home/slitaz
	xterm -geometry 80x16 \
		-title "clean-distro" \
		-e "tazlito clean-distro && sleep 1"
}

show_flavors()
{
	tazlito list-flavors | \
	awk '{if (show && $1 != "") print $1} /^==/ {show=1}'
}

get_flavor_action()
{
	cd /home/slitaz
	xterm -geometry 80x16 \
		-title "get-flavor $COMMUNITY_FLAVOR" \
		-e "tazlito get-flavor $COMMUNITY_FLAVOR && echo -e \"----\nENTER to continue...\" && read close"
}

gen_distro_action()
{
	cd /home/slitaz
	xterm -geometry 80x16 \
		-title "gen-distro" \
		-e "tazlito gen-distro && echo -e \"----\nENTER to continue...\" && read close"
}

gen_flavor_action()
{
	cd /home/slitaz
	xterm -geometry 80x16 \
		-title "gen-flavor $GEN_FLAVOR_NAME" \
		-e "tazlito gen-flavor $GEN_FLAVOR_NAME && echo -e \"----\nENTER to continue...\" && read close"
}

edit_distro_script()
{
	[ ! -f $DISTRO_SCRIPT ] && echo "#!/bin/sh" > $DISTRO_SCRIPT
	chmod +x $DISTRO_SCRIPT
	editor $DISTRO_SCRIPT
}

loram_action()
{
	case "$LORAM_RAM$LORAM_CDROM$LORAM_CDROM_SMALL" in
	falsefalsetrue)	type=smallcdrom ;;
	falsetruefalse)	type=cdrom ;;
	*)		type=ram ;;
	esac
	xterm -geometry 80x16 \
		-title "build loram iso image $LORAM_OUT" \
		-e "tazlito build-loram $LORAM_IN $LORAM_OUT $type ; echo -e \"----\nENTER to continue...\" && read close"
}

merge_input()
{
	set -- $(cat /tmp/tazlito.mergelist 2> /dev/null)
	while [ -n "$2" ]; do
		echo "$2 | $1"
		shift 2
	done
}

merge_add()
{
	 echo -n " $ISO_IN $ISO_RAM" >> /tmp/tazlito.mergelist
}

merge_del()
{
	set -- $(cat /tmp/tazlito.mergelist)
	ARGS=""
	while [ -n "$2" ]; do
		case "$1" in
		$ISO_ITEM) ;;
		*)	[ -n "$ARGS" ] && ARGS="$ARGS "
			ARGS="$ARGS$1 $2" ;;
		esac
		shift 2
	done
	echo -n "$ARGS" > /tmp/tazlito.mergelist
}

merge_args()
{
	first=true
	while [ -n "$2" ]; do
		echo "$(stat -c "%s" $1) $1 $2"
		shift 2
	done | sort -nr | while read size file ram; do
		if $first; then
			cp $file $ISO_OUT
			echo -n "$ram $ISO_OUT "
			first=false
			continue
		fi
		dir=$tmp/$(basename $file)
		mkdir $dir
		mount -o loop,ro $file $dir
		echo -n "$ram $dir/boot/rootfs.gz "
	done
}

merge_action()
{
	set -- $(cat /tmp/tazlito.mergelist 2> /dev/null)
	rm -f /tmp/tazlito.mergelist
	tmp=/tmp/tazlitobox$$
	mkdir $tmp
	[ -n "$4" ] || return
	xterm -geometry 80x16 -title "build meta iso image $ISO_OUT" \
		-e "script -c 'tazlito merge $(merge_args $@)' && echo -e \"----\nENTER to continue...\" && read close"
	mv $ISO_OUT.merged $ISO_OUT
	for i in $tmp/*; do
		umount -d $i
	done
	rm -rf $tmp
}

# Boxes

export HELP='
<window title="Tazlito Box - Help" icon-name="media-cdrom">
<vbox>
	<text use-markup="true">
		<label>"
<b>Tazlito Box - Help</b>"
		</label>
	</text>
	
	<frame Overview>
		<text wrap="true" width-chars="52" use-markup="true">
			<label>
"
Tazlito Box is a tiny interface to the SliTaz Live Tool aka Tazlito.
You can simply generate a LiveCD in a few minutes, using your
own flavor or one from the SliTaz community. Tazlito can also 
generate a LiveCD flavor using all of the currently installed 
packages. Tazlito commands are executed in a XTerm, you can 
press ENTER or the mouse to close the window.
"
			</label>
		</text>
	</frame>
	
	<frame Default paths>
		<text wrap="true" width-chars="50" use-markup="true">
			<label>
"
Distro      : /home/slitaz/distro
Flavors    : /home/slitaz/flavors
Packages : /home/slitaz/packages
"
			</label>
		</text>
	</frame>
	
	<hbox>
		<button>
			<input file icon="exit"></input>
			<action type="closewindow">HELP</action>
		</button>
	</hbox>

</vbox>
</window>
'

# List all flavors on the mirror.
export LIST_FLAVORS_BOX='
<window title="Community flavors list" icon-name="tazlito">
<vbox>
	<tree>
		<width>500</width><height>200</height>
		<variable>COMMUNITY_FLAVOR</variable>
		<label>Name|ISO|Rootfs|Description</label>
		<input exported_column="0">$BIN list_flavors</input>
	</tree>
	<hbox>
		<button>
			<label>Recharge list</label>
			<input file icon="reload"></input>
			<action>$BIN recharge_list</action>
			<action>refresh:COMMUNITY_FLAVOR</action>
		</button>
		<button>
			<label>Exit</label>
			<input file icon="exit"></input>
			<action type="closewindow">LIST_FLAVORS_BOX</action>
		</button>
	</hbox>
</vbox>
</window>'

# Execute tazlito commands in a XTerm. Notebook: tab-pos="GTK_POS_LEFT"
export TAZLITO_BOX='
<window title="Tazlito Box" icon-name="media-cdrom">
<vbox>

	<hbox>
	<text use-markup="true">
		<label>"<b>SliTaz Live Tool</b>"</label>
	</text>
	<pixmap>
		<input file>/usr/share/pixmaps/tazlito.png</input>
	</pixmap>
	</hbox>

	<notebook labels="Writeiso|Live flavor|Flavors list|Gen flavor|Meta|Low RAM|Configuration">

	<frame Filesystem to ISO>

	<text wrap="true" width-chars="60" use-markup="true">
		<label>
"
Writeiso will generate an ISO image of the current filesystem as
is, including the /home directory. It is an easy way to remaster a
SliTaz Live system, you just have to: boot, modify, writeiso.
"
		</label>
	</text>

	<hbox>
		<text use-markup="true">
			<label>"<b>Compression:</b>"</label>
		</text>
		<combobox>
			<variable>COMPRESSION</variable>
			<item>gzip</item>
			<item>lzma</item>
			<item>none</item>
		</combobox>
		<button>
			<label>Writeiso</label>
			<input file icon="forward"></input>
			<action>$BIN writeiso_action</action>
		</button>
	</hbox>
	
	</frame>
	
	<frame Current packages selection>
	
	<text wrap="true" width-chars="60" use-markup="true">
		<label>
"
Gen Live flavor will create a LiveCD based on all the currently
installed packages. To build the rootfs and ISO image it will use
original SliTaz packages. Any addfiles can be copied before
generating the distro.
"
		</label>
	</text>

	<hbox>
		<text use-markup="true">
			<label>"<b>Flavor name :</b>"</label>
		</text>
		<entry>
			<default>slitaz</default>
			<variable>GEN_LIVEFLAVOR_NAME</variable>
		</entry>
	</hbox>

	<hbox>
		<button>
			<label>Addfiles</label>
			<input file icon="gtk-open"></input>
			<action>$BIN addfiles_action</action>
		</button>
		<button>
			<label>Gen distro</label>
			<input file icon="forward"></input>
			<action>$BIN gen_liveflavor_action</action>
		</button>
		<button>
			<label>Clean distro</label>
			<input file icon="edit-clear"></input>
			<action>$BIN clean_distro_action</action>
		</button>
	</hbox>

	</frame>
	
	<frame Community and personal flavors>
	
	<text wrap="true" width-chars="60" use-markup="true">
		<label>
"
Get and build preconfigured flavors from the community. The list of 
flavors can be recharged from the SliTaz mirror. Some addfiles can 
be copied before generating the distro.
"
		</label>
	</text>

	<hbox>
		<text use-markup="true">
			<label>"<b>Flavor :</b>"</label>
		</text>
		<entry>
			<default>core</default>
			<variable>COMMUNITY_FLAVOR</variable>
		</entry>
		<button>
			<label>List flavors</label>
			<input file icon="media-cdrom"></input>
			<action type="launch">LIST_FLAVORS_BOX</action>
		</button>
	</hbox>

	<hbox>
		<button>
			<label>Get flavor</label>
			<input file icon="forward"></input>
			<action>$BIN get_flavor_action</action>
		</button>
		<button>
			<label>Addfiles</label>
			<input file icon="gtk-open"></input>
			<action>$BIN addfiles_action</action>
		</button>
		<button>
			<label>Gen distro</label>
			<input file icon="forward"></input>
			<action>$BIN gen_distro_action</action>
		</button>
		<button>
			<label>Clean distro</label>
			<input file icon="edit-clear"></input>
			<action>$BIN clean_distro_action</action>
		</button>
	</hbox>

	</frame>
	<frame Generate a flavor>

	<text wrap="true" width-chars="60" use-markup="true">
		<label>
"
Gen Flavor will generate a new flavor file based on a rootfs in 
/home/slitaz/distro. All addfiles will be included in the flavor file.
"
		</label>
	</text>

	<hbox>
		<text use-markup="true">
			<label>"<b>New flavor :</b>"</label>
		</text>
		<entry>
			<default>slitaz</default>
			<variable>GEN_FLAVOR_NAME</variable>
		</entry>
	</hbox>
	
	<hbox>
		<button>
			<label>Addfiles</label>
			<input file icon="gtk-open"></input>
			<action>$BIN addfiles_action</action>
		</button>
		<button>
			<label>Gen new flavor</label>
			<input file icon="forward"></input>
			<action>$BIN gen_flavor_action</action>
		</button>
	</hbox>

	</frame>
	
	<frame Meta ISO image>

	<vbox>
		<hbox>
			<vbox>
				<text wrap="true" width-chars="33" use-markup="true">
					<label>
"
Combines several ISO flavors like
nested Russian dolls. The amount
of RAM available at startup will be
used to select the utmost one. "
					</label>
				</text>
				<hbox>
					<text use-markup="true">
						<label>"<b>ISO input: </b> "</label>
					</text>
					<entry accept="filename">
						<variable>ISO_IN</variable>
					</entry>
					<button>
						<input file stock="gtk-open"></input>
						<action type="fileselect">ISO_IN</action>
					</button>
				</hbox>
				<hbox>
					<text use-markup="true">
						<label>"<b>RAM needed:</b>  "</label>
					</text>
					<entry>
						<default>128M</default>
						<variable>ISO_RAM</variable>
					</entry>
					<button>
						<input file icon="add"></input>
						<action>$BIN merge_add</action>
						<action>refresh:ISO_ITEM</action>
					</button>
				</hbox>
			</vbox>
			<tree>
				<width>140</width><height>50</height>
				<label>RAM|ISO</label>
				<variable>ISO_ITEM</variable>
				<input>$BIN merge_input</input>
				<action>$BIN merge_del</action>
				<action>refresh:ISO_ITEM</action>
			</tree>
		</hbox>
		<hbox>
			<text use-markup="true">
				<label>"<b>ISO output:</b>"</label>
			</text>
			<entry accept="filename">
				<default>meta.iso</default>
				<variable>ISO_OUT</variable>
			</entry>
			<button>
				<input file icon="go-next"></input>
				<label>"Build ISO"</label>
				<action>$BIN merge_action</action>
			</button>
		</hbox>
	</vbox>
	
	</frame>
	
	<frame Low RAM ISO image>

	<vbox>
		<hbox>
	<text wrap="true" width-chars="25" use-markup="true">
		<label>
"
Build a Low RAM version
of a SliTaz ISO image.
The root filesystem is
compressed and slower.
"
		</label>
	</text>

			<vbox>
		<radiobutton>
			<label>The filesystem is always in RAM.</label>
			<variable>LORAM_RAM</variable>
		</radiobutton>
		<radiobutton>
			<label>The filesystem may be on a small CDROM.</label>
			<variable>LORAM_CDROM_SMALL</variable>
		</radiobutton>
		<radiobutton>
			<label>The filesystem may be on a large CDROM.</label>
			<variable>LORAM_CDROM</variable>
		</radiobutton>
			</vbox>
		</hbox>
		<hbox>
			<text use-markup="true">
				<label>"<b>ISO input: </b>"</label>
			</text>
			<entry accept="filename">
				<variable>LORAM_IN</variable>
			</entry>
			<button>
				<input file stock="gtk-open"></input>
				<action type="fileselect">LORAM_IN</action>
			</button>
		</hbox>
		<hbox>
			<text use-markup="true">
				<label>"<b>ISO output:</b>"</label>
			</text>
			<entry accept="filename">
				<default>loram.iso</default>
				<variable>LORAM_OUT</variable>
			</entry>
			<button>
				<input file icon="go-next"></input>
				<label>"Build ISO"</label>
				<action>$BIN loram_action</action>
			</button>
		</hbox>
	</vbox>
	
	</frame>
	
	<frame Flavor config and packages list>

	<text wrap="true" width-chars="60" use-markup="true">
		<label>
"
Before editing files you must have a flavor description. A distro
script can be used to perform actions into the rootfs before
compression.
"
		</label>
	</text>

	<hbox>
		<text use-markup="true">
			<label>"<b>Flavor config :</b>"</label>
		</text>
		<entry>
			<default>/home/slitaz/tazlito.conf</default>
			<variable>CONFIG_FILE</variable>
		</entry>
		<button>
			<input file icon="accessories-text-editor"></input>
			<action>editor $CONFIG_FILE</action>
		</button>
	</hbox>

	<hbox>
		<text use-markup="true">
			<label>"<b>Packages list :</b>"</label>
		</text>
		<entry>
			<default>/home/slitaz/distro-packages.list</default>
			<variable>PKGS_LIST</variable>
		</entry>
		<button>
			<input file icon="accessories-text-editor"></input>
			<action>editor $PKGS_LIST</action>
		</button>
	</hbox>

	<hbox>
		<text use-markup="true">
			<label>"<b>Distro script :</b> "</label>
		</text>
		<entry>
			<default>/home/slitaz/distro.sh</default>
			<variable>DISTRO_SCRIPT</variable>
		</entry>
		<button>
			<input file icon="accessories-text-editor"></input>
			<action>$BIN edit_distro_script</action>
		</button>
	</hbox>

	</frame>

	</notebook>

	<hbox>
		<button help>
			<input file icon="help-browser"></input>
			<action type="launch">HELP</action>
		</button>
		<button>
			<label>Exit</label>
			<input file icon="exit"></input>
			<action type="exit">Exit</action>
		</button>
	</hbox>

</vbox>
</window>
'

case $1 in
	*_*)
		# Exec all functions called by args (must have an underscore).
		$1 ;;
	*)
		# Tazlitobox action.
		check_root
		gtkdialog --center --program=TAZLITO_BOX ;;
esac

exit 0
