Skip to content

Instantly share code, notes, and snippets.

@yrps
Last active July 25, 2016 05:19
Show Gist options
  • Save yrps/53cb9e08ebae5ee52b747315ee5a0727 to your computer and use it in GitHub Desktop.
Save yrps/53cb9e08ebae5ee52b747315ee5a0727 to your computer and use it in GitHub Desktop.
Open archives with the Audacious media player
#!/bin/sh
set -eu
exclude="txt|nfo|bat|c|usflib|psflib"
invoke="$(basename "$0")"
if [ -d "$XDG_RUNTIME_DIR" ]; then
dest="$XDG_RUNTIME_DIR/$invoke"
else
dest="/tmp/$invoke-$USER"
fi
playlist="$dest/$invoke.m3u"
usage() {
cat <<USAGE
Usage: $invoke ARCHIVE1 [ARCHIVE2]...
Extracts archives containing audio files using atool to temporary directory
$dest and enqueues the contents.
Archives containing playlists will enqueue the playlist contents instead of
the files.
USAGE
}
if \! &>/dev/null command -v aunpack; then
usage
echo >&2 "The atool utility is required."
exit 2
fi
if [ $# -lt 1 ]; then
usage
echo >&2 "At least one archive is required."
exit 127
fi
test -d "$dest" && rm -r "$dest"
(umask 077 && mkdir "$dest")
# atool does not support:
# aunpack --quiet --each --subdir --extract-to="$dest" -- "$@"
# Assemble a playlist dynamically.
for arch in "$@"; do
# Extract archive to its own subdir.
dir="$dest/$(basename "$arch").d"
aunpack --quiet --extract-to="$dir" -- "$arch"
# shellcheck disable=SC2016
archlists="$(find "$dir" -type f -regextype egrep -iregex '.+\.(pls|m3u)' \
-print0 | xargs -0 awk -v subdir="$dir/" '{print subdir $0}' )"
if [ -n "$archlists" ]; then
# If playlists are in the archive, append their contents
echo "$archlists" >>"$playlist"
else
# otherwise sort and append all audio files from the archive.
find "$dir" -type f -regextype egrep ! -iregex ".+\.($exclude)" \
-print | sort >>"$playlist"
fi
done
audacious --enqueue-to-temp "$playlist"
@yrps
Copy link
Author

yrps commented Jul 23, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment