From aecac52d087a550a1d16dc685e72c2ddc3cbb1f8 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Sat, 26 Mar 2016 23:52:07 +1300 Subject: Dress up a fair bit --- plz | 57 ++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 46 insertions(+), 11 deletions(-) diff --git a/plz b/plz index 8d597b2..9e6a0f4 100755 --- a/plz +++ b/plz @@ -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 +# 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 -- cgit v1.2.3