#!/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}"