#! /bin/sh
#
# Copyright (c) 2019 Marc Espie <espie@openbsd.org>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

# this uses xwallpaper and random_run from ports
set -e

exit=false
PATH=${LOCALBASE}/bin:$PATH
if [ ! -x ${LOCALBASE}/bin/rr ]
then
	echo 1>&2 "You must pkg_add random_run for this to work"
	exit=true
fi
if [ ! -x ${LOCALBASE}/bin/xwallpaper ]
then
	echo 1>&2 "You must pkg_add xwallpaper for this to work"
	exit=true
fi

if $exit
then
	exit 1
fi
cd ${TRUEPREFIX}

xrandr|grep -w connected|sed -e 's/primary//'|  {
while read display connected res right rest;
do
    case "$right" in
    left|right)
	pic=`rr -1rp ${PPICS}/*`
	;;
    *)
	pic=`rr -1rp ${LPICS}/*`
	;;
    esac
    pics="$pics $pic"
    args="$args --output $display --maximize $pic"
done
eval xwallpaper $args
}
