diff options
Diffstat (limited to 'plz')
-rwxr-xr-x | plz | 57 |
1 files changed, 46 insertions, 11 deletions
@@ -1,54 +1,89 @@ #!/usr/bin/env bash +# +# plz -- Play a media file from a configured directory with a configured media +# player if it's a unique match for all the words given as arguments. If not +# unique, print all the matches to help the user pick unique terms. +# +# $ plz led zep stairw +# $ plz black sab iron m +# $ plz beeth symph 9 +# +# Create a file /etc/plzrc and/or ~/.plzrc to use: +# +# dir=/path/to/media +# player=mplayer +# +# Author: Tom Ryder <tom@sanctum.geek.nz> +# Copyright: 2016 +# License: MIT +# + +# Name self self=plz +# Specify array of configuration files; /etc/plzrc and ~/.plzrc, probably declare -a confs # shellcheck disable=SC2140 confs=(/etc/"$self"rc "$HOME"/."$self"rc) -for conf in "${confs[@]}" ; do +# Iterate through configuration files, source any that exist +for conf in "${PLZ_CONFIG:-${confs[@]}}" ; do [[ -e "$conf" ]] || continue source "$conf" done +# Default the configuration to the environment variables if set +: "${dir:=$PLZ_DIR}" +: "${player:=$PLZ_PLAYER}" + +# If we still don't have a media directory or player, bail +: "${dir:?}" +: "${player:?}" + +# Make usage printing function usage() { printf 'USAGE: %s TERM1 [TERM2 ...]\n' \ "$self" >&2 } +# If no arguments, print usage and exit with an error if ! (($#)) ; then usage >&2 - exit 1 + exit 2 fi +# Start building array of args for find(1) declare -a fargs for term ; do fargs[${#fargs[@]}]=-ipath fargs[${#fargs[@]}]=\*"$term"\* done -shopt -s extglob - +# Collect an array of the full paths of matching files declare -a matches while IFS= read -d '' -r file ; do matches[${#matches[@]}]=$file -done < <( - find "${PLZ_DIR:-$HOME/Music}" \ - -type f "${fargs[@]}" -print0 -) +done < <(find "$dir" -type f "${fargs[@]}" -print0) +# Action depends on count of matches case ${#matches[@]} in + + # No matches; report and exit with errors 0) printf '%s: No matches.\n' \ "$self" >&2 exit 1 ;; + + # One match; play the file 1) - exec "${PLZ_PLAYER:-mpv}" -- "${matches[0]}" + exec "$player" -- "${matches[0]}" ;; + + # More than one match; print the names *) - printf '%s\n' \ - "${matches[@]}" + printf '%s\n' "${matches[@]}" ;; esac |