#!/bin/bash # Usage: setwp -h # Author: Skiqqy # This script allows the setting of a static wallpaper, or cycling through a set of wallpapers. # Variables set -B t=600 args=( "$@" ) usage () { cat << EOF Usage: setwp [options] [file|dir] h: Shows this message. d: Use the pictures in the specified directory, cycle with time t (default 10 min). t: Set time t to a specific value. z: Make feh zoom in on the wallpapers. f: Specify a single picture to use. p: Show the path to the picture in the bottem left corner of the picture. EOF } # Set a wallpaper with text in the bottem left corner # Usage swptext /path/to/picure "text" swptext () { rm -f /tmp/setwp_text.png convert "$1" -gravity SouthWest -annotate +1+3 "$2" /tmp/setwp_text.png if [ -z "$zoom" ] then opt=scale else opt=fill fi feh --bg-"$opt" /tmp/setwp_text.png } swp () { if [ -z "$zoom" ] then opt=scale else opt=fill fi feh --bg-"$opt" "$1" } # cycle /path/to/dir cycle () { d=${1%/} if [ $(echo "$t < 1" | bc) -eq 1 ] then echo "Invalid time used: t was '$t', now set to '600'" t=600 fi for ((;;)) do for file in $d/{*.png,*.jpg,*.jpeg} do if [ -z "$path" ] then swp "$file" else swptext "$file" "$(realpath "$file")" fi sleep $t if [ ! $? -eq 0 ] then echo "An error occured in cycle, did you set time to a valid number?" exit 1 fi done done } # Downloads and sets a wallpaper, provides an option to save permanently. # Usage: download "$url" download () { ftype=$(basename "$1" | cut -d\. -f 2) file=/tmp/"testwp.$ftype" curl -s "$1" > "$file" if ! swp "$file" > /dev/null 2>&1 then echo "An error occured, is this a valid picture link?" exit $? fi read -rp "Save? Y/N: " input [[ ! "$input" =~ ^(y|Y)(es)?$ ]] && exit # Exit if we not saving read -rp "Name (Empty for random name): " name [ -z "$name" ] && name=$(tr -dc 0-9A-Za-z < /dev/random | head -c 15) read -rep "Path (Empty for curr dir): " save [ -z "$save" ] && save=. save=$(realpath "${save%/}") if [ -f "$save/$name.$ftype" ] then read -rp "This file already exist! Proceed? Y/N: " input [[ "$input" =~ ^(n|N)(o)?$ ]] && echo "Save aborted." && exit 0 fi echo "Saving! $save/$name.$ftype" mv "$file" "$save/$name.$ftype" exit } main () { last=$((${#args[@]} - 1)) [ "$last" -eq -1 ] && usage && exit 1 file=${args["$last"]} zoom= path= while getopts "hd:t:zf:p" opt do case $opt in h) usage exit 0 ;; t) t="$OPTARG" ;; z) zoom=1 ;; f|d) file="$OPTARG" ;; p) path=1 ;; *) echo "Invalid arg" exit 1 ;; esac done [[ "$file" =~ ^http[s]?:// ]] && download "$file" # Check that it is a url [ -z "$file" ] && echo "Invalid file, see -h" && exit 1 if [ -d "$file" ] then cycle "$file" elif [ -f "$file" ] then if [ -z "$path" ] then swp "$file" else swptext "$file" "$(realpath "$file")" fi else echo " Please check -h" exit 1 fi } main "$@"