-
Notifications
You must be signed in to change notification settings - Fork 0
/
wal.sh
executable file
·66 lines (57 loc) · 1.33 KB
/
wal.sh
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
#!/usr/bin/env sh
BUFFER=~/.local/share/wal
SLEEP_TIME=900
set_wp() {
hsetroot -cover "$1"
realpath "$1" > "$BUFFER"
}
get_random_wp() {
WP=$(find "$1" -type f | grep -i -e .jpg -e .jpeg -e .png | shuf -n1)
if [ -z "$WP" ]; then
echo No images found in provided directory. >&2
exit 1
fi
realpath "$WP"
}
set_random_wp() {
if WP_NEW=$(get_random_wp "$1"); then
set_wp "$WP_NEW"
else
exit 1
fi
}
delete_wp() {
WP_OLD=$(cat "$BUFFER")
if file -b --mime-type "$WP_OLD" | grep -q image; then
set_random_wp "$(dirname "$WP_OLD")"
rm "$WP_OLD"
else
echo Wallpaper-Path is corrupt: "$WP_OLD" >&2
exit 1
fi
}
loop() {
if [ "$(pgrep -f "wal -l" | wc -l)" -gt 2 ]; then
set_random_wp "$(dirname "$(cat "$BUFFER")")"
echo AN INSTANCE IS ALREADY RUNNING && return
fi
while true; do
set_random_wp "$(dirname "$(cat "$BUFFER")")"
sleep "$SLEEP_TIME"
done
}
[ ! -r $BUFFER ] && touch $BUFFER
case $1 in
"") set_random_wp . && return ;;
-l) loop && return ;;
-d) delete_wp && return ;;
-*) echo INVALID ARGUMENTS && return ;;
esac
if [ -d "$1" ]; then
set_random_wp "$1"
elif file -b --mime-type "$1" | grep -q image; then
set_wp "$1"
else
echo INVALID ARGUMENTS >&2
exit 1
fi