--- flac.old/test/test_seeking.sh	2013-03-11 10:29:30.293319471 -0500
+++ flac/test/test_seeking.sh	2013-03-11 15:33:13.459498543 -0500
@@ -17,141 +17,387 @@
 #  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=../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/flac:$PATH
 PATH=../src/metaflac:$PATH
 PATH=../src/test_seeking:$PATH
 PATH=../src/test_streams:$PATH
 PATH=../objs/$BUILD/bin:$PATH
 
+# If FLAC__TEST_LEVEL is not set, default to '1'
 if [ x"$FLAC__TEST_LEVEL" = x ] ; then
 	FLAC__TEST_LEVEL=1
 fi
 
-flac --help 1>/dev/null 2>/dev/null || die "ERROR can't find flac executable"
-metaflac --help 1>/dev/null 2>/dev/null || die "ERROR can't find metaflac executable"
+# Find where `flac', `metaflac', and `valgrind' are located in $PATH
+flac_binary="$(command -v flac)"
+metaflac_binary="$(command -v metaflac)"
+valgrind_binary="$(command -v valgrind)"
+
+# General die function
+die () {
+	printf "%s\n" "$@" 1>&2
+	exit 1
+}
 
-run_flac ()
-{
+# Run `flac' with configured arguments
+run_flac () {
+	# 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 flac $*" >>test_seeking.valgrind.log
-		valgrind --leak-check=yes --show-reachable=yes --num-callers=100 --log-fd=4 flac $* 4>>test_seeking.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 flac $@" >>test_seeking.valgrind.log
+
+			# `Valgrind' command wrapping around `flac'
+			"$valgrind_binary" \
+				--leak-check=yes \
+				--show-reachable=yes \
+				--num-callers=100 \
+				--log-fd=4 \
+					"$flac_binary" \
+						"$@" \
+						4>>test_seeking.valgrind.log
+		else
+			die "ERROR can't find \`valgrind' executable"
+		fi
+
+	# Run test _WITHOUT_ `valgrind'
 	else
-		flac $*
+		"$flac_binary" "$@"
 	fi
 }
 
-run_metaflac ()
-{
+# Run `metaflac' with configured arguments
+run_metaflac () {
+	# 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 metaflac $*" >>test_seeking.valgrind.log
-		valgrind --leak-check=yes --show-reachable=yes --num-callers=100 --log-fd=4 metaflac $* 4>>test_seeking.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 metaflac $@" >>test_seeking.valgrind.log
+
+			# `Valgrind' command wrapping around `metaflac'
+			"$valgrind_binary" \
+				--leak-check=yes \
+				--show-reachable=yes \
+				--num-callers=100 \
+				--log-fd=4 \
+					"$metaflac_binary" \
+						"$@" \
+						4>>test_seeking.valgrind.log
+		else
+			die "ERROR can't find \`valgrind' executable"
+		fi
+
+	# Run test _WITHOUT_ `valgrind'
 	else
-		metaflac $*
+		"$metaflac_binary" "$@"
 	fi
 }
 
-run_test_seeking ()
-{
+# Run `test_seeking' with configured arguments
+run_test_seeking () {
+	# `test_libFLAC' is an executable which as was added to $PATH
+
+	# Test with `valgrind' if enabled.  If `test_seeking' 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_seeking $*" >>test_seeking.valgrind.log
-		valgrind --leak-check=yes --show-reachable=yes --num-callers=100 --log-fd=4 test_seeking $* 4>>test_seeking.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_seeking $@" >>test_seeking.valgrind.log
+
+			# `Valgrind' command wrapping around `test_seeking'
+			"$valgrind_binary" \
+				--leak-check=yes \
+				--show-reachable=yes \
+				--num-callers=100 \
+				--log-fd=4 \
+					test_seeking \
+						"$@" \
+						4>>test_seeking.valgrind.log
+		else
+			die "ERROR can't find \`valgrind' executable"
+		fi
+
+	# Run test _WITHOUT_ `valgrind'
 	else
-		test_seeking $*
+		test_seeking "$@"
 	fi
 }
 
-echo "Checking for --ogg support in flac..."
-if flac --ogg --silent --force-raw-format --endian=little --sign=signed --channels=1 --bps=8 --sample-rate=44100 -c $0 1>/dev/null 2>&1 ; then
-	has_ogg=yes;
-	echo "flac --ogg works"
+################
+#  BEGIN TEST  #
+################
+
+# Test for `flac' and `metaflac' executables, exiting if they cannot be found
+[ -n "${flac_binary}" ] || die "ERROR can't find flac \`flac' executable"
+[ -n "${metaflac_binary}" ] || die "ERROR can't find flac \`metaflac' executable"
+
+# Print out current operation
+printf "Checking for --ogg support in flac...\n"
+
+# Check if compiled `flac' has OGG support
+if "$flac_binary" --ogg --silent --force-raw-format --endian=little --sign=signed --channels=1 --bps=8 --sample-rate=44100 -c $0 1>/dev/null 2>&1 ; then
+	# `flac' executable support OGG
+	has_ogg=yes
+
+	# Print out OGG support
+	printf "flac --ogg works\n"
 else
-	has_ogg=no;
-	echo "flac --ogg doesn't work"
+	# `flac' executable does _NOT_ support OGG
+	has_ogg=no
+
+	# Print out no OGG support
+	printf "flac --ogg doesn't work\n"
 fi
 
+# Print out current operation
+printf "Generating streams...\n"
 
-echo "Generating streams..."
+# Check if 'noise.raw' exists before running `test_streams'
 if [ ! -f noise.raw ] ; then
-	test_streams || die "ERROR during test_streams"
+	test_streams || die "ERROR during \`test_streams'"
 fi
 
-echo "generating FLAC files for seeking:"
-run_flac --verify --force --silent --force-raw-format --endian=big --sign=signed --sample-rate=44100 --bps=8 --channels=1 --blocksize=576 -S- --output-name=tiny.flac noise8m32.raw || die "ERROR generating FLAC file"
-run_flac --verify --force --silent --force-raw-format --endian=big --sign=signed --sample-rate=44100 --bps=16 --channels=2 --blocksize=576 -S- --output-name=small.flac noise.raw || die "ERROR generating FLAC file"
-run_flac --verify --force --silent --force-raw-format --endian=big --sign=signed --sample-rate=44100 --bps=8 --channels=1 --blocksize=576 -S10x --output-name=tiny-s.flac noise8m32.raw || die "ERROR generating FLAC file"
-run_flac --verify --force --silent --force-raw-format --endian=big --sign=signed --sample-rate=44100 --bps=16 --channels=2 --blocksize=576 -S10x --output-name=small-s.flac noise.raw || die "ERROR generating FLAC file"
+# Print out current operation
+printf "Generating FLAC files for seeking:\n"
 
-tiny_samples=`metaflac --show-total-samples tiny.flac`
-small_samples=`metaflac --show-total-samples small.flac`
+# Generate various FLAC files
 
+# tiny.flac
+run_flac \
+	--verify \
+	--force \
+	--silent \
+	--force-raw-format \
+	--endian=big \
+	--sign=signed \
+	--sample-rate=44100 \
+	--bps=8 \
+	--channels=1 \
+	--blocksize=576 \
+	-S- \
+	--output-name=tiny.flac \
+	noise8m32.raw || die "ERROR generating FLAC file"
+
+# small.flac
+run_flac \
+	--verify \
+	--force \
+	--silent \
+	--force-raw-format \
+	--endian=big \
+	--sign=signed \
+	--sample-rate=44100 \
+	--bps=16 \
+	--channels=2 \
+	--blocksize=576 \
+	-S- \
+	--output-name=small.flac \
+	noise.raw || die "ERROR generating FLAC file"
+
+# tiny-s.flac
+run_flac \
+	--verify \
+	--force \
+	--silent \
+	--force-raw-format \
+	--endian=big \
+	--sign=signed \
+	--sample-rate=44100 \
+	--bps=8 \
+	--channels=1 \
+	--blocksize=576 \
+	-S10x \
+	--output-name=tiny-s.flac \
+	noise8m32.raw || die "ERROR generating FLAC file"
+
+# small-s.flac
+run_flac \
+	--verify \
+	--force \
+	--silent \
+	--force-raw-format \
+	--endian=big \
+	--sign=signed \
+	--sample-rate=44100 \
+	--bps=16 \
+	--channels=2 \
+	--blocksize=576 \
+	-S10x \
+	--output-name=small-s.flac \
+	noise.raw || die "ERROR generating FLAC file"
+
+# Obtain total samples from 'tiny.flac' and 'small.flac'
+tiny_samples="$(metaflac --show-total-samples tiny.flac)"
+small_samples="$(metaflac --show-total-samples small.flac)"
+
+# Set the 'tiny.flac' seek count to '100'
 tiny_seek_count=100
+
+# If FLAC__TEST_LEVEL is set to greater than 1, set a high seek count
 if [ "$FLAC__TEST_LEVEL" -gt 1 ] ; then
 	small_seek_count=10000
 else
 	small_seek_count=100
 fi
 
-for suffix in '' '-s' ; do
-	echo "testing tiny$suffix.flac:"
-	if run_test_seeking tiny$suffix.flac $tiny_seek_count $tiny_samples noise8m32.raw ; then : ; else
-		die "ERROR: during test_seeking"
+#-------------------------------------------------#
+# Run `test_seeking' on FLAC files _WITH_ samples #
+#-------------------------------------------------#
+for flac in 'tiny.flac' 'small.flac' 'tiny-s.flac' 'small-s.flac' ; do
+
+	# Print current file
+	printf "Testing '${flac}':\n"
+
+	# 'tiny.flac' or 'tiny-s.flac'
+	if [ "$flac" = tiny.flac ] || [ "$flac" = tiny-s.flac ] ; then
+
+		# Run test and check exit code
+		if ! run_test_seeking "$flac" "$tiny_seek_count" "$tiny_samples" noise8m32.raw ; then
+			# `run_test_seeking' failed
+			die "ERROR: during test_seeking"
+		fi
 	fi
 
-	echo "testing small$suffix.flac:"
-	if run_test_seeking small$suffix.flac $small_seek_count $small_samples noise.raw ; then : ; else
-		die "ERROR: during test_seeking"
-	fi
+	# 'small.flac' or 'small-s.flac'
+	if [ "$flac" = small.flac ] || [ "$flac" = small-s.flac ] ; then
 
-	echo "removing sample count from tiny$suffix.flac and small$suffix.flac:"
-	if run_metaflac --no-filename --set-total-samples=0 tiny$suffix.flac small$suffix.flac ; then : ; else
-		die "ERROR: during metaflac"
+		# Run test and check exit code
+		if ! run_test_seeking "$flac" "$small_seek_count" "$small_samples" noise.raw ; then
+			# `run_test_seeking' failed
+			die "ERROR: during test_seeking"
+		fi
 	fi
+done
 
-	echo "testing tiny$suffix.flac with total_samples=0:"
-	if run_test_seeking tiny$suffix.flac $tiny_seek_count $tiny_samples noise8m32.raw ; then : ; else
-		die "ERROR: during test_seeking"
-	fi
+# Remove total sample from each FLAC file for next operation
 
-	echo "testing small$suffix.flac with total_samples=0:"
-	if run_test_seeking small$suffix.flac $small_seek_count $small_samples noise.raw ; then : ; else
-		die "ERROR: during test_seeking"
+# Print removal operation
+printf "Removing sample count from 'tiny.flac' 'small.flac' 'tiny-s.flac' & 'small-s.flac':\n"
+
+# Remove total samples from all the FLAC files
+if ! run_metaflac --no-filename --set-total-samples=0 tiny*.flac small*.flac ; then
+	# `metaflac' failed
+	die "ERROR: during metaflac"
+fi
+
+#----------------------------------------------------#
+# Run `test_seeking' on FLAC files _WITHOUT_ samples #
+#----------------------------------------------------#
+for flac in 'tiny.flac' 'small.flac' 'tiny-s.flac' 'small-s.flac' ; do
+
+	# Print current file
+	printf "Testing '${flac}' with total_samples=0:\n"
+
+	# 'tiny.flac' or 'tiny-s.flac'
+	if [ "$flac" = "tiny.flac" ] || [ "$flac" = "tiny-s.flac" ] ; then
+
+		# Run test and check exit code
+		if ! run_test_seeking "$flac" "$tiny_seek_count" "$tiny_samples" noise8m32.raw ; then
+			# `run_test_seeking' failed
+			die "ERROR: during test_seeking"
+		fi
 	fi
-done
 
-if [ $has_ogg = "yes" ] ; then
+	# 'small.flac' or 'small-s.flac'
+	if [ "$flac" = "small.flac" ] || [ "$flac" = "small-s.flac" ] ; then
 
-	echo "generating Ogg FLAC files for seeking:"
-	run_flac --verify --force --silent --force-raw-format --endian=big --sign=signed --sample-rate=44100 --bps=8 --channels=1 --blocksize=576 --output-name=tiny.oga --ogg noise8m32.raw || die "ERROR generating Ogg FLAC file"
-	run_flac --verify --force --silent --force-raw-format --endian=big --sign=signed --sample-rate=44100 --bps=16 --channels=2 --blocksize=576 --output-name=small.oga --ogg noise.raw || die "ERROR generating Ogg FLAC file"
-	# seek tables are not used in Ogg FLAC
+		# Run test and check exit code
+		if ! run_test_seeking "$flac" "$small_seek_count" "$small_samples" noise.raw ; then
+			# `run_test_seeking' failed
+			die "ERROR: during test_seeking"
+		fi
+	fi
+done
 
-	echo "testing tiny.oga:"
-	if run_test_seeking tiny.oga $tiny_seek_count $tiny_samples noise8m32.raw ; then : ; else
+# If `flac' was compiled with Ogg support, run `test_seeking' on some generated
+# Ogg FLAC files
+if [ "$has_ogg" = "yes" ] ; then
+
+	# Print out current operation
+	printf "Generating Ogg FLAC files for seeking:\n"
+
+	# Generate various Ogg FLAC files
+	#
+	# NOTE: Seektables are _NOT_ used in Ogg FLAC files, so we don't need to
+	#       to test for them
+
+	# tiny.oga
+	run_flac \
+		--verify \
+		--force \
+		--silent \
+		--force-raw-format \
+		--endian=big \
+		--sign=signed \
+		--sample-rate=44100 \
+		--bps=8 \
+		--channels=1 \
+		--blocksize=576 \
+		--output-name=tiny.oga \
+		--ogg noise8m32.raw || die "ERROR generating Ogg FLAC file"
+
+	# small.oga
+	run_flac \
+		--verify \
+		--force \
+		--silent \
+		--force-raw-format \
+		--endian=big \
+		--sign=signed \
+		--sample-rate=44100 \
+		--bps=16 \
+		--channels=2 \
+		--blocksize=576 \
+		--output-name=small.oga \
+		--ogg noise.raw || die "ERROR generating Ogg FLAC file"
+
+	#--------------------------------------#
+	# Run `test_seeking' on Ogg FLAC files #
+	#--------------------------------------#
+	# Print current file
+	printf "Testing 'tiny.oga':\n"
+
+	# Run test and check exit code
+	if ! run_test_seeking tiny.oga "$tiny_seek_count" "$tiny_samples" noise8m32.raw ; then
+		# `run_test_seeking' failed
 		die "ERROR: during test_seeking"
 	fi
 
-	echo "testing small.oga:"
-	if run_test_seeking small.oga $small_seek_count $small_samples noise.raw ; then : ; else
+	# Print current file
+	printf "Testing 'small.oga':\n"
+
+	# Run test and check exit code
+	if ! run_test_seeking small.oga "$small_seek_count" "$small_samples" noise.raw ; then
+		# `run_test_seeking' failed
 		die "ERROR: during test_seeking"
 	fi
 
 fi
 
+# Clean up the temporarily created FLAC files, if applicable
 rm -f tiny.flac tiny.oga small.flac small.oga tiny-s.flac small-s.flac
