diff options
author | sinanmohd <sinan@firemail.cc> | 2023-05-10 23:12:18 +0530 |
---|---|---|
committer | sinanmohd <sinan@firemail.cc> | 2023-05-10 23:12:18 +0530 |
commit | b878cc923fe7132f667a73c19e8db628869ceb8d (patch) | |
tree | 370ef3984b34aaba9946d9a6497d3bd52abddd44 /.local/bin | |
parent | ef2f0f53c8f60e9329508fb89e195a3ba72075c9 (diff) |
cwall: initial commit
a helper script for swaybg
Diffstat (limited to '.local/bin')
-rwxr-xr-x | .local/bin/cwall | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/.local/bin/cwall b/.local/bin/cwall new file mode 100755 index 0000000..49c11de --- /dev/null +++ b/.local/bin/cwall @@ -0,0 +1,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 "$@" |