#!/bin/bash

DIR="/home/doug/Pictures/rotate_background"

# Get full path
FULL_PATH=$(gsettings get org.gnome.desktop.background picture-uri)

# Clean off quotes.
FULL_PATH=$(echo $FULL_PATH | sed 's/.$//' | sed 's/^.//')

# Get base filename for comparison with ls output. 
FILE_NAME=$(basename $FULL_PATH)

# Find the next file after this using grep.
NEXT_LINE=$(ls $DIR/* | grep $FILE_NAME -A1 | tail -n 1)

NEW_FULL_PATH="file://$NEXT_LINE"

# If we were already on the last line, we're stuck.  So loop to the beginning.
if [ $NEW_FULL_PATH = $FULL_PATH ] 
then 
    NEXT_LINE=$(ls $DIR/* | head -n 1)
fi

# Now set that image!
gsettings set org.gnome.desktop.background picture-uri "file://$NEXT_LINE"