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

. "lib_common.sh"
. "lib_handle.sh"

help() {
cat << EOF
${progname}: 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 ~/.local/share/wallpapers
A symlink at ~/.cache/wallpapers/current is created pointing to the selected wallpaper.
EOF
exit 0
}

wallpaper_handle check_program
SYMLINK="${XDG_CACHE_HOME:-$HOME/.cache}/wallpaper/current"

[ "${#}" -eq 0 ] && input="${XDG_DATA_HOME:-$HOME/.local/share}/wallpapers"
[ "${#}" -gt 2 ] && invalid_use

while getopts "cd:h" option; do
	case "${option}" in
	c)
		rm -f "${SYMLINK}"
		run --reload-compositor wallpaper_handle clear
	;;
	d) input="${OPTARG}" ;;
	h) help ;;

	*) invalid_use -h ;;
	esac
done
shift $((OPTIND - 1))

[ "${#}" != 0 ] && invalid_use

if [ -n "${input}" ]; then
	if [ -d "${input}" ]; then
		waldir="${input}"
	elif [ -f "${input}" ]; then
		case "${input}" in
			*.jpg|*.JPG|*.jpeg|*.JPEG|*.png|*.PNG) image="${input}" ;;
			*) err "Unsupported file type" ;;
		esac
	else
		err "Couldn't read given file"
	fi
fi

if [ -d "$waldir" ]; then
	image=$(
		find "$waldir" \( -name '*.jpg' -o -name '*.jpeg' -o -name '*.png' \) 2>/dev/null |
			awk 'BEGIN{srand()} {a[NR]=$0} END{if(NR>0) print a[int(rand()*NR)+1]}'
		)
fi

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

wallpaper_handle set "${image}" && echo "${image}" && \
mkdir -p "$(dirname "${SYMLINK}")" && ln -sf "${image}" "${SYMLINK}"