#!/bin/sh help() { cat << EOF ${0}: 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 ~/.config/wallpapers EOF } err() { for line in "${@}"; do echo "${line}" >&2 done exit 1 } run() { if ! ${1} > /dev/null 2>&1; then err "${2}" fi [ -n "${3}" ] && echo "${3}" exit 0 } if ! command -v xwallpaper > /dev/null 2>&1; then err "${0}: xwallpaper must be installed" fi if [ ${#} = 0 ]; then input="${HOME}/.config/wallpapers" elif [ ${#} != 1 ] && [ $# != 2 ]; then err "${0}: Invalid usage" \ "Try '$0 -h' for help." fi while getopts "hcd:" option; do case "${option}" in c) run "xwallpaper --clear" \ "${0}: Failed to clear wallpaper" ;; d) input="${OPTARG}" ;; h) help; exit 0 ;; *) err "Try '${0} -h' for help" ;; esac done shift $((OPTIND - 1)) [ ${#} != 0 ] && err "${0}: Invalid usage" "Try '${0} -h' for help." if [ -n "${input}" ]; then case $(file -b --mime-type "${input}") in image/*) image="${input}" ;; inode/directory) waldir="${input}" ;; *) err "${0}: Couldn't read given file" ;; esac fi if [ -n "${waldir}" ]; then image=$(find "${waldir}" -iregex '.*\.jpeg\|.*\.jpng\|.*\.png' 2>/dev/null \ | shuf -n 1 ) fi [ -z "${image}" ] && err "${0}: No image file found" run "xwallpaper --zoom ${image}" "${0}: Failed to set wallpaper" "${image}"