--- flac.old/test/test_grabbag.sh	2013-03-11 10:29:30.292319483 -0500
+++ flac/test/test_grabbag.sh	2013-03-13 01:16:50.042582372 -0500
@@ -17,134 +17,247 @@
 #  restrictive of those mentioned above.  See the file COPYING.Xiph in this
 #  distribution.
 
-die ()
-{
-	echo $* 1>&2
-	exit 1
-}
-
+# Checks for debug build
 if [ x = x"$1" ] ; then 
 	BUILD=debug
 else
 	BUILD="$1"
 fi
 
+# Set up various paths to libs
 LD_LIBRARY_PATH=../src/libFLAC/.libs:$LD_LIBRARY_PATH
 LD_LIBRARY_PATH=../src/share/grabbag/.libs:$LD_LIBRARY_PATH
 LD_LIBRARY_PATH=../src/share/replaygain_analysis/.libs:$LD_LIBRARY_PATH
 LD_LIBRARY_PATH=../objs/$BUILD/lib:$LD_LIBRARY_PATH
+
+# Must be exported
 export LD_LIBRARY_PATH
 export MALLOC_CHECK_=3
 export MALLOC_PERTURB_=$(($RANDOM % 255 + 1))
+
+# Set up various paths to built binaries in-tree
 PATH=../src/test_grabbag/cuesheet:$PATH
 PATH=../src/test_grabbag/picture:$PATH
 PATH=../objs/$BUILD/bin:$PATH
 
-test_cuesheet -h 1>/dev/null 2>/dev/null || die "ERROR can't find test_cuesheet executable"
-test_picture -h 1>/dev/null 2>/dev/null || die "ERROR can't find test_picture executable"
+# General die function
+die () {
+	printf "%s\n" "$@" 1>&2
+	exit 1
+}
 
-run_test_cuesheet ()
-{
+# Run `test_cuesheet' with configured arguments
+run_test_cuesheet () {
+	# Test with `valgrind' if enabled.  If `flac' was _NOT_ compiled with debug
+	# symobls, this will error out and the valgrind.log will specify why
 	if [ x"$FLAC__TEST_WITH_VALGRIND" = xyes ] ; then
-		echo "valgrind --leak-check=yes --show-reachable=yes --num-callers=100 test_cuesheet $*" >>test_grabbag.valgrind.log
-		valgrind --leak-check=yes --show-reachable=yes --num-callers=100 --log-fd=4 test_cuesheet $* 4>>test_grabbag.valgrind.log
+
+		# Check for `valgrind' before continuing
+		if [ -n "$valgrind_binary" ] ; then
+			# Print out command executed to log
+			printf "%s\n" "valgrind --leak-check=yes --show-reachable=yes --num-callers=100 test_cuesheet $@" >>test_grabbag.valgrind.log
+
+			# `Valgrind' command wrapping around `test_cuesheet'
+			valgrind \
+				--leak-check=yes \
+				--show-reachable=yes \
+				--num-callers=100 \
+				--log-fd=4 \
+					"$test_cuesheet" \
+						"$@" \
+						4>>test_grabbag.valgrind.log
+		else
+			die "ERROR: can't find \`valgrind' executable"
+		fi
+
+	# Run test _WITHOUT_ `valgrind'
 	else
-		test_cuesheet $*
+		"$test_cuesheet" "$@"
 	fi
 }
 
-run_test_picture ()
-{
+# Run `test_picture' with configured arguments
+run_test_picture () {
+	# Test with `valgrind' if enabled.  If `flac' was _NOT_ compiled with debug
+	# symobls, this will error out and the valgrind.log will specify why
 	if [ x"$FLAC__TEST_WITH_VALGRIND" = xyes ] ; then
-		echo "valgrind --leak-check=yes --show-reachable=yes --num-callers=100 test_picture $*" >>test_grabbag.valgrind.log
-		valgrind --leak-check=yes --show-reachable=yes --num-callers=100 --log-fd=4 test_picture $* 4>>test_grabbag.valgrind.log
+
+		# Check for `valgrind' before continuing
+		if [ -n "$valgrind_binary" ] ; then
+			# Print out command executed to log
+			printf "%s\n" "valgrind --leak-check=yes --show-reachable=yes --num-callers=100 test_picture $@" >>test_grabbag.valgrind.log
+
+			# `Valgrind' command wrapping around `test_cuesheet'
+			valgrind \
+				--leak-check=yes \
+				--show-reachable=yes \
+				--num-callers=100 \
+				--log-fd=4 \
+					"$test_picture" \
+						"$@" \
+						4>>test_grabbag.valgrind.log
+		else
+			die "ERROR: can't find \`valgrind' executable"
+		fi
+
+	# Run test _WITHOUT_ `valgrind'
 	else
-		test_picture $*
+		"$test_picture" "$@"
 	fi
 }
 
-if [ `env | grep -ic '^comspec='` != 0 ] ; then
+# Find where `test_cuesheet', `test_picture', `valgrind', and `diff' are located
+# in $PATH
+test_cuesheet="$(command -v test_cuesheet)"
+test_picture="$(command -v test_picture)"
+valgrind_binary="$(command -v valgrind)"
+diff_binary="$(command -v diff)"
+
+# Test for `test_cuesheet', `test_picture' and `diff' executable, exiting if not
+# found
+[ -n "$test_cuesheet" ] || die "ERROR: can't find \`test_cuesheet' executable"
+[ -n "$test_picture" ] || die "ERROR: can't find \`test_picture' executable"
+[ -n "$diff_binary" ] || die "ERROR: can't find \`diff' executable"
+
+# Test if running on Windows platform
+if [ $(env | grep -ic '^comspec=') != 0 ] ; then
+	# Windows
 	is_win=yes
 else
+	# Non Windows
 	is_win=no
 fi
 
-########################################################################
-#
-# test_picture
-#
-########################################################################
+#---------------#
+# Test Pictures #
+#---------------#
+# Log file name
+log="picture.log"
+
+# Picture directory
+picture_dir="pictures"
+
+# Print current operation
+printf "Running test_picture...\n"
+
+# Remove old 'picture.log', if it exists from previous runs
+rm -f "$log"
+
+# Run `test_picture' using the specified picture directory, logging everything
+# (STDOUT/STDERR) to the specified log file
+run_test_picture "$picture_dir" >> "$log" 2>&1
+
+# [ WINDOWS ] Run `diff' on control 'picture.ok' and the newly created
+# 'picture.log', displaying the difference (if any) to 'picture.diff'.  If the
+# exit code of the operation is not '0', display error and exit
+if [ "$is_win" = "yes" ] ; then
+	diff -w picture.ok "$log" > picture.diff || die "Error: '$log' file does not match 'picture.ok' file, see 'picture.diff'"
+
+# [ NON WINDOWS ] Run `diff' on control 'picture.ok' and the newly created
+# 'picture.log', displaying the difference (if any) to 'picture.diff'.  If the
+# exit code of the operation is not '0', display error and exit
+else
+	diff picture.ok "$log" > picture.diff || die "Error: '$log' file does not match 'picture.ok' file, see 'picture.diff'"
+fi
 
-log=picture.log
-picture_dir=pictures
+# Print 'Test Picture' test passed
+printf "PASSED (results are in ${log})\n"
 
-echo "Running test_picture..."
+#----------------#
+# Test Cuesheets #
+#----------------#
+# Log file name
+log="cuesheet.log"
 
-rm -f $log
+# Store all the 'bad' cuesheets into a variable
+bad_cuesheets="cuesheets/bad.*.cue"
 
-run_test_picture $picture_dir >> $log 2>&1
+# Store all the 'good' cuesheets into a variable
+good_cuesheets="cuesheets/good.*.cue"
 
-if [ $is_win = yes ] ; then
-	diff -w picture.ok $log > picture.diff || die "Error: .log file does not match .ok file, see picture.diff"
-else
-	diff picture.ok $log > picture.diff || die "Error: .log file does not match .ok file, see picture.diff"
-fi
+# Store a 'good leadout' to be used when testing cuesheets
+good_leadout="$(( 80 * 60 * 44100 ))"
 
-echo "PASSED (results are in $log)"
+# Store a 'bad leadout' to be used when testing cuesheets
+bad_leadout="$(( $good_leadout + 1 ))"
 
-########################################################################
-#
-# test_cuesheet
-#
-########################################################################
-
-log=cuesheet.log
-bad_cuesheets=cuesheets/bad.*.cue
-good_cuesheets=cuesheets/good.*.cue
-good_leadout=`expr 80 \* 60 \* 44100`
-bad_leadout=`expr $good_leadout + 1`
-
-echo "Running test_cuesheet..."
-
-rm -f $log
-
-#
-# negative tests
-#
+# Print current operation
+printf "Running test_cuesheet...\n"
+
+# Remove old 'cuesheet.log', if it exists from previous runs
+rm -f "$log"
+
+# Negative Tests
+#----------------
+# Run through all the 'bad' cuesheets, running `test_cuesheet' on them and
+# checking whether `test_cuesheet' successfully completes
 for cuesheet in $bad_cuesheets ; do
-	echo "NEGATIVE $cuesheet" >> $log 2>&1
-	run_test_cuesheet $cuesheet $good_leadout 44100 cdda >> $log 2>&1
-	exit_code=$?
+
+	# Print current operation
+	printf "%s\n" "NEGATIVE $cuesheet" >> "$log" 2>&1
+
+	# Run `test_cuesheet' on current cuesheet with configured arguments/options,
+	# logging STDOUT/STDERR to the configured log file
+	run_test_cuesheet "$cuesheet" "$good_leadout" 44100 cdda >> "$log" 2>&1
+
+	# Store exit code of `test_cuesheet'
+	exit_code="$?"
+
+	# If the exit code of `test_cuesheet' is '255', then display error and exit
 	if [ "$exit_code" = 255 ] ; then
 		die "Error: test script is broken"
 	fi
-	cuesheet_pass1=${cuesheet}.1
-	cuesheet_pass2=${cuesheet}.2
-	rm -f $cuesheet_pass1 $cuesheet_pass2
+
+	# Remove temporary cuesheet passes from `test_cuesheet'
+	rm -f "${cuesheet}.1" "${cuesheet}.2"
 done
 
-#
-# positve tests
-#
+# Positive Tests
+#----------------
+# Run through all the 'good' cuesheets, running `test_cuesheet' on them and
+# checking whether `test_cuesheet' successfully completes
 for cuesheet in $good_cuesheets ; do
-	echo "POSITIVE $cuesheet" >> $log 2>&1
-	run_test_cuesheet $cuesheet $good_leadout 44100 cdda >> $log 2>&1
-	exit_code=$?
+
+	# Print current operation
+	printf "%s\n" "POSITIVE $cuesheet" >> "$log" 2>&1
+
+	# Run `test_cuesheet' on current cuesheet with configured arguments/options,
+	# logging STDOUT/STDERR to the configured log file
+	run_test_cuesheet "$cuesheet" "$good_leadout" 44100 cdda >> "$log" 2>&1
+
+	# Store exit code of `test_cuesheet'
+	exit_code="$?"
+
+	# If the exit code of `test_cuesheet' is '255', then display error and exit
 	if [ "$exit_code" = 255 ] ; then
 		die "Error: test script is broken"
+
+	# If the exit code of `test_cuesheet' is _NOT_ '0' or '255', then display
+	# error and exit
 	elif [ "$exit_code" != 0 ] ; then
 		die "Error: good cuesheet is broken"
 	fi
-	cuesheet_pass1=${cuesheet}.1
-	cuesheet_pass2=${cuesheet}.2
-	diff $cuesheet_pass1 $cuesheet_pass2 >> $log 2>&1 || die "Error: pass1 and pass2 output differ"
-	rm -f $cuesheet_pass1 $cuesheet_pass2
+
+	# Run `diff' on first and second pass from `test_cuesheet', logging
+	# STDOUT/STDERR to the configured log file.  Display error and exit if the
+	# output from pass pass and second pass differ
+	# -- Positive Tests Only --
+	"$diff_binary" "${cuesheet}.1" "${cuesheet}.2" >> "$log" 2>&1 || die "Error: 'Pass 1' and 'Pass 2' output differ, see '$log'"
+
+	# Remove temporary cuesheet passes from `test_cuesheet'
+	rm -f "${cuesheet}.1" "${cuesheet}.2"
 done
 
-if [ $is_win = yes ] ; then
-	diff -w cuesheet.ok $log > cuesheet.diff || die "Error: .log file does not match .ok file, see cuesheet.diff"
+# [ WINDOWS ] If the generated log from the 'Negative' and 'Positive' tests
+# differ, display error and exit
+if [ "$is_win" = "yes" ] ; then
+	"$diff_binary" -w cuesheet.ok "$log" > cuesheet.diff || die "Error: '$log' file does not match 'cuesheet.ok' file, see 'cuesheet.diff'"
+
+# [ NON WINDOWS ] If the generated log from the 'Negative' and 'Positive' tests
+# differ, display error and exit
 else
-	diff cuesheet.ok $log > cuesheet.diff || die "Error: .log file does not match .ok file, see cuesheet.diff"
+	"$diff_binary" cuesheet.ok "$log" > cuesheet.diff || die "Error: '$log' file does not match 'cuesheet.ok' file, see 'cuesheet.diff'"
 fi
 
-echo "PASSED (results are in $log)"
+# Print 'Test Cuesheet' test passed
+printf "PASSED (results are in $log)\n"
