mass update.
[my-dotfiles.git] / bin / change_wallpaper.sh
diff --git a/bin/change_wallpaper.sh b/bin/change_wallpaper.sh
new file mode 100644 (file)
index 0000000..dd93b23
--- /dev/null
@@ -0,0 +1,117 @@
+#! /bin/bash
+
+PID=$$
+PIDFILE=${PIDFILE:-/tmp/wallpaper.pid}
+WAIT_CYCLE="5m"
+
+trap "rm -f $PIDFILE" SIGTERM
+
+WP_SETTER="/usr/bin/feh"
+QARMA="/usr/bin/qarma"
+
+function set_wp() {
+       NEW_WP=$1
+       $WP_SETTER --bg-fill $NEW_WP
+}
+
+# set background function (requires input)
+function wpapers() {
+       if [[ -f $1 ]]; then
+               # We have a single file as input
+               set_wp $1
+               exit 0
+       elif [[ -d $1 ]]; then
+               # directory as input
+               while true; do
+                       BGIMG=$(find $1 -type f -print | shuf -n1)
+                       set_wp ${BGIMG}
+                       sleep $WAIT_CYCLE
+               done
+       fi
+}
+
+function file_or_dir() {
+       if [[ -f /tmp/choice ]]; then
+               rm /tmp/choice
+       fi
+
+       FOD=$($QARMA --list --text="single image or directory?" --hide-header "file" "directory" > /tmp/choice)
+       case $? in
+               0 )
+                       file_chooser $(cat /tmp/choice)
+                       ;;
+               1 )
+                       $QARMA --info --title="Exiting!" --text="No wallpaper was changed."
+                       ;;
+               -1 )
+                       $QARMA --error --title="Ooops!!" --text="Something unexpected happened."
+                       ;;
+       esac
+}
+
+function file_chooser() {
+       case $1 in
+               "file" )
+                       FILE="$($QARMA --file-selection --title='Choose your Wallpaper')"
+                       case $? in
+                               0 )
+                                       wpapers $FILE
+                                       ;;
+                               1 )
+                                       $QARMA --info --title="Exiting!" --text="No wallpaper was changed."
+                                       ;;
+                               -1 )
+                                       $QARMA --error --title="Ooops!!" --text="Something unexpected happened."
+                       esac
+                       ;;
+               "directory" )
+                       FILE="$($QARMA --file-selection --directory --title='Choose your Wallpaper directory')"
+                       case $? in
+                               0 )
+                                       wpapers $FILE
+                                       ;;
+                               1 )
+                                       $QARMA --info --title="Exiting!" --text="No wallpaper was changed."
+                                       ;;
+                               -1 )
+                                       $QARMA --error --title="Ooops!!" --text="Something unexpected happened."
+                       esac
+                       ;;
+               * )
+                       $QARMA --error --title="Ooops!!" --text="Something unexpected happened."
+                       ;;
+       esac
+}
+
+function run() {
+       if [[ $(basename $0) == "wallpaper.sh" ]]; then
+               # we were called as wallpaper.sh, so simple wallpaper setter without qarma interaction
+               wpapers $1
+       elif [[ $(basename $0) == "change_wallpaper.sh" ]]; then
+               # we use qarma to set the wallpaper
+               $QARMA --question --title="Change Wallpaper" --text="Do you want to change the wallpaper?"
+               case $? in
+                       0 )
+                               file_or_dir
+                               ;;
+                       1 )
+                               $QARMA --info --title="Exiting!" --text="No wallpaper was changed."
+                               ;;
+                       -1 )
+                               $QARMA --error --title="Ooops!!" --text="Something unexpected happened."
+               esac
+       fi
+}
+
+if [[ -r $PIDFILE ]]; then
+       # PIDFILE exists, so I guess there's already an instance running
+       # let's kill it and run again
+       kill -s 15 $(cat $PIDFILE) > /dev/null 2>&1
+       # should already be deleted by trap, but just to be sure
+       rm $PIDFILE
+fi
+
+# create PIDFILE
+echo $PID > $PIDFILE
+
+run