#!/bin/sh
# Copyright (C) 2005,2006 Krzysztof Kozlowski
# License: GNU General Public License version 2
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# version 2 as published by the Free Software Foundation.
#
# 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.
#
# Homepage: http://www.kozik.net.pl
#
# Ostatnia zmiana: 4.10.2006
#
# Change GNOME wallpaper to random one chosen from a directory
#
# Zmienne :
USER="kozik"
DIR="/home/${USER}/Spoko/Tapety/sentences" # Dir with wallpapers
GNOME_WALLPAPER_FILE="/home/${USER}/.gconf/desktop/gnome/background/%gconf.xml"
WALLPAPERS=`ls $DIR`
if [ $? -ne 0 ]; then
echo "Error reading directory $DIR"
exit 1
fi
MAX_WALLPAPER=`ls $DIR | wc -l`
if [ $? -ne 0 ]; then
echo "Error reading directory $DIR"
exit 1
fi
# Poszukiwanie zsh lub bash (shelle, ktore rozumieja skladnie "echo $RANDOM")
GOOD_SHELL=`which zsh 2> /dev/null`
if [ ! -f "GOOD_SHELL" ]; then
GOOD_SHELL=`which bash 2> /dev/null`
if [ ! -f "GOOD_SHELL" ]; then
if [ -f "/usr/local/bin/zsh" ]; then
GOOD_SHELL="/usr/local/bin/zsh"
elif [ -f "/usr/bin/zsh" ]; then
GOOD_SHELL="/usr/bin/zsh"
elif [ -f "/bin/zsh" ]; then
GOOD_SHELL="/bin/zsh"
elif [ -f "/usr/local/bin/bash" ]; then
GOOD_SHELL="/usr/local/bin/bash"
elif [ -f "/bin/bash" ]; then
GOOD_SHELL="/bin/bash"
else
echo "Nie znalazlem interesujacego shella potrafiacego generowac losowe liczby"
exit 3 ;
fi
fi
fi
# ############################################
# Get a random number :
TEXT="\$RANDOM"
number=`$GOOD_SHELL -c "echo $TEXT"`
# Truncate number to number of wallpapers in our directory :
let "number %= $MAX_WALLPAPER"
# ############################################
# Do the loop :
count=0
NEW_WALLPAPER=""
for i in $WALLPAPERS; do
count=`expr $count + 1`
if [ $count -ne $number ]; then
continue
else
NEW_WALLPAPER=$i
break
fi
done
# ############################################
# Replace text:
# WALLPAPER
# between:
#
# and
#
echo "Setting $NEW_WALLPAPER wallpaper..."
NEW_WALLPAPER_PATH=`echo "${DIR}/${NEW_WALLPAPER}"`
#
cat $GNOME_WALLPAPER_FILE | sed -e "//,/<\/entry>/ s_.*<\/stringvalue>_$NEW_WALLPAPER_PATH<\/stringvalue>_" > ${GNOME_WALLPAPER_FILE}.tmp
if [ $? -eq 0 ]; then
mv ${GNOME_WALLPAPER_FILE}.tmp $GNOME_WALLPAPER_FILE
exit 0
else
rm ${GNOME_WALLPAPER_FILE}.tmp
echo "Blad przy zmianie tapety"
exit 1
fi