summary refs log tree commit diff stats
path: root/radio
blob: dc1297ae92b5839a08badab679ab3c9623344ddb (plain)
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env sh
# -*- sh -*-

: "${RADIO_STATIONS:=${XDG_CONFIG_HOME:-$HOME/.config}/radio/stations}"
: "${RADIO_PLAYER:=$(command -v mpv 2>/dev/null)}"

_radio_url=1
_radio_desc=2
_radio_tags=3

RADIO_PID_FILE=/tmp/radio.pid

usage() {
	cat <<EOF
RADIO: play the radio
USAGE:	radio -h
	radio -k
	radio -l [QUERY]
	radio [STATION]

PARAMETERS:
 STATION	Radio station to play.  Stations are defined in
		\$RADIO_STATIONS (default: $RADIO_STATIONS).
		If STATION matches more than one station, or if it
		isn't present, radio will display a menu to select
		from.

FLAGS:
 -h		Show this help.
 -k		Kill the currently-playing radio invocation.
 -e		Edit \$RADIO_STATIONS using \$EDITOR.
 -l [QUERY]	List all known stations and exit.
		If QUERY is present, filter the list to those stations
		matching QUERY.

See radio(1) and radio.stations(5) for more details. (TODO)
EOF
	exit ${1:-0}
}

_radio_play() {
	candidates="$(sed -e 1d -e '/^#/d' "$RADIO_STATIONS" | grep -i "$1")"
	if [ -z "$candidates" ]; then
		candidates="$(sed -e 1d -e '/^#/d' "$RADIO_STATIONS")"
	fi
	if [ "$(printf '%s\n' "$candidates" | wc -l)" -gt 1 ]; then
		printf '%s\n' "$candidates" | cut -f $_radio_desc | cat -n -
		read -p "Radio> " radio
		if ! (echo "$radio" | grep -qE '^[0-9]+$'); then
			exit 1
		fi
		station="$(printf '%s\n' "$candidates" | head -n "$radio" | tail -n 1)"
	else
		station="$candidates"
	fi
	echo >&2 "Playing $(echo "$station" | cut -f $_radio_desc)..."
	"$RADIO_PLAYER" "$(echo "$station" | cut -f $_radio_url)" &
	echo $! >"$RADIO_PID_FILE"
	exit
}

_radio_kill() {
	if ! xargs -a "$RADIO_PID_FILE" kill; then
		echo >&2 "I don't think radio is running."
		exit 1
	fi
	exit
}

_radio_edit() {
	exec $EDITOR "$RADIO_STATIONS"
}

_radio_list() {
	sed 1d "$RADIO_STATIONS" |
		grep "$1" |
		awk '
	BEGIN { FS="\t"; }
	$0 !~ /^#/ {
	   descstr = $'$_radio_desc';
	   urlstr = $'$_radio_url';
	   tagstr = $'$_radio_tags';
	   printf "%-23s\t", substr(descstr,1,20) (length(descstr)>20?"...":"");
	   printf "%-23s\t", substr(tagstr,1,20) (length(tagstr)>20?"...":"");
	   printf "%-23s\n", substr(urlstr,1,20) (length(urlstr)>20?"...":"");
	}'
	exit
}

main() {
	while getopts :khel: opt; do
		case $opt in
		h) usage ;;
		k) _radio_kill ;;
		e) _radio_edit ;;
		l) _radio_list "$OPTARG" ;;
		:)
			case "$OPTARG" in
			l) _radio_list ;;
			*)
				echo >&2 "Option -$OPTARG requires an argument"
				exit 1
				;;
			esac
			;;
		\?)
			echo >&2 "Uknown option: -$OPTARG"
			exit 1
			;;
		esac
	done
	shift $((OPTIND - 1))
	_radio_play "$@"
}

main "$@"