aboutsummaryrefslogtreecommitdiff
path: root/wallpaper/wpPointer.sh
blob: a816f186abb9faf3a48801b34d1b354803c18c79 (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
#!/bin/sh

# wpPointer.sh: Set next or previous wallpaper in wallpaper history or set and add one to it
#
# Usage: wpPointer.sh [-n|p] [-o] FILE...
#
# -n sets next wallpaper if it exists
# -p sets previous wallpaper if it exists
# -o opens recorded wallpapers in sxiv

usageMessage="Usage: ${0} [-n|p] [-o] FILE..."

wpDatFile="$CONFIG_WPLIST"
wpPointerFile="$CONFIG_WPPOINTER"
recordedMax="30"

nextOrPrev=""
next="n"
prev="p"

open=""

error() {
	printf '%s error: %s\n' "$0" "$*" >&2
	notify-send "${0} error" "$*"
}

errorAndUsage() {
	printf '%s error: %s\n%s\n' "$0" "$*" "$usageMessage" >&2
	notify-send "${0} error" "${*}\n\n${usageMessage}"
}

getRecordedAmount() {
	wc -l "$wpDatFile" | cut -d' ' -f1
}

setWP() {
	wp="$1"
	feh --bg-max "$wp"
	ln -sf "$wp" "${HOME}/wp"
}

setWpByPointer() {
	pointer="$1"
	wp="$(sed -n "${pointer}p" "$wpDatFile")"
	echo "$pointer" >"$wpPointerFile"
	if ! setWP "$wp"; then
		error "Unable to set ${wp} as wallpaper."
	fi
}

setNextOrPrev() {

	nextOrPrev="$1"

	if ! [ -f "$wpDatFile" ]; then
		error "${wpDatFile} does not exist or is not a regular file. Maybe no wallpapers were registered yet."
		exit 2
	fi

	if ! [ -s "$wpDatFile" ]; then
		error "${wpDatFile} is empty."
		exit 2
	fi

	datFileLength="$(getRecordedAmount)"

	if [ -f "$wpPointerFile" ]; then
		pointer="$(cat "$wpPointerFile")"
	else
		pointer="$datFileLength"
	fi

	if [ "$nextOrPrev" = "$next" ]; then

		if [ "$pointer" -eq "$datFileLength" ]; then
			error 'Last recorded wallpaper already selected.'
			exit 3
		fi

		pointer=$((pointer+1))
		setWpByPointer "$pointer"

	fi

	if [ "$nextOrPrev" = "$prev" ]; then

		if [ "$pointer" -eq "1" ]; then
			error 'First recorded wallpaper already selected.'
			exit 3
		fi

		pointer=$((pointer-1))
		setWpByPointer "$pointer"

	fi

}

addWp() {

	wp="$1"

	if ! [ -f "$1" ]; then
		error "File ${1} does not exist."
		exit 4
	fi

	if ! setWP "$wp"; then
		error "Unable to set ${wp} as wallpaper."
		exit 4
	fi

	wpPath="$(readlink -f "$wp")"
	# Double \ for explicit \ to reach variable!
	wpPathSanitized="$(echo "$wpPath" | sed 's|/|\\/|g')"

	# Delete existing references to the wallpaper to avoid repetition in history
	sed -i "/^${wpPathSanitized}$/d" "$wpDatFile"

	echo "$wpPath" >>"$wpDatFile"
	[ "$(getRecordedAmount)" -gt "$recordedMax" ] && sed -i "1d" "$wpDatFile"
	getRecordedAmount >"$wpPointerFile"

}

# Check pointer file makes sense

if [ -f "$wpPointerFile" ]; then
	pointer="$(cat "$wpPointerFile")"
	pointerAsNumber="$(echo "$pointer" | sed 1q | grep -E '[0-9]+')"
	if [ "$pointer" != "$pointerAsNumber" ]; then
		rm "$wpPointerFile"
	else
		if [ "$pointer" -lt 1 ]; then
			echo "1" >"$wpPointerFile"
		fi
		recordedAmount=$(getRecordedAmount)
		if [ "$pointer" -gt "$recordedAmount" ]; then
			echo "$recordedAmount" >"$wpPointerFile"
		fi
	fi
fi

# Process options

while getopts ':npo' opt; do
	case $opt in

		'n' )
			if [ -n "$nextOrPrev" ]; then
				errorAndUsage 'Tried to use -n and -p at the same time.'
				exit 1
			fi
			setNextOrPrev "$next"
			;;

		'p' )
			if [ -n "$nextOrPrev" ]; then
				errorAndUsage 'Tried to use -n and -p at the same time.'
				exit 1
			fi
			setNextOrPrev "$prev"
			;;

		'o' )
			open="y"
			;;

		'?' )
			printf '%s\n' "$usageMessage"
			exit 1

	esac
done
shift $((OPTIND - 1))

# Set and add arguments to wallpaper history

for wp in "$@"; do
	addWp "$wp"
done

[ -n "$open" ] && sxiv -t $(cat "$wpDatFile")