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

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
}

err() {
	echo "$*" >&2
	exit 1
}

run() {
	if ! ${1} > /dev/null 2>&1; then
		err "${2}"
	fi
}

if ! command -v xwallpaper > /dev/null 2>&1; then
	err "${0}: xwallpaper must be installed"
fi

if [ $# != 0 ] && [ $# != 1 ] && [ $# != 2 ]; then
	err "${0}: Invalid usage
Try '$0 -h' for help."
fi


waldir="${HOME}"/.config/wallpapers

while getopts "hcd:" option; do
	case "${option}" in
	c)
		run "xwallpaper --clear" \
		"${0}: Failed to clear wallpaper"
		exit 0
	;;
	d) buf="${OPTARG}" ;;

	h) help; exit 0 ;;

	*) err "Try '${0} -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 )
fi

run "xwallpaper --zoom ${image}" \
"${0}: Failed to set wallpaper"

echo "${image}"