#!/bin/sh
#Copyright (c) 2006, Zane C. Bowers
#All rights reserved.
#
#Redistribution and use in source and binary forms, with or without modification,
#are permitted provided that the following conditions are met:
#
#   * Redistributions of source code must retain the above copyright notice,
#    this list of conditions and the following disclaimer.
#   * Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
#
#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
#ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
#WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
#IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
#INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
#BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
#DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
#LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
#THE POSSIBILITY OF SUCH DAMAGE.

#print this if not sure what else to do or bad args
usage(){
    echo roxbg version 1.0.0 by Zane C. Bowers
    echo
    echo -f	fill the screen with the image
    echo -s	scale the image with out stretching it
    echo -c	center image and do not scale
    echo -t	tile the image
    echo 
    echo default: -s
    echo
    echo -h	display this message
    echo
    echo example:
    echo  roxbg -f /arc/pics/foo/bar.jpg
}

#get the options
while getopts fscth OPTION ; do
    case "$OPTION" in
        f) style="stretch" ;;
        s) style="scale" ;;
	c) style="centre" ;;
	t) style="tile" ;;
	h) usage=true ;;
       \?) usage=true ;;
    esac
done

#if usage is not empty, print the usage massage and exit
if [ ! -z $usage ]; then
    usage
    exit 1
fi

#gets the arguement that represents the file
#and set the style if none was specified
filearg="2"
if [ -z $style ]; then
    style="scale"
    filearg="1"
fi

#gets the file if no style was specified
if [ $filearg -eq 1 ]; then
    file=$1
fi

#gets the file if a style was specified
if [ $filearg -eq 2 ]; then
    file=$2
fi

#finally set the bg
echo "<?xml version=\"1.0\"?>\
    <env:Envelope xmlns:env=\"http://www.w3.org/2001/12/soap-envelope\">\
	<env:Body xmlns=\"http://rox.sourceforge.net/SOAP/ROX-Filer\">\
	    <SetBackdrop>\
		<Filename>"$file"</Filename>\
		<Style>"$style"</Style>\
	    </SetBackdrop>\
	</env:Body>\
    </env:Envelope>" | rox --RPC
