summaryrefslogtreecommitdiff
path: root/url.sh
blob: 985a2d236c9025c7b309b5ba8560cfe91a4c7131 (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
#!/bin/sh
# vim: shiftwidth=4 tabstop=4 noexpandtab

# 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}"