blob: c551ca938cf0ec90ca1cc81d946356a2cda62ed3 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
#!/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)
# Iterate through configuration files, source any that exist
# shellcheck disable=SC2066,SC2068
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 2
fi
# Start building array of args for find(1)
declare -a fargs
for term ; do
fargs[${#fargs[@]}]=-ipath
fargs[${#fargs[@]}]=\*"$term"\*
done
# 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 "$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 "$player" "${opts[@]}" -- "${matches[0]}"
;;
# More than one match; print the names
*)
printf '%s\n' "${matches[@]}"
;;
esac
|