summaryrefslogtreecommitdiff
path: root/scripts/swall
blob: 20e4ea9df38f3d93c10502a883a0b8ce2c5fb721 (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
#!/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 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 ]; then
	input="${HOME}/.config/wallpapers"
elif [ $# != 1 ] && [ $# != 2 ]; then
	err "${0}: Invalid usage
Try '$0 -h' for help."
fi

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

	h) help; exit 0 ;;

	*) err "Try '${0} -h' for help" ;;
	esac
done


if [ -n "${input}" ]; then
	case $(file -b --mime-type "${input}") in
	image/*) image="${input}" ;;

	inode/directory) waldir="${input}" ;;

	*) err "Couldn't read given file" ;;
	esac
fi

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

[ -z "${image}" ] && err "${0}: No image file found"

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

echo "${image}"