#!/bin/sh
# Little script to cook flavor in tazwok chroot waiting
# for something more consistent into tazlito.

[ "$1" ] || { echo "Usage: gen-iso flavorname [gzip]" >&2; exit 1; }
source /usr/lib/slitaz/libtaz

# Report progress on webserver.
log_step="$LOCAL_REPOSITORY/log/step"
run_on_exit="$run_on_exit
rm -f $LOCAL_REPOSITORY/log/step
rm -f $LOCAL_REPOSITORY/log/package"

[ -d "$SLITAZ_DIR/flavors" ] || ln -s $LOCAL_REPOSITORY/flavors $SLITAZ_DIR
if [ -d "$SLITAZ_DIR/flavors/$1" ]; then
	source_lib report
	export log_opt=$LOCAL_REPOSITORY/log/iso-$1.html
	rm -f $log_opt
	echo "iso-$1" > $LOCAL_REPOSITORY/log/package
	report start
	if ! [ -x /usr/bin/tazlito ]; then
		report step "Installing tazlito"
		tazpkg get-install tazlito || report exit "Installation of tazlito failed."
	fi
	[ -d "$SLITAZ_DIR/distro" ] && rm -r $SLITAZ_DIR/distro
	report step "Generating ISO $1"
	cd $tmp
	tazlito get-flavor $1 || report exit "Get flavor failed"
	sed "/^ISO_NAME/ s/$1/$SLITAZ_VERSION-$1/" -i tazlito.conf
	[ "$2" = gzip ] && echo "COMPRESSION=gzip" >> tazlito.conf
	echo -e "\n" | tazlito gen-distro || report exit "Generation of iso failed"
	cp $SLITAZ_DIR/distro/*.iso $SLITAZ_DIR/distro/*.md5 $LOCAL_REPOSITORY/iso
	clean-chroot
	report stop
else
	echo "$SLITAZ_DIR/flavors/$1 doesn't exists." >&2
	exit 1
fi
