#!/bin/sh
#
# Live system creation wizard in GTK using Yad.
#
# Copyright (C) 2012-2015 SliTaz GNU/Linux - GNU gpl v2
# Authors : Christophe Lincoln <pankso@slitaz.org>
#

default_icon="--image=slitaz-icon"
opts="--height=320 --width=600 --center --image-on-top --window-icon=slitaz-icon --title=LiveWizard"
rel=$(cat /etc/slitaz-release)
#[ "$rel" != "cooking" ] && rel=stable
live="/home/slitaz/$rel/live"
db='/var/lib/tazpkg'
list="$live/distro-packages.list"
distro="/home/slitaz/$rel/distro"
addfiles="$distro/addfiles"


# TazLito wizard is only for root.

if [ $(id -u) -ne 0 ]; then
	exec tazbox su $0
	exit 0
fi


# I18n

. /lib/libtaz.sh
export TEXTDOMAIN='tazlito'


# Sanity check.

mkdir -p $live && cd $live
#rm -rf *


#
# Functions
#

edit_list() {
	cat $list | yad --list $opts --image='system-software-update' \
		--text="<b>$(_ 'Edit the distro packages list')</b>" \
		--no-headers --print-all --separator='' \
		--editable --column=0:TEXT > "$live/list"
		mv -f "$live/list" "$list"
}


# Start page GUI

start_main() {
	yad --form $opts $default_icon \
		--text="<b>$(_ 'SliTaz Live system creator wizard')</b>" \
		--field="$(_ "Distro name:")" \
		--field="$(_ "Based on:")":CB \
		--field="":LBL --field=" ":LBL \
		--field="$(_ "*.flavor file (optional):")":SFL \
		--button='gtk-help:4' \
		--button="$(_ 'Write ISO')!iso-image-burn:3" \
		--button="$(_ 'TazPanel Live'):2" \
		--button='gtk-cancel:1' \
		--button='gtk-go-forward:0' \
		'custom' 'core!core64!gtkonly!justx!base'
}


# Start page handler

start() {
	# Store box results
	main=$(start_main)

	# Deal with --button values
	case $? in
		1) exit 0;;
		2) tazpanel live; exit 0;;
		3) terminal -T 'write-iso' -e 'tazlito writeiso lzma'; exit 0;;
		4) tazweb 'file:///usr/share/doc/tazlito/tazlito.html'; exit 0;;
	esac

	# Deal with $main values

	(
		export output='raw'
		# Numbers are moved the progressbar
		echo '30'
		# Lines started with '#' are displayed in the log
		tazpkg recharge | sed 's|^.*|#&|'
		name="$(echo $main | cut -d'|' -f1)"; name="${name:-custom}"
		skel="$(echo $main | cut -d'|' -f2)"
		flvf="$(echo $main | cut -d'|' -f5)"
		if [ ${flvf/*./} = flavor ]; then
			cp -a $flvf $live ; skel=$(basename $flvf)
		fi
		echo "$skel" > $live/skel
		echo '60'
		tazlito get-flavor $skel | sed 's|^.*|#&|'
		echo '90'
		sed -i "s|^ISO_NAME=.*|ISO_NAME=\"$name\"|" tazlito.conf
		sed -i "s|^VOLUM_NAME=.*|VOLUM_NAME=\"SliTaz $name\"|" tazlito.conf
	) | \
	yad --progress $opts --image='system-software-update' \
		--text="<b>$(_ 'Getting flavor file and packages list...')</b>" \
		--enable-log="$(_ 'Log')" --log-expanded --button='gtk-go-forward:0'

}


# Packages page GUI

pkgs_main() {
	pkgs=$(cat $list | wc -l)
	skel=$(cat $live/skel)
	text=$(_p 'Packages - The "%s" has %d package' 'Packages - The "%s" has %d packages' "$pkgs"   "$skel" "$pkgs")
	yad --form $opts --image='application-x-tazpkg' \
		--text="<b>$text</b>" --separator=' ' \
		--field="$(_ 'Additional packages separated by space or by line:')\\n$(_ '(will be auto added to "Edit packages list")')":TXT \
		--button="$(_ 'Edit packages list')!document-properties:2" \
		--button='gtk-cancel:1' --button='gtk-go-forward:0'
}


# Packages page handler

pkgs() {
	# Store box results
	main=$(pkgs_main)
	# Deal with --button values
	case $? in
		1) exit 0 ;;
		2) add_to_list  ; edit_list ;;
		*) add_to_list ;;
	esac
}


add_to_list() {
	for pkg in $(echo $main | sed s'/\\n/ /'g); do
		vers=$(grep -E "^$pkg \|" $db/packages.desc | awk '{print $3}')
		[ -z $vers ] || \
		(grep -v -q "^$pkg-$vers" $list && \
			echo "$pkg-$vers" >> $list
		)
		unset vers
	done
}


# Wallpaper page GUI

wallpaper_main() {
	yad --form $opts --image='preferences-desktop-wallpaper' \
		--text="<b>$(_ 'SliTaz desktop wallpaper')</b>" --separator='' \
		--field="$(_ 'Wallpaper JPG image:')":FL \
		--button='gtk-cancel:1' --button='gtk-go-forward:0'
}


# Wallpaper page handler

wallpaper() {
	# Store box results
	main=$(wallpaper_main)
	# Deal with --button values
	case $? in
		1) exit 0 ;;
		*) continue ;;
	esac
	if echo "$main" | fgrep -q .jpg; then
		mkdir -p $addfiles/rootfs/usr/share/images
		cp -f $main $addfiles/rootfs/usr/share/images
	fi
}


# Last page GUI

gen_distro_main() {
	(
		_ "
Now it's time to generate the distro. Last chance to start over or stop. \
Creating a Live system uses quite a lot of resources and takes some time.
Note you can still add some files to the SliTaz root filesystem or on the CD-ROM."
		echo
		echo $addfiles
	) | yad --text-info $opts $default_icon \
		--text="<b>$(_ 'Generate the distribution')</b>" --wrap --margins=20 \
		--button='gtk-cancel:1' --button='gtk-go-forward:0'
}


# Last page handler

gen_distro() {
	# Store box results
	main=$(gen_distro_main)

	# Deal with --button values
	case $? in
		1) exit 0 ;;
		*)
			export output='raw'
			echo -e "\n" | tazlito gen-distro 2>&1 | yad \
			--text-info $opts $default_icon --tail \
			--text="<b>$(_ 'Building the Live system...')</b>" \
			--button='gtk-go-forward:0'
			;;
	esac
}


# Summary

summary() {
	. ./tazlito.conf
	iso_size=$(du -sh $distro/$ISO_NAME.iso | awk '{print $1}')
	distro_size=$(du -sh $distro/rootfs | awk '{print $1}')
	text="$(_ 'Live system summary')"
	echo -e "\
$(_ 'Generated ISO') \n$distro/$ISO_NAME.iso
$(_ 'Image size') \n$iso_size
$(_ 'Uncompressed size') \n$distro_size" | \
	yad --list $opts $default_icon --text="<b>$text</b>" \
		--column="$(_ 'Information')":0 --column="$(_ 'Value')":1 \
		--button="gtk-close":0
}


#
# Script commands
#

case "$1" in
	usage)
		echo "Usage: $(basename $0) [command]" ;;
	*)
		start
		pkgs
		wallpaper
		gen_distro
		summary ;;
esac

exit 0
