summaryrefslogtreecommitdiff
path: root/posix-shell/utils.sh
blob: 596263ecc63f726fd8beac410b8079378a73819f (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/bin/sh

# cdda2flac TARGET_FOLDER
#
# Script to rip the inserted audio compact disc (CD) to a folder of
# FLAC-compressed music files with original quality.
#
# Provide the wanted target folder as command line argument. It will be created
# during execution.
cdda2flac() {
	folder="$1"
	folder="$(realpath "${folder}")"
	echo "Saving to ${folder}"

	mkdir "${folder}"
	return_path="$(pwd)"
	cd "${folder}"

	cdparanoia -B

	find . -type f -iname '*.wav' -exec flac --delete-input-file {} \;

	cd "${return_path}"

	echo "\nDone! Files are saved to '${folder}'."
}

# cheat PROGRAM
#
# cheat gets the cheat sheet from https://cheat.sh about the given PROGRAM.
cheat() {
	curl "https://cheat.sh/${1}"
}

# radio STATION_NAME
#
# radio translates the STATION_NAME into a web radio URL via a user-provided
# TSV file and starts to play the station's audio stream with mpv.
radio() {
	# constants
	data_repo="${HOME}/.local/share/mydata"
	data_rel="tables/radio.tsv"
	data_abs="${data_repo}/${data_rel}"

	# read station from command line arguments
	station="$1"

	# set a handler for keyboard interrupts
	trap 'exit 0' 2 15

	# check dependency and warn user if necessary
	if ! command -v mpv > /dev/null 2>&1
	then
		echo 'You have to install mpv (e.g. sudo pacman -S mpv mpv-mpris)'
		exit 1
	fi

	# check if data file exists
	if ! [ -f "${data_abs}" ]
	then
		echo "Data file '""${data_abs}""' not found."
		exit 1
	fi

	# translate sender name to URL
	address="$(grep "${station}" "${data_abs}" | awk -F '\t' '{print $2}')"
	if [ "${address}" = "" ]
	then
		echo "Could not translate station ${station} to URL."
		exit 1
	fi

	# stream from address
	echo "Streaming ${station} from ${address}."
	mpv "${address}"
}

# showdot SOURCE [COMPILATION_ARGS]
#
# showdot displays a GraphViz source file (*.dot) with an image viewer. The
# source code is converted into a temporary image file on the fly.
showdot() {
	# process arguments
	code=$1
	shift

	file=$(mktemp)

	# compile *.dot code to *.png
	if ! dot "$@" -T png -o "${file}" "${code}"
	then
		echo "Failed to compile with GraphViz."
		exit 1
	fi

	# launch image viewer
	if ! sxiv "${file}"
	then
		echo "Failed to display generated image with sxiv."
		exit 1
	fi

	rm "${file}"
}

# tv STATION_NAME
#
# tv translates the STATION_NAME into a tv stream URL via a user-provided TSV
# file and starts to play the station's stream with mpv.
tv() {
	# constants
	data_repo="${HOME}/.local/share/mydata"
	data_rel="tables/tv.tsv"
	data_abs="${data_repo}/${data_rel}"

	# read station from command line arguments
	station="$1"

	# set a handler for keyboard interrupts
	trap 'exit 0' 2 15

	# check dependency and warn user if necessary
	if ! command -v mpv > /dev/null 2>&1
	then
		echo 'You have to install mpv (e.g. sudo pacman -S mpv mpv-mpris)'
		exit 1
	fi

	# check if data file exists
	if ! [ -f "${data_abs}" ]
	then
		echo "Data file '""${data_abs}""' not found."
		exit 1
	fi

	# translate sender name to URL
	address="$(grep "${station}" "${data_abs}" | awk -F '\t' '{print $2}')"
	if [ "${address}" = "" ]
	then
		echo "Could not translate station ${station} to URL."
		exit 1
	fi

	# stream from address
	echo "Streaming ${station} from ${address}."
	mpv "${address}"
}

# url
#
# url opens a menu program with URL names saved in a user-provided TSV file and
# opens the selected URL with a browser.
url() {
	# constants
	data_repo="${HOME}/.local/share/mydata"
	data_rel="tables/urls.tsv"
	data_abs="${data_repo}/${data_rel}"

	# check if data file exists
	if ! [ -f "${data_abs}" ]
	then
		echo "Data file '""${data_abs}""' not found."
		exit 1
	fi

	# select a menu program or panic
	if command -v bemenu > /dev/null 2>&1
	then
		menu='bemenu'
	elif command -v dmenu > /dev/null 2>&1
	then
		menu='dmenu'
	elif command -v sxmo_dmenu.sh > /dev/null 2>&1
	then
		menu='sxmo_dmenu.sh'
	else
		echo 'You have to install bemenu, sxmo_dmenu.sh or dmenu!'
		exit 1
	fi

	# check firefox dependency and warn user if necessary
	if ! command -v firefox > /dev/null 2>&1
	then
		echo 'You have to install firefox.'
		exit 1
	fi

	# let user select the URL
	closer="[close menu]"
	options="$(awk -F '\t' '{print $2}' < "${data_abs}" | sed "$ a ${closer}")"
	key="$(echo "${options}" | ${menu} -i -l 10)"

	# close if user selected the close entry or stopped menu with ESC
	if [ "${key}" = "${closer}" ] || [ "${key}" = "" ]
	then
		exit 0
	fi

	# search for the selected key in data file
	url=""
	while read -r line
	do
		if [ "$(echo "${line}" | awk -F '\t' '{print $2}')" = "${key}" ]
		then
			if [ "${url}" = "" ]
			then
				url="$(echo "${line}" | awk -F '\t' '{print $1}')"
			else
				# warn if multiple URLs have this key
				echo "More than one URL in data file matching selected key!" 1>&2
				break
			fi
		fi
	done < "${data_abs}"

	# open selected URL in browser
	firefox "${url}"
}