#!/bin/bash

# Copyright (c) 2000-2002 Timothy M. King (tmk@lordzork.com)
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the 
# Software is furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in 
# all copies or substantial portions of the Software. 
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL 
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
# DEALINGS IN THE SOFTWARE.

PATH=$PATH:/usr/bin:/usr/local/bin:/usr/X11R6/bin

img_apps="display xli xsetbg Esetroot qiv wmsetbg xv"

display_full_cmd="display -geometry 800x600 -window root"
display_tile_cmd="display -window root"
display_center_cmd="display -backdrop -window root"
display_default_cmd="$display_center_cmd"

Esetroot_full_cmd="Esetroot -scale"
Esetroot_tile_cmd="Esetroot"
Esetroot_center_cmd="Esetroot -c"
Esetroot_default_cmd="$Esetroot_center_cmd"

wmsetbg_full_cmd="wmsetbg -s -S"
wmsetbg_tile_cmd="wmsetbg -t"
wmsetbg_center_cmd="wmsetbg -e"
wmsetbg_default_cmd="$wmsetbg_center_cmd"

qiv_full_cmd="qiv --root_s"
qiv_tile_cmd="qiv --root_t"
qiv_center_cmd="qiv --root"
qiv_default_cmd="$qiv_center_cmd"

xv_full_cmd="xv -max -smooth -root -quit"
xv_tile_cmd="xv -root -quit"
xv_center_cmd="xv -rmode 5 -root -quit"
xv_default_cmd="$xv_center_cmd"

xli_full_cmd="xli -fullscreen -onroot -quiet"
xli_tile_cmd="xli -onroot -quiet"
xli_center_cmd="xli -center -onroot quiet"
xli_default_cmd="$xli_center_cmd"

xsetbg_full_cmd="xsetbg -fullscreen"
xsetbg_tile_cmd="xsetbg"
xsetbg_center_cmd="xsetbg -center"
xsetbg_default_cmd="$xsetbg_center_cmd"

##################################

me=`basename $0`
version=2.1
copyright="(c) 2000-$(date +%Y) by Timothy M. King (http://lordzork.com/)"
config=$HOME/.bsetbgrc
last_cmd_file=$HOME/.bsetbg_last_cmd
refresh_cmd=xrefresh
p=$me:

quit() 
{
	[ "$1" ] && rc=$1 && shift 1
	[ "$*" ] && echo -e $*
	exit ${rc:-0}
}

bool() {
	case $1 in
		[yY][eE][sS]|1|[yY]|[tT][rR][uU][eE]|[oO][nN]) : ;;
		*) return 1 ;;
	esac
}

check_exe_in_path() 
{
	if [ -z "$1" ]; then
    		return 1
	elif [ -x "$(which $1 2>/dev/null)" ]; then
    		return 0
	elif [ -x "$(type $1 2>/dev/null)" ]; then
    		return 0
	else
    		return 1
	fi
}

help_msg() 
{
	cat <<EOF
$me $version $copyright

  -center <file>           center an image on the desktop
  -tile <file>             tile an image on the desktop
  -full <file>             stretch an image to fill the desktop
  -exec <args> <file>      specify an external command to execute
  
  -app  <app>              specify the image application to use
  -post <string>           arguments to be passed to the post-command
  -debug                   prints commands without executing them
EOF

	# this is extremely lame, but probably more portable than grep -E   
	bsetroot_help=$(bsetroot -help  2>&1| grep -v "^bsetroot" | grep -v "^  -help")
	case $bsetroot_help in
   		BaseDisplay*)	echo ;;
		*-gradient*)	echo "$bsetroot_help"
	esac

	cat <<EOF
  -generate <string>       generate a config file
  -help                    this message
  -version                 output version information
EOF
	quit ${1:-0}
}

get_apps() 
{
	for a in $img_apps; do
    		if check_exe_in_path $a; then
			eval center_cmd=\$$a\_center_cmd
			eval full_cmd=\$$a\_full_cmd
			eval tile_cmd=\$$a\_tile_cmd
			eval default_cmd=\$$a\_default_cmd
			return 0
		else
			if [ "$not_found" ]; then
				not_f                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              