#!/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() { echo "$*" >&2 exit 1 } run() { if ! ${1} > /dev/null 2>&1; then err "${2}" fi } 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" exit 0 ;; d) input="${OPTARG}" ;; h) help; exit 0 ;; *) err "Try '${0} -h' for help" ;; esac done if [ -n "${input}" ]; then case $(file -b --mime-type "${input}") in image/*) image="${input}" ;; inode/directory) waldir="${input}" ;; *) err "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" echo "${image}"