#!/bin/bash

# cut down wallpaper for non-kde desktops
# now handles spaced picture names Mon Jan 14 2002
# and reads better Fri Jun 14 2002
# for large original size pix try "chbg -mode center FILE"
# author: S.A. <stevenaaus@yahoo.com>

WALLPAPER=$HOME/.wallpaper
DEFAULT="Tiled /mnt/e/graphics/redhat.jpg"
DEFAULT_COL=thistle3
export DEFAULT_DIR="/mnt/e/graphics/"

function Usage () {
cat <<EOF

wallpaper [ -c  | -t  | -s   | -m | -x | -2 | -3 ]    Picture    [Color]
    ( centred tiled scaled maxpect max+tiled | 2x | 3x  file    background )
wallpaper Bitmap [-bg Color] [-fg Color]
wallpaper -{color|solid} Color
wallpaper -none | -?
wallpaper -r 	(refresh) 
wallpaper -p 	(refresh) 

EOF
}

function Error () {
	echo "Wallpaper: $1 $2"
	exit 1
}

function MakeItSo () {
	$*
	SaveWallpaper
	echo $* >$WALLPAPER
}
function SaveWallpaper () {
	cp $WALLPAPER ${WALLPAPER}.prev
}
	

function MakeWallpaper () {
	if [ "$2" == "" ] ; then
		# rescale the current wallpapers size
		set "$1" "`wallpaper  | sed -e 's/^[^"]*"//' | tr -d '"'`"
	fi
	[ ! -f "$2" ] && Error "No such file" \'$2\'

	case $1 in
		# xv -rmode options
		CentredMaxpect) SCALEOPTS="-rmode 5 -maxpect" ;;
		Centred) SCALEOPTS="-rmode 5" ;;
		2x) SCALEOPTS="-rmode 5 -expand 2" ;;
		3x) SCALEOPTS="-rmode 5 -expand 3" ;;
		Tiled)   SCALEOPTS="-rmode 0" ;;
		TiledMaxpect)   SCALEOPTS="-rmode 4 -maxpect" ;;
		Scaled)	 SCALEOPTS="-max" ;;
		*)       Error "Programming Error"
	esac

	PAPER=`fullname "$2"`
	if [ "$3" == "" ] ; then
		BGCOLOR=gray50
	else
		BGCOLOR="$3"
	fi
	COMMAND="xv -root $SCALEOPTS -quit -rbg '$BGCOLOR'" 
	# $COMMAND "$PAPER"
	# this works ok, but not for space embedded colours
	# so I use "wallpaper -r". go figure
	SaveWallpaper
        echo $COMMAND \"$PAPER\"> $WALLPAPER
 	wallpaper -r

}

####### m a i n #######

case "$1" in
    "")		cat $WALLPAPER ;;
    -r)		. $WALLPAPER || wallpaper -? ;;
    -p)		. ${WALLPAPER}.prev 
    		swap -q ${WALLPAPER}.prev ${WALLPAPER} ;;
    -\?)		# random is now the default
		file=$DEFAULT_DIR/$(
		  cd $DEFAULT_DIR
		  num=$(expr $RANDOM % `ls -1 */*.jpg */*.gif | wc -l` + 1)
		  ls -1 */*.jpg */*.gif | head -n $num | tail -n 1
		)
		# Picture is tiled if size < 15k
		if expr `ls -s $file | print_column 2` \< 15 ; then
			how=Tiled
			colour=""
		else
			how=Centred
			colour="$DEFAULT_COL"
		fi
		[ -n "$2" ] && colour="$2"
		MakeWallpaper $how $file $colour
		;;
 -none)		MakeItSo xsetroot -solid grey65 ;;
    -b)		MakeItSo xsetroot -solid black ;;
-color|\
-solid)		MakeItSo xsetroot -solid "$2" ;;
    -2)		MakeWallpaper 2x "$2" "$3" ;;
    -3)		MakeWallpaper 3x "$2" "$3" ;;
   -c*)		MakeWallpaper Centred "$2" "$3" ;;
   -t*)		MakeWallpaper Tiled "$2" "$3" ;;
   -x*)		MakeWallpaper TiledMaxpect "$2" "$3" ;;
   -s*)		MakeWallpaper Scaled "$2" "$3" ;;
   -m*)		MakeWallpaper CentredMaxpect "$2" "$3" ;;
*.xpm | *.bmp | *.tga | \
*.ppm | *.png | *.pcx | \
*.gif | *.jpeg | *.jpg )\
		MakeWallpaper Centred "$1" "$2" ;;
-h|-\?)		Usage ;;
    -*)		Error "Illegal option" ;;
     *)		if (test -f "$1") 
		then MakeItSo xsetroot -bitmap `fullname "$1"` "$2" "$3" "$4" "$5"
		else Error "No such file" \'$1\'
		fi
esac
