#!/usr/bin/env zsh loglvl=() typeset -A loglvl loglvl[err]=3 loglvl[warning]=4 loglvl[notice]=5 loglvl[info]=6 loglvl[debug]=7 minlevel=$loglvl[${LOG_LEVEL:-info}] function writelog { level=$loglvl[$1] if ! (( minlevel > level )); then return fi printf "<%d>%s\n" 1 "$1" 2 "$2" } parserror=err function parse-config { writelog debug "Checking for config file $HOME/.config/wallpaper/config" if ! [[ -f $HOME/.config/wallpaper/config ]]; then writelog $parserror "Config does not exist, cannot operate." return 6 fi writelog debug "Loading config file" source $HOME/.config/wallpaper/config IFS=' ' writelog debug "Config file loaded, got COLOR='$COLOR' IMAGE='$IMAGE' MODE='$MODE'" args=() good_config=0 if (( ${+COLOR} )); then args+="--color=${COLOR}" good_config=1 writelog debug "Config is valid: COLOR is specified" fi if (( ${+IMAGE} )); then args+="--image=$IMAGE" good_config=1 writelog debug "Config is valid: IMAGE is specified" fi if (( ${+MODE} )); then args+="--mode=$MODE" fi if ! [[ $good_config == '1' ]]; then writelog $parserror "Missing COLOR or IMAGE, cannot set background." return 78 fi writelog debug "Arguments parsed, got args='$args'" } function reload { systemd-notify --reloading --status "Reloading config" parse-config if (( ? != 0 )); then writelog err "Reload failed" systemd-notify --ready --status "Config reload failed. Background not changed." return fi kill $swaybg_pid # now will return to the while loop where swaybg will be relaunched with new $args } function stop { writelog info "Received stop request, shutting down..." systemd-notify --stopping should_exit=1 kill $swaybg_pid } parse-config exit_code=$? parserror=warning if (( exit_code )); then exit $exit_code fi trap reload 1 trap stop 15 should_exit=0 while [[ $should_exit == 0 ]]; do writelog debug "Launching swaybg" swaybg --output='*' "$=args" & swaybg_pid=$! writelog debug "swaybg launched with pid $swaybg_pid" sleep 0.1 systemd-notify --ready --status "Wallpaper set" wait done