#!/bin/sh
# 
# Copyright (C) 2007 Moritz Orbach <zufall@apfelboymchen.homeunix.net>
# 
# Zufall 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 3 of the License, or
# (at your option) any later version.
# 
# Zufall 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.
# 

# i hope all this stuff is portable.

GCONFTOOL="/usr/bin/gconftool-2"

if [ -z "$GCONFTOOL" ]; then
  echo "$0: gconftool could not be found" 1>&2
  exit 1
fi

# because of the quotation marks there's always one parameter
# -> would give the wrong error message
if [ -z "$@" ]; then
  echo "$0: please specify a directory or image" 1>&2
  exit 2
fi

exit=0
# send signals to childs
trap ':'  1
trap ':' 10
trap 'exit=1' 15 2

# gnome-control-center, preferences.h
# define BG_PREFERENCES_DRAW_BACKGROUND    "/desktop/gnome/background/draw_background"
# define BG_PREFERENCES_PRIMARY_COLOR      "/desktop/gnome/background/primary_color"
# define BG_PREFERENCES_SECONDARY_COLOR    "/desktop/gnome/background/secondary_color"
# define BG_PREFERENCES_COLOR_SHADING_TYPE "/desktop/gnome/background/color_shading_type"
# define BG_PREFERENCES_PICTURE_OPTIONS    "/desktop/gnome/background/picture_options"
# define BG_PREFERENCES_PICTURE_OPACITY    "/desktop/gnome/background/picture_opacity"
# define BG_PREFERENCES_PICTURE_FILENAME   "/desktop/gnome/background/picture_filename"
zufall -n -c "$GCONFTOOL -s /desktop/gnome/background/picture_filename -t string \"%i\"; $GCONFTOOL -s /desktop/gnome/background/primary_color -t string \"%c\"" "$@" & pid=$!

# the wait gets interrupted when a signal arrives. 
while test $exit -eq 0; do
        wait $pid >/dev/null 2>&1 || exit=1
done

kill -15 $pid >/dev/null 2>&1
