summaryrefslogtreecommitdiff
path: root/scripts/swall
blob: 6d3b854537e25885dc22f9a8910c2d337c9dff08 (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
#!/bin/sh

# TODO add the option error checking with optind

err() {
	echo "${0}": "${1}"
	exit 1
}


help() {
cat << EOF
$0: Wrapper script to set wallpapers

options:
 -d [File]	Select a wallpaper or a directory
 -h 		Print this message and exit
 -c 		Remove the current wallpaper

NOTE: default wallpaper directory is ~/.config/wallpapers
EOF
}

waldir="${XDG_CONFIG_HOME:-$HOME}/.config/wallpapers"

while getopts "hcd:" option; do
	case "${option}" in
	h)
		help
		exit 0
	;;
	c)
		xwallpaper --clear > /dev/null 2>&1 \
		|| err "Couldn't clear wallpaper"
		exit 0
	;;
	d) buf="${OPTARG}" ;;
	*) err 'add -h for help' ;;
	esac
done


if [ -n "${buf}" ]; then
	case $(file -b --mime-type "${buf}") in
	image/*) image="${buf}" ;;
	inode/directory) waldir="${buf}" ;;
	*) err "Couldn't read given file" ;;
	esac
fi

if [ -z "${image}" ]; then
	image=$(find "${waldir}" -iregex '.*\.jpeg\|.*\.jpng\|.*\.png' 2>/dev/null \
	| shuf -n 1 2>/dev/null)
fi

xwallpaper --zoom "${image}" > /dev/null 2>&1 || err "Couldn't set wallpaper"

echo "${image}"