#!/bin/bash

# src/mp3toogg-gui.  Generated from mp3toogg-gui.sh by configure.
#
# Copyright 2004 Alfredo Pironti
# alfredio@ciaoweb.it
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

#some generic useful varibles
prefix="/usr"
exec_prefix="/usr"
bindir="/usr/bin"
datadir="/usr/share"
progname=`basename $0`
mp3toogg="$bindir/mp3toogg"
zenity="/usr/bin/zenity"
required_progs="$mp3toogg $zenity"

#needed for i18n
. gettext.sh

TEXTDOMAIN="mp3toogg"
export TEXTDOMAIN
TEXTDOMAINDIR="$datadir/locale"
export TEXTDOMAINDIR

starting() {
"$zenity" --info --title="`eval_gettext '$progname starting'`" \
--text="`gettext \"I am starting the job right now.\\nI will work in the background.\\nI will tell you when I have finished.\"`"
}

show_bitrate_sel() {
bitrate_window_title="`gettext \"Bitrate selection\"`"
bitrate_window_text="`gettext \"Please select output bitrate\"`"
bitrate=`"$zenity" --entry --title="$bitrate_window_title" --text="$bitrate_window_text"`
if [ $? -ne 0 ]; then # If the user press "cancel" we abort
	echo "`eval_gettext '$progname: User aborted.'`" >&$stderr
	exit $user_abort
fi

# Now we have to check if the bitrate is valid
if [ `echo "$bitrate" | egrep '^[0-9]+$'` ]; then # This is a number, we set the bitrate variable properly, then return
	bitrate=" -b $bitrate" # Remainder: this string must begin with a withespace
	return 0
else	# This is not a number, we warn the user, then recall this function again
	"$zenity" --warning --title="`gettext \"Bad bitrate selected\"`" --text="`gettext \"The bitrate must be a positive integer number.\"`"
	if [ $? -ne 0 ]; then # If the user press "cancel" we abort
		echo "`eval_gettext '$progname: User aborted.'`" >&$stderr
		exit $user_abort
	fi
	show_bitrate_sel
fi
}

stderr=2

# If the user passed any args from the command line we ignore it, but we report this on stderr
if [ $# -ne 0 ]; then
	for i in "$@"
		do echo "`eval_gettext '$progname: Ignoring command line parameter "$i"'`" >&$stderr
	done
fi

# exit code reminder:
not_found=63
user_abort=65

exiting=1 # Tells the program if we are going to quit due to an error

# Check for all needed programs to be correctly installed
for prog in $required_progs; do
	if [ ! -f "$prog" ]; then
		echo "`eval_gettext '$progname: needed $prog program not found.'`" >&$stderr
		exiting=0
	fi
done

if [ $exiting -eq 0 ]; then
	exit $not_found
fi
		
# Ask the user what files or directories he/she wants
file_sel="`"$zenity" --file-selection --title="\`gettext \"Select mp3s to convert\"\`" --multiple 2> /dev/null`"
if [ $? -ne 0 ]; then # If the user press "cancel" we report it and abort
	echo "`eval_gettext '$progname: User aborted.'`" >&$stderr
	exit $user_abort
fi

fs=1;count=0; # This is needed for the workaround to work
while [ $fs -ne 0 ] #this is the workaround
        do fs=`expr index "$file_sel" \|`
	if [ $fs -eq 0 ]; then
		if [ $count -eq 0 ]; then
			args="\"$file_sel\""
			count=1
		else	
			args="$args"" \"$file_sel\""
		fi
	else
		if [ $count -eq 0 ]; then
			args="\"${file_sel:0:$fs-1}\""
			count=1
		else
			args="$args"" \"${file_sel:0:$fs-1}\""
		fi
		file_sel=${file_sel:$fs}
	fi
done

#we set a variable for each long description of possible options to mp3toogg
#this way we get better i18n support and better code manteineance
conservative_desc="`gettext \"Do not delete mp3 files after a correct conversion\"`"
overwrite_desc="`gettext \"Do not overwrite any exisiting ogg file\"`"
forcing_desc="`gettext \"Convert mp3 files regardless of their exstension\"`"
recursive_desc="`gettext \"Descend subdirectories if any\"`"
symlinks_desc="`gettext \"Do not follow symlinks\"`"
bitrate_desc="`gettext \"Force output bitrate\"`"
logging_desc="`gettext \"Write output to log file\"`"
quiet_desc="`gettext \"Be quiet\"`"

opt_sel=`"$zenity" --title="\`gettext \"Select Options\"\`" --width="500" --height="300" --list --checklist --column="\`gettext \"Check\"\`" --column="\`gettext \"Options list\"\`" \
	"" "$conservative_desc" \
	"" "$overwrite_desc" \
	"" "$forcing_desc" \
	"" "$recursive_desc" \
	"" "$symlinks_desc" \
	"" "$bitrate_desc" \
	"" "$logging_desc" \
	"" "$quiet_desc"`

if [ $? -ne 0 ]; then # If the user press "cancel" we abort
	echo "`eval_gettext '$progname: User aborted.'`" >&$stderr
	exit $user_abort
fi

count=1
fs=1 # This is needed for the workaround to work
while [ $fs -ne 0 ] #this is the workaround
	do fs=`expr index "$opt_sel" \|`
	if [ $fs -eq 0 ]; then
		opt[$count]="$opt_sel"
	else
		opt[$count]="${opt_sel:0:$fs-1}"
	fi
	opt_sel=${opt_sel:$fs}
	count=$[$count+1]
done

for ((i=1;i<$count;i++))
	# Remainder: every textual option set must start with a whitespace
	do case "${opt[$i]}" in
		"$conservative_desc" ) conservative=" -c";;
		"$overwrite_desc" ) overwrite=" -o" ;;
		"$forcing_desc" ) forcing=" -f";;
		"$recursive_desc" ) recursive=" -r";;
		"$symlinks_desc" ) symlinks=" -n";;
		"$bitrate_desc" ) show_bitrate_sel ;; # This function set the bitrate variable
		"$logging_desc" ) logging=" -l";;
		"$quiet_desc" ) quiet=" -q";;
	esac
done

# Remainder: this string must not end with a whitespace
cmd_to_call="$mp3toogg""$bitrate""$conservative""$logging""$forcing""$overwrite""$recursive""$quiet""$symlinks"

# Do the work
starting &
eval $cmd_to_call $args
"$zenity" --info --title="`eval_gettext '$progname finished'`" --text="`gettext \"I finished my work.\\nGood listening.\"`"
exit 0
