#!/bin/sh # wpPointer.sh: Set next or previous wallpaper in wallpaper history or set and add one to it # # Usage: wpPointer.sh [-n|p] [-o] FILE... # # -n sets next wallpaper if it exists # -p sets previous wallpaper if it exists # -o opens recorded wallpapers in sxiv usageMessage="Usage: ${0} [-n|p] [-o] FILE..." wpDatFile="$CONFIG_WPLIST" wpPointerFile="$CONFIG_WPPOINTER" recordedMax="30" nextOrPrev="" next="n" prev="p" open="" error() { printf '%s error: %s\n' "$0" "$*" >&2 notify-send "${0} error" "$*" } errorAndUsage() { printf '%s error: %s\n%s\n' "$0" "$*" "$usageMessage" >&2 notify-send "${0} error" "${*}\n\n${usageMessage}" } getRecordedAmount() { wc -l "$wpDatFile" | cut -d' ' -f1 } setWP() { wp="$1" feh --bg-max "$wp" ln -sf "$wp" "${HOME}/wp" } setWpByPointer() { pointer="$1" wp="$(sed -n "${pointer}p" "$wpDatFile")" echo "$pointer" >"$wpPointerFile" if ! setWP "$wp"; then error "Unable to set ${wp} as wallpaper." fi } setNextOrPrev() { nextOrPrev="$1" if ! [ -f "$wpDatFile" ]; then error "${wpDatFile} does not exist or is not a regular file. Maybe no wallpapers were registered yet." exit 2 fi if ! [ -s "$wpDatFile" ]; then error "${wpDatFile} is empty." exit 2 fi datFileLength="$(getRecordedAmount)" if [ -f "$wpPointerFile" ]; then pointer="$(cat "$wpPointerFile")" else pointer="$datFileLength" fi if [ "$nextOrPrev" = "$next" ]; then if [ "$pointer" -eq "$datFileLength" ]; then error 'Last recorded wallpaper already selected.' exit 3 fi pointer=$((pointer+1)) setWpByPointer "$pointer" fi if [ "$nextOrPrev" = "$prev" ]; then if [ "$pointer" -eq "1" ]; then error 'First recorded wallpaper already selected.' exit 3 fi pointer=$((pointer-1)) setWpByPointer "$pointer" fi } addWp() { wp="$1" if ! [ -f "$1" ]; then error "File ${1} does not exist." exit 4 fi if ! setWP "$wp"; then error "Unable to set ${wp} as wallpaper." exit 4 fi wpPath="$(readlink -f "$wp")" # Double \ for explicit \ to reach variable! wpPathSanitized="$(echo "$wpPath" | sed 's|/|\\/|g')" # Delete existing references to the wallpaper to avoid repetition in history sed -i "/^${wpPathSanitized}$/d" "$wpDatFile" echo "$wpPath" >>"$wpDatFile" [ "$(getRecordedAmount)" -gt "$recordedMax" ] && sed -i "1d" "$wpDatFile" getRecordedAmount >"$wpPointerFile" } # Check pointer file makes sense if [ -f "$wpPointerFile" ]; then pointer="$(cat "$wpPointerFile")" pointerAsNumber="$(echo "$pointer" | sed 1q | grep -E '[0-9]+')" if [ "$pointer" != "$pointerAsNumber" ]; then rm "$wpPointerFile" else if [ "$pointer" -lt 1 ]; then echo "1" >"$wpPointerFile" fi recordedAmount=$(getRecordedAmount) if [ "$pointer" -gt "$recordedAmount" ]; then echo "$recordedAmount" >"$wpPointerFile" fi fi fi # Process options while getopts ':npo' opt; do case $opt in 'n' ) if [ -n "$nextOrPrev" ]; then errorAndUsage 'Tried to use -n and -p at the same time.' exit 1 fi setNextOrPrev "$next" ;; 'p' ) if [ -n "$nextOrPrev" ]; then errorAndUsage 'Tried to use -n and -p at the same time.' exit 1 fi setNextOrPrev "$prev" ;; 'o' ) open="y" ;; '?' ) printf '%s\n' "$usageMessage" exit 1 esac done shift $((OPTIND - 1)) # Set and add arguments to wallpaper history for wp in "$@"; do addWp "$wp" done [ -n "$open" ] && sxiv -b -t -n "$(getRecordedAmount)" - <"$wpDatFile"