Advertisement
Guest User

background_update.sh

a guest
Jan 16th, 2011
897
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.17 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Set up a little bit first
  4. FILE=background.xml
  5. # Gonna need zenity; let's make sure we have it.
  6. which zenity >/dev/null 2>&1
  7. if [ $? != 0 ] ; then
  8. echo 'Oh no! I couldn't find zenity! Install it and try again.'
  9. exit 1
  10. fi
  11.  
  12. # Get rid of special characters that will mess up the script and frighten small
  13. # children.
  14. if [ $(ls *[\`\~\!\@\#\$\%\^\&\*\(\)\{\}\[\]\;\"\'\<\>\\\?\ \ ]* 2>/dev/null | wc -l) -ne 0 ] ; then
  15. 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"
  16. if [ $? = 0 ] ; then
  17. for i in *[\`\~\!\@\#\$\%\^\&\*\(\)\{\}\[\]\;\"\'\<\>\\\?\ \ ]* ; do
  18. mv -n "$i" $(echo $i | tr "\`\~\!\@\#\$\%\^\&\*\(\)\{\}\[\]\;\"\'\<\>\\\?\ \ " "__________________________")
  19. done
  20. else
  21. exit
  22. fi
  23. fi
  24. # All right, so we couldn't get rid of some; time to make the user do it his own self.
  25. if [ $(ls *[\`\~\!\@\#\$\%\^\&\*\(\)\{\}\[\]\;\"\'\<\>\\\?\ \ ]* 2>/dev/null | wc -l) -ne 0 ] ; then
  26. 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"
  27. exit
  28. fi
  29.  
  30. # How long does the user want each background displayed?
  31. # First look for an old version of the XML file and get the default value out of it,
  32. # if it exists
  33. if [ -e $FILE ] ; then
  34. INCR=$(grep '<duration>' $FILE | head -1 | awk -F\> '{print $2}' | awk -F\. '{print $1}')
  35. INCR=$((INCR/60))
  36. fi
  37. if [ ! $INCR ] ; then
  38. INCR=5
  39. fi
  40. # Now, we ask for a value.
  41. 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?")
  42. if [ $? != 0 ] ; then
  43. exit
  44. fi
  45. # Convert minutes to seconds for the XML file
  46. INCR="$((INCR * 60)).0"
  47.  
  48. # Randomize everything
  49. NUM=$(ls *.[Jj][Pp][gG] *.[jJ][pP][eE][gG] *.[pP][nN][gG] *.[sS][vV][gG] 2>/dev/null | wc -l)
  50. BADLIST="^$"
  51. TMPFILE=$(mktemp)
  52. exec 3>$TMPFILE
  53. for (( IND=NUM ; IND >= 1 ; IND=IND-1 )) ; do
  54. RANDINT=$(( (RANDOM / (32767 / IND)) + 1 ))
  55. if [ $RANDINT -ge $IND ] ; then RANDINT=1 ; fi
  56. 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)
  57. BADLIST="$BADLIST|^$PIC$"
  58. echo $PIC >&3
  59. #echo -e "# $PIC\n$(( 100 - ((100 * IND) / NUM) ))" #This is slow
  60. echo "$(( 100 - ((100 * IND) / NUM) ))"
  61. done |
  62. zenity --progress --title "Please stand by" --text "Randomizing backgrounds..." --percentage=0 --auto-close
  63. if [ $? != 0 ] ; then
  64. exit
  65. fi
  66. exec 3<&-
  67.  
  68. # ---
  69. # Now that that's out of the way, let's generate some XML!
  70. # ---
  71.  
  72. # Miscellaneous information that goes at the beginning
  73. cat >$FILE <<EOF
  74. <background>
  75. <starttime>
  76. <year>2009</year>
  77. <month>08</month>
  78. <day>04</day>
  79. <hour>00</hour>
  80. <minute>00</minute>
  81. <second>00</second>
  82. </starttime>
  83. <!-- This animation will start at midnight. -->
  84. EOF
  85.  
  86. # And now we dump our randomized list in the XML file! (Nicely formatted, of course.)
  87. PICLIST=$(cat $TMPFILE)
  88. rm -f $TMPFILE
  89. echo $PICLIST | tr "\ " "\n" | awk -vPWD=$(pwd) -vINCR=$INCR '{
  90. print " <static>"
  91. print " <duration>" INCR "</duration>"
  92. print " <file>" PWD"/"$0 "</file>"
  93. print " </static>"
  94. }' >>$FILE
  95.  
  96. # Finish up. This is _important_.
  97. echo '</background>' >>$FILE
  98.  
  99. # Should we set this as the background?
  100. # Let's make sure it isn't already the background (because it's kinda dumb to ask to
  101. # set it if it's already set).
  102. SETTING=/desktop/gnome/background/picture_filename
  103. if [ "$(gconftool --get $SETTING)" != "$(pwd)/$FILE" ] ; then
  104. zenity --question --title "One last thing..." --text "Set this slideshow as your background?" --ok-label "_Set as background" --cancel-label "_Cancel"
  105. if [ $? = 0 ] ; then
  106. gconftool --set $SETTING "$(pwd)/$FILE" --type string
  107. fi
  108. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement