#!/bin/bash
#
# setup-background
# by Lars Wirzenius
# "@(#):$Id$"
#
# Set the X root window to what the user prefers:
#
#	if ~/.fvwm2/background.xpm exists, do xpmroot for it,
#	else if ~/.fvwm2/background.{gif,jpg} exists, do xsetbg for it,
#	else if ~/.fvwm2/background.color exists, do xsetroot -solid for it,
#	else do the same tests for /etc/X11/fvwm2
#

xpmfile="$HOME/.fvwm2/background.xpm"
giffile="$HOME/.fvwm2/background.gif"
jpgfile="$HOME/.fvwm2/background.jpg"
colorfile="$HOME/.fvwm2/background.color"
listfile="$HOME/.fvwm2/background.list"

available()
{
  type -path $1 1>/dev/null 2>&1
}

randline()
{
  filename=$1

  set `wc -l $filename`
  lines=$1

  line=$[ ( $RANDOM % $lines ) + 1 ]

  head -$line $filename | tail -1
}

if available xsetbg; then
  viewer=xsetbg
elif available xv; then
  viewer="xv -root -quit"
else
  viewer="exit 1;"
fi

[ -s $xpmfile ] && xpmroot $xpmfile && exit 0
[ -s $giffile ] && $viewer $giffile && exit 0
[ -s $jpgfile ] && $viewer $jpgfile && exit 0
[ -s $colorfile ] && xsetroot -solid `cat $colorfile` && exit 0
[ -s $listfile ] && $viewer `randline $listfile` && exit 0

xpmfile="/etc/X11/fvwm2/background.xpm"
giffile="/etc/X11/fvwm2/background.gif"
jpgfile="/etc/X11/fvwm2/background.jpg"
colorfile="/etc/X11/fvwm2/background.color"
listfile="/etc/X11/fvwm2/background.list"

[ -s $xpmfile ] && xpmroot $xpmfile && exit 0
[ -s $giffile ] && $viewer $giffile && exit 0
[ -s $jpgfile ] && $viewer $jpgfile && exit 0
[ -s $colorfile ] && xsetroot -solid `cat $colorfile` && exit 0
[ -s $listfile ] && $viewer `randline $listfile` && exit 0
