summaryrefslogtreecommitdiff
path: root/scripts/Scripts/setwall
blob: 25bcfb04cffdb1fda636d4f331642ec789028c72 (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
#!/usr/bin/env bash

WALLFILE="$HOME/Pictures/wallconfig"
WALLLINK="$HOME/Pictures/current_wall"
BINNAME="setwall"

function unsetwalls() {
	for wall in $@; do
        echo unsetting $wall
		sed -i.bak ":$wall:d" $WALLFILE
	done
}
function unsetcur() {
    if [ -f "${WALLLINK}" ]; then
        unsetwalls $(realpath --relative-to=$HOME $WALLLINK)
    else
        echo "could not resolve current wallpaper"
        exit 1
    fi
}
function delete() {
	echo deleting wallfile
	rm $WALLFILE
}
function unsetall() {
	echo clearing wallfile
	echo -n > $WALLFILE
}

function setwalls() {
	for wall in $@; do
		if grep -q $wall "$WALLFILE"; then
			echo $wall already set
		else
			echo setting $wall
			echo $wall >> $WALLFILE
		fi
	done
}

function runsetwall() {
	feh --bg-fill $1
    # wal --saturate 0.9 -i $1
}

function applywall() {
	echo applying wallpaper $1
	rm $WALLLINK
	ln -s $(pwd)/$1 $WALLLINK
	runsetwall $1
}

function reapply() {
	echo reapplying linked wallpaper
	# feh --bg-fill $WALLLINK
    runsetwall $WALLLINK
}

function help() {
	echo "Wallpaper mangement script"
	echo
	echo "Syntax: ${BINNAME} OPTIONS"
	echo "options:"
	echo "  -A, --apply [OPTIONS]           applies a random wallpaper"
	echo "  -S, --set FILES                 sets new wallpapers"
	echo "  -U, --unset [OPTIONS] [FILES]   unsets new wallpapers"
	echo "  -P, --preview FILE              temporarily sets a wallpaper"
	echo "  -h, --help                      this message"
	echo
	echo "unset options:"
	echo "  -a, --all                       clears wallconfig file"
	echo "  -d, --delete                    deleted wallconfig file"
	echo "  -c, --current                   unset currently applied wallpaper"
	echo
	echo "apply options:"
	echo "  -r, --reapply                   reapply last applied wallpaper"
}

function unknown() {
	echo "${BINNAME}: unknown option $1" 1>&2
	exit 1
}

if [[ $# -eq 0 ]]; then
	echo "${BINNAME}: no arguments given" 1>&2
	help
	exit 1
fi

mode="N"
pos=""

while [[ $# -gt 0 ]]; do
	keys="$1"
	# If $1 does is single - argument
	if [[ $1 == '-'* ]] && [[ ! $1 == '--'* ]] ; then
		# Remove first -
		keys=${keys#?}
		# Place space between each character
		keys=$(echo $keys | sed 's/./-& /g')
	fi

	for key in $keys; do
		case $mode in 
			U)
				case $key in
					-a|--all)
						unsetall
						exit 0
						;;
					-d|--delete)
						delete
						exit 0
						;;
                    -c|--current)
                        unsetcur
                        exit 0
                        ;;
					'-'*|'--'*)
						unknown $key
						;;
					*)
						pos="$pos $key"
						shift
						;;
				esac
				;;
			A)
				case $key in
					-r|--reapply)
						reapply
						exit 0
						;;
					'-'*|'--'*)
						unknown $key
						;;
					*)
						echo Unexpected argument
						help
						exit 0
						;;
				esac
				;;
			S)
				pos="$pos $key"
				shift
				;;

			*)
				case $key in
					-A|--apply)
						mode="A"
						shift
						;;
					-U|--unset)
						mode="U"
						shift
						;;
					-S|--set)
						mode="S"
						shift
						;;
					-P|--preview)
						applywall $2
						exit 0
						;;
					-h|--help)
						help
						shift
						exit 0
						;;
					*)
						unknown $key
						;;
				esac
				;;
		esac
	done
done

case $mode in
	U)
		unsetwalls $pos
		;;
	S)
		setwalls $pos
		;;
	A)
		applywall $(shuf -n 1 $WALLFILE)
		;;
esac