summaryrefslogtreecommitdiffstats
path: root/.local/bin/statusbar/sb-iplocate
blob: a1606e1129e40e51bf832ea323aa9a033372701b (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
#!/bin/sh

# Gets your public ip address checks which country you are in and
# displays that information in the statusbar
#
# https://www.maketecheasier.com/ip-address-geolocation-lookups-linux/

ifinstalled "geoip" || exit 1

getip() {
	{ grep -q -m1 '^up$' /sys/class/net/w*/operstate || grep -q -m1 '^up$' /sys/class/net/e*/operstate; } &&
		curl -sf api.ipify.org --output "$ipfile"
}

ipfile="$XDG_RUNTIME_DIR/iplocate"
addr=$(cat "$ipfile" 2>/dev/null) && addr=$(geoiplookup "$addr" 2>/dev/null) && rm "$ipfile" ||
	( flock -n 9 &&
		( tries=0; while [ $tries -ne 100 ]; do
			getip && break ||
				{ tries=$((tries+1)); sleep .1; }
		done
		! [ -f "$ipfile" ] &&
			until getip; do sleep 60; done &&
		pkill -RTMIN+"${1:-27}" "${STATUSBAR:-dwmblocks}"
		) &
	echo; exit ) 9>"${XDG_RUNTIME_DIR}/sb-iplocate.lock"
name="${addr##*, }"
flag="$(grep "flag: $name" "${XDG_DATA_HOME:-$HOME/.local/share}/larbs/emoji")"
flag="${flag%% *}"
printf "%s %s\\n" "$flag" "$name"