#!/bin/bash
#
# Random GNOME background changer
#
# change-background.sh
# Version 0.5
#
# XMLStarlet tool needed to run  
# (see http://xmlstar.sourceforge.net/) 
#
# Copyright (c) 2005 Geronimo Orozco <patux@glo.org.mx>
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2, or (at your option)
#   any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software Foundation,
#   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#

export PATH=$PATH:/usr/local/bin:/usr/bin/X11:/opt/gnome/bin
GCT=`which gconftool-2 2>/dev/null`
XML=`which xmlstarlet 2>/dev/null`
if [ -z $XML ]; then
# Some old versions of xmlstartlet called xml to the binary file
    XML=`which xml 2>/dev/null`
fi

if [ -z $XML ]; then
    echo "XMLStarlet tool needed to run"
    echo "(see http://xmlstar.sourceforge.net/)"
    exit 1
fi
select_wallpapers(){
    $XML sel -T -t -m wallpapers/wallpaper  -v "@deleted" -o "|" -v filename \
    -o "|" -v options -o "|" -v shade_type -o "|" -v pcolor -o "|" -v scolor \
    -n  $HOME/.gnome2/backgrounds.xml | grep false 
}

counter=0;
for i in `select_wallpapers`
do 
    wallpaper[$counter]=$i;
    counter=$((counter + 1))
done
selected_wallpaper=${wallpaper[$((RANDOM%counter))]}
picture_filename=`echo $selected_wallpaper | cut -d"|" -f2`
picture_options=`echo $selected_wallpaper | cut -d"|" -f3`
color_shading_type=`echo $selected_wallpaper | cut -d"|" -f4`
primary_color=`echo $selected_wallpaper | cut -d"|" -f5`
secondary_color=`echo $selected_wallpaper | cut -d"|" -f6`
$GCT -t str --set /desktop/gnome/background/picture_options "$picture_options"
$GCT -t str --set /desktop/gnome/background/color_shading_type "$color_shading_type"
$GCT -t str --set /desktop/gnome/background/primary_color "$primary_color"
$GCT -t str --set /desktop/gnome/background/secondary_color "$secondary_color"
$GCT -t str --set /desktop/gnome/background/picture_filename "$picture_filename"

