#! /usr/bin/env bash
USAGE="$0: /path/to/APEFile"
if [ $# -lt 1 ]; then echo -e "Error: Ensure that you're supplying the path to your .APE file.\n$USAGE" >&2; exit 1; fi
file=$1
tracks=`gawk -vRS="TRACK" 'END {print NR-1}' "$file"`
shnsplit -f "$file" -o "flac flac -V --best -o %f -" "$1" -t "%n - %t" 2>&1 | \
gawk -vvar=$tracks 'NR>=1 && NR<=var {print "#Track ",NR," of ", var;print (NR-1)*100/var};{fflush();}' | \
(if $(zenity --title="Splitting Tracks..." --progress --auto-close --percentage=0);
then
echo 'done'
else
killall shnsplit
exit 1
fi)