summary refs log tree commit diff stats
path: root/radio
blob: d94d58532a173bbe7dcb20e8795d38c27d1c8af8 (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/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
RADIO_STATUS_FILE=/tmp/radio.status

usage() {
	cat <<EOF
RADIO: play the radio
USAGE:	radio -h
	radio -k
	radio [-s|-S]
	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.
 -K		Kill all instances of $RADIO_PLAYER.
 -r		Begin playing most recently-played radio, or prompt user.
 -e		Edit \$RADIO_STATIONS using \$EDITOR.
 -s		Show radio's status and exit.
 -S		Show radio's status until the user quits with C-c.
 -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}
}

main() {
	while getopts :kKrsShel: opt; do
		case $opt in
		h) usage ;;
		k) _radio_kill ;;
		K)
			pkill "$RADIO_PLAYER"
			exit
			;;
		r) set -- "$(cat /tmp/radio.last_played)" ;;
		e) _radio_edit ;;
		s) _radio_status ;;
		S) _radio_status -f ;;
		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 "$@"
}

_radio_status() {
	trap 'echo;exit 0' INT
	if [ "x$1" = x-f ]; then
		follow=-f
	else
		follow=
	fi
	tail $follow "$RADIO_STATUS_FILE"
	echo
	exit
}

_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 "$station" | cut -f $_radio_desc >/tmp/radio.last_played

	if [ -f "$RADIO_PID_FILE" ]; then
		_radio_kill_impl
	fi

	echo >&2 "Playing $(echo "$station" | cut -f $_radio_desc)..."
	"$RADIO_PLAYER" "$(echo "$station" | cut -f $_radio_url)" >"$RADIO_STATUS_FILE" 2>&1 &
	echo $! >"$RADIO_PID_FILE"
	exit
}

_radio_kill_impl() {
	while xargs -a "$RADIO_PID_FILE" kill 2>/dev/null; do
		printf '.'
	done
}

_radio_kill() {
	if ! [ -f "$RADIO_PID_FILE" ]; then
		echo >&2 "I don't think radio is running."
		exit 1
	fi
	printf >&2 '%s' "Killing radio"
	_radio_kill_impl
	rm "$RADIO_PID_FILE"
	echo
	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 "$@"