summaryrefslogtreecommitdiff
path: root/.local/bin/cwall
blob: 49c11de0aaadc44690ecc81ab43e04ed9bb3a673 (plain) (blame)
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
#!/bin/sh

bgpid="${XDG_RUNTIME_DIR:-${TMPDIR:-/tmp}}/swaybg.pid"
bgroot="${XDG_DATA_HOME:-$HOME/.local/share}"
bgimg="${bgroot}/wallpaper"

die()
{
	printf "\033[31;1merr: %b\033[0m\n" "${1:-no args for die()}" 1>&2
	exit "${2:-1}"
}

usage()
{
	cat <<- EOF
	Usage: ${0##*/} [wallpaper.img]
	A helper script for swaybg

	EOF
}

dep_check()
{
	# usage: cmd_check cmd_1 ...
	: "${1:?}"

	for dep; do
		command -v "$dep" 1>/dev/null ||
			die "${dep}: command not found" 127
	done

	unset dep
}

updatewall()
{
	: "${1:?}"

	rm -f "$bgimg"
	cp  "$1" "$bgimg"

	[ -s "$bgpid" ] &&
		kill "$(cat "$bgpid")"
	swaybg --mode fill --image "$bgimg" > /dev/null 2>&1 &
	echo $! > "$bgpid"
}

main()
{
	[ ! -r "$1" ] &&
		usage && exit 1;
	[ ! -d "$bgroot" ] &&
		mkdir -p "$bgroot"

	dep_check swaybg file

	case "$(file --brief --dereference --mime-type "$1")" in
	"image/"*)
		updatewall "$1"
		;;
	*)
		die "invalid image file"
		;;
	esac
}

main "$@"