dotfiles/.local/bin/wallpaper
2024-07-03 19:59:32 -04:00

88 lines
1.9 KiB
Bash
Executable file

#!/bin/sh
LOCK_RESOLUTION="1920x1080"
LOCK_QUALITY=70
err() {
echo "$1" >&2
}
die() {
err "${0##*/}: $1"
exit 1
}
usage() {
if [ -n "$1" ]; then
err "${0##*/}: $1"
err
fi
err "usage: ${0##*/} [-hqr] [-e RESOLUTION] [-u QUALITY] IMAGE"
err
err " -h show this message and exit"
err " -q suppress output and notifications"
err " -r restore the previous image as your wallpaper"
err " -e <RESOLUTION> override lock screen RESOLUTION"
err " -u <QUALITY> override lock screen QUALITY"
exit 0
}
convert_lock() {
magick "$1" -resize "$LOCK_RESOLUTION"^ -gravity center -extent "$LOCK_RESOLUTION" -quality "$LOCK_QUALITY" "$XDG_CACHE_HOME/lock.png"
}
restore() {
[ -f "$XDG_DATA_HOME/wallpaper" ] || die "previous image not found"
previous="$(readlink -f "$XDG_DATA_HOME/wallpaper")"
case "${previous##*.}" in
png | jpg | jpeg) ;;
*) die "$previous: unsupported file type" ;;
esac
cmp -s "$previous" "$XDG_CACHE_HOME/lock.png" || convert_lock "$previous" &
xwallpaper --zoom "$XDG_DATA_HOME/wallpaper"
exit 0
}
while getopts ":hqre:u:" opt; do
case $opt in
h) usage ;;
q) QUIET=1 ;;
r) restore ;;
e) LOCK_RESOLUTION=$OPTARG ;;
u) LOCK_QUALITY=$OPTARG ;;
\?) usage "invalid option -- '$OPTARG'" ;;
:) usage "option requires an argument -- '$OPTARG'" ;;
esac
done
shift $((OPTIND - 1))
if [ "$1" = "-" ]; then
while read -r line; do
image=$line
done
else
image=$1
fi
[ -n "$image" ] || usage "IMAGE was not provided"
[ -d "$image" ] && die "$image: is a directory"
[ -f "$image" ] || die "$image: no such file"
case "${image##*.}" in
png | jpg | jpeg) ;;
*) die "$image: unsupported file type" ;;
esac
cmp -s "$image" "$XDG_CACHE_HOME/lock.png" || convert_lock "$image" &
ln -sf "$image" "$XDG_DATA_HOME/wallpaper"
xwallpaper --zoom "$XDG_DATA_HOME/wallpaper"
[ -z "$QUIET" ] && notify-send -u low "System" "Wallpaper set to <b>${image##*/}</b>."