#!/bin/sh
#
# Nico Schottelius <nico@schottelius.(net|org)>
# Date: 30-Jul-2003
# Last Modified: -
#
# generates a random background
# use `find imagedir > ~/.bgimages" and then run this script
#

FILE="$HOME/.bgimages"
CUR_FILE="$HOME/.bgimages-current"
TMP="/tmp/`basename $0`.$USER.$$"
IMG="/NONEXISTEN"
MAKEBG="wmsetbg -a -b black"

while [ ! -f "$IMG" -o -z "$IMG" ]; do
   NUM=`cat $FILE | wc | awk '{ print $1 }'`
   NUM="$[$NUM+1]" # random uses -1
   TAKETHIS=`echo $(($RANDOM % $NUM))`
   IMG=`head -n$TAKETHIS "$FILE" | tail -n1`
   echo "$IMG" > ~/.bgimages-current
   cat $FILE | grep -v "^$IMG\$" > $TMP
   mv $TMP $FILE
done

$MAKEBG $IMG