#!/bin/bash
# Set up a little bit first
FILE=background.xml
# Gonna need zenity; let's make sure we have it.
which zenity >/dev/null 2>&1
if [ $? != 0 ] ; then
echo 'Oh no! I couldn't find zenity! Install it and try again.'
exit 1
fi
# Get rid of special characters that will mess up the script and frighten small
# children.
if [ $(ls *[\`\~\!\@\#\$\%\^\&\*\(\)\{\}\[\]\;\"\'\<\>\\\?\ \ ]* 2>/dev/null | wc -l) -ne 0 ] ; then
ls *[\`\~\!\@\#\$\%\^\&\*\(\)\{\}\[\]\;\"\'\<\>\\\?\ \ ]* | zenity --title "Bad bad filenames - not good" --list --text='The following files have special characters\n(perhaps including spaces and tab characters)\nin their filenames. This will not fly.\nShould I try to fix them?' --hide-header --column="Files"
if [ $? = 0 ] ; then
for i in *[\`\~\!\@\#\$\%\^\&\*\(\)\{\}\[\]\;\"\'\<\>\\\?\ \ ]* ; do
mv -n "$i" $(echo $i | tr "\`\~\!\@\#\$\%\^\&\*\(\)\{\}\[\]\;\"\'\<\>\\\?\ \ " "__________________________")
done
else
exit
fi
fi
# All right, so we couldn't get rid of some; time to make the user do it his own self.
if [ $(ls *[\`\~\!\@\#\$\%\^\&\*\(\)\{\}\[\]\;\"\'\<\>\\\?\ \ ]* 2>/dev/null | wc -l) -ne 0 ] ; then
ls *[\`\~\!\@\#\$\%\^\&\*\(\)\{\}\[\]\;\"\'\<\>\\\?\ \ ]* | zenity --title "Oh dear - there's an error" --list --text='The following files have special characters\n(including spaces and tab characters) in their\nfilenames. This will not fly. Please change the\nfilenames of the listed files.' --hide-header --column="Files"
exit
fi
# How long does the user want each background displayed?
# First look for an old version of the XML file and get the default value out of it,
# if it exists
if [ -e $FILE ] ; then
INCR=$(grep '<duration>' $FILE | head -1 | awk -F\> '{print $2}' | awk -F\. '{print $1}')
INCR=$((INCR/60))
fi
if [ ! $INCR ] ; then
INCR=5
fi
# Now, we ask for a value.
INCR=$(zenity --scale --value=$INCR --min-value=1 --max-value=60 --title "I need guidance" --text "How long (in minutes) should each background be displayed?")
if [ $? != 0 ] ; then
exit
fi
# Convert minutes to seconds for the XML file
INCR="$((INCR * 60)).0"
# Randomize everything
NUM=$(ls *.[Jj][Pp][gG] *.[jJ][pP][eE][gG] *.[pP][nN][gG] *.[sS][vV][gG] 2>/dev/null | wc -l)
BADLIST="^$"
TMPFILE=$(mktemp)
exec 3>$TMPFILE
for (( IND=NUM ; IND >= 1 ; IND=IND-1 )) ; do
RANDINT=$(( (RANDOM / (32767 / IND)) + 1 ))
if [ $RANDINT -ge $IND ] ; then RANDINT=1 ; fi
PIC=$(echo $RANDINT | select i in $(ls *.[jJ][pP][gG] *.[Pp][nN][gG] *.[jJ][pP][eE][gG] *.[sS][vV][gG] | egrep -v "$BADLIST") ; do echo $i ; break ; done 2>/dev/null)
BADLIST="$BADLIST|^$PIC$"
echo $PIC >&3
#echo -e "# $PIC\n$(( 100 - ((100 * IND) / NUM) ))" #This is slow
echo "$(( 100 - ((100 * IND) / NUM) ))"
done |
zenity --progress --title "Please stand by" --text "Randomizing backgrounds..." --percentage=0 --auto-close
if [ $? != 0 ] ; then
exit
fi
exec 3<&-
# ---
# Now that that's out of the way, let's generate some XML!
# ---
# Miscellaneous information that goes at the beginning
cat >$FILE <<EOF
<background>
<starttime>
<year>2009</year>
<month>08</month>
<day>04</day>
<hour>00</hour>
<minute>00</minute>
<second>00</second>
</starttime>
<!-- This animation will start at midnight. -->
EOF
# And now we dump our randomized list in the XML file! (Nicely formatted, of course.)
PICLIST=$(cat $TMPFILE)
rm -f $TMPFILE
echo $PICLIST | tr "\ " "\n" | awk -vPWD=$(pwd) -vINCR=$INCR '{
print " <static>"
print " <duration>" INCR "</duration>"
print " <file>" PWD"/"$0 "</file>"
print " </static>"
}' >>$FILE
# Finish up. This is _important_.
echo '</background>' >>$FILE
# Should we set this as the background?
# Let's make sure it isn't already the background (because it's kinda dumb to ask to
# set it if it's already set).
SETTING=/desktop/gnome/background/picture_filename
if [ "$(gconftool --get $SETTING)" != "$(pwd)/$FILE" ] ; then
zenity --question --title "One last thing..." --text "Set this slideshow as your background?" --ok-label "_Set as background" --cancel-label "_Cancel"
if [ $? = 0 ] ; then
gconftool --set $SETTING "$(pwd)/$FILE" --type string
fi
fi