#!/bin/sh
# 
# Gtkdialog box to burn CD or DVD using Wodim with basic options. Burnbox
# can burn ISO images, blank rewritable CD or create audio CD with WAV.
# 
# (c) - SliTaz GNU/Linux 2009 - GNU GPL v3
#
VERSION=20090204

# Display audio cd size.
audio_cd_stats()
{
	AUDIO_CD_SIZE=`du -m /tmp/audio-cd | awk '{print $1}'`
	TRACK_NB=`ls -1 /tmp/audio-cd | wc -l`
	echo -n "Total tracks: $AUDIO_CD_SIZE Mb"
}

decode_ogg()
{
	if ls /tmp/audio-cd | grep -q .ogg; then
		cd /tmp/audio-cd
		xterm -bg gray93 -fg black -geometry 80x16 -title "Oggdec" \
			-e 'for i in *.ogg; do oggdec "$i" && rm "$i"; done; sleep 2'
	fi
}

# We must think about " space " between directory/track and
# filter directory copy to avoid copying other files than audio.
# Only burning non-compressed wav format is supported directly.
copy_audio_file()
{
	if ls "$NEW_TRACK" | grep -q .ogg; then
		cp "${NEW_TRACK%.ogg}.ogg" /tmp/audio-cd
		decode_ogg
	else
		cp "${NEW_TRACK%.wav}.wav" /tmp/audio-cd
	fi
}
copy_audio_dir()
{
	for i in .wav .ogg
	do
		cp "$NEW_DIR"/*$i /tmp/audio-cd 2>/dev/null
	done
	decode_ogg
}

burn_iso()
{
	xterm -bg gray93 -fg black -geometry 80x16 -title "Wodim" \
		-e "wodim -v speed=$SPEED dev=$DEVICE $OPTIONS $ISO_IMAGE; sleep 4"
}

# Use -pad to avoid file size error.
burn_audio()
{
	xterm -bg gray93 -fg black -geometry 80x16 -title "Wodim" \
		-e "wodim -v speed=$SPEED dev=$DEVICE $OPTIONS -pad -dao -audio /tmp/audio-cd/*.wav; sleep 4"
}

# Main GTK interface
MAIN_DIALOG='
<window title="SliTaz - Burnbox" icon-name="drive-optical">
<vbox>

	<notebook labels="General|ISO image|Audio CD">
	
	<vbox>
		<frame Information>
			<text>
				<label>
"Burn CD or DVD using Wodim from cdrkit. Before burning,
please verify device writer settings or add options if needed.
Audio CD uses uncompressed WAV (OGG are decoded)."
				</label>
			</text>
		</frame>
		<frame Settings>
			<hbox>
				<text use-markup="true">
					<label>"<b>Device:     </b>"</label>
				</text>
				<entry>
					<default>/dev/cdrom</default>
					<variable>DEVICE</variable>
				</entry>
			</hbox>
			<hbox>
				<text use-markup="true">
					<label>"<b>Speed:     </b>"</label>
				</text>
				<entry>
					<input>cat /proc/sys/dev/cdrom/info | grep "drive speed" | cut -f 3</input>
					<variable>SPEED</variable>
				</entry>
			</hbox>
			<hbox>
				<text use-markup="true">
					<label>"<b>Options:  </b>"</label>
				</text>
				<entry>
					<default>-eject -multi</default>
					<variable>OPTIONS</variable>
				</entry>
				<button>
					<input file icon="help"></input>
					<action>xterm -sb -bg gray93 -fg black -geometry 95x25 -title "wodim help" -e "wodim --help ; echo -e \"----\nENTER to continue...\" && read close"</action>
				</button>
			</hbox>
		</frame>
		<frame Blank CD/DVD-RW>
			<hbox>
				<text use-markup="true">
					<label>"<b>Option:   </b>"</label>
				</text>
				<entry>
					<variable>BLANK_OPTS</variable>
					<default>fast</default>
				</entry>
				<button>
					<input file icon="help"></input>
					<action>xterm -bg gray93 -fg black -geometry 80x15 -title "wodim blank=help" -e "wodim blank=help ; echo -e \"----\nENTER to continue...\" && read close"</action>
				</button>
				<button>
					<label>Blank disk</label>
					<input file icon="forward"></input>
					<action>xterm -bg gray93 -fg black -title "Wodim" -e "wodim -v -blank=$BLANK_OPTS dev=$DEVICE; sleep 2"</action>
				</button>
			</hbox>
		</frame>
	</vbox>
	
	<vbox>
		<frame Select and burn>
		
			<text>
				<label>
"
You can create or manipulate ISO images with ISO Master utility
or use genisoimage from the command line.
"
				</label>
			</text>
		
			<hbox>
				<text use-markup="true">
					<label>"<b>ISO path:</b>"</label>
				</text>
				<entry>
					<variable>ISO_IMAGE</variable>
				</entry>
				<button>
					<input file stock="gtk-cdrom"></input>
					<action type="fileselect">ISO_IMAGE</action>
				</button>
			</hbox>'
# Burn iso button.
MAIN_DIALOG=${MAIN_DIALOG}"
			<hbox>
				<button>
					<label>Burn disk</label>
					<input file icon=\"forward\"></input>
					<action>$0 burn_iso</action>
				</button>
			</hbox>"
MAIN_DIALOG=${MAIN_DIALOG}'
		</frame>
		
	</vbox>
	<vbox>	
		<tree icon_name="audio-x-generic">
			<width>500</width><height>200</height>
			<variable>TRACKS_LIST</variable>
			<label>Track name</label>
			<input>ls -1 /tmp/audio-cd</input>
			<action>rm "/tmp/audio-cd/$TRACKS_LIST"</action>
			<action>refresh:TRACKS_LIST</action>
			<action>refresh:TRACKS_SIZE</action>
		</tree>'
# Select, add and burn audio buttons.
MAIN_DIALOG=${MAIN_DIALOG}"
		<hbox>
			<text>
				<label>Title:   </label>
			</text>
			<entry accept=\"filename\">
				<label>Select an Audio track</label>
				<variable>NEW_TRACK</variable>
			</entry>
			<button>
				<input file stock=\"gtk-open\"></input>
				<action type=\"fileselect\">NEW_TRACK</action>
			</button>
			<button>
				<label>Add</label>
				<input file stock=\"gtk-add\"></input>
				<action>$0 copy_audio_file</action>
				<action>refresh:TRACKS_LIST</action>
				<action>refresh:TRACKS_SIZE</action>
			</button>
		</hbox>
		<hbox>
			<text>
				<label>Album:</label>
			</text>
			<entry accept=\"directory\">
				<label>Select an Audio track</label>
				<variable>NEW_DIR</variable>
			</entry>
			<button>
				<input file stock=\"gtk-open\"></input>
				<action type=\"fileselect\">NEW_DIR</action>
			</button>
			<button>
				<label>Add</label>
				<input file stock=\"gtk-add\"></input>
				<action>$0 copy_audio_dir</action>
				<action>refresh:TRACKS_LIST</action>
				<action>refresh:TRACKS_SIZE</action>
			</button>
		</hbox>
		<hbox>
			<text>
				<variable>TRACKS_SIZE</variable>
				<input>$0 audio_cd_stats</input>
			</text>
			<button>
				<label>Clean</label>
				<input file stock=\"gtk-clear\"></input>
				<action>rm -rf /tmp/audio-cd/*</action>
				<action>refresh:TRACKS_LIST</action>
				<action>refresh:TRACKS_SIZE</action>
				<action>clear:NEW_TRACK</action>
				<action>clear:NEW_DIR</action>
			</button>
			<button>
				<label>Burn disk</label>
				<input file icon=\"forward\"></input>
				<action>$0 burn_audio</action>
			</button>
		</hbox>
	</vbox>"
export MAIN_DIALOG=${MAIN_DIALOG}'
	
	</notebook>
	
	<hbox>
		<button>
			<label>Exit</label>
			<input file icon="exit"></input>
			<action type="exit">Exit</action>
		</button>
	</hbox>
	
</vbox>
</window>
'

# Script can be called with an arg to exec a function.
if [ -n "$1" ]; then
	$1
else
	mkdir -p /tmp/audio-cd
	gtkdialog --center --program=MAIN_DIALOG >/dev/null
	rm -rf /tmp/audio-cd
fi

exit 0
