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

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

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

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

err() {
	for line in "${@}"; do
		echo "${line}" >&2
	done
	exit 1
}

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

	[ -n "${3}" ] && echo "${3}"
	exit 0
}

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"
	;;
	d) input="${OPTARG}" ;;

	h) help; exit 0 ;;

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

	esac
done

shift $((OPTIND - 1))

[ ${#} != 0 ] && err "${0}: Invalid usage" "Try '${0} -h' for help."

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

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

	*) err "${0}: Couldn't read given file" ;;
	esac
fi

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

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

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