#!/bin/sh -
umask 077

# Haven't got this entirely safely working on non-primary displays; and
#   xrsh's won't appear in nested display :-(
# 2018-06-27 With Ubuntu 18.04, it seems primary DISPLAYs may be :1.0, so
#   let's try excepting that, too; see also etc/xinitrc
case "$DISPLAY,$XNEST_DISPLAY" in
*:[01],|*:[01].*,)	xrsh=xrsh;;
*)			xrsh='echo SKIPPING:';;
esac

host=${HOST-${HOSTNAME-`(hostname) 2> /dev/null`}}
case "$host" in
'')     host=`uname -n`;;
esac

: dflt	local;		XLOAD=;		  XLOAD2=;	    XLOAD3=
	XLOADM0=;	XLOADM=;	  XLOADM2=;	    XLOADM3=
	XOSVWM0=;			  XOSVWM2=
case "$host" in
[uv]kinzler*)		XLOAD=$MAILHOST;  XLOAD2=;	    XLOAD3=
	XLOADM0=-once;	XLOADM=-once;	  XLOADM2=;	    XLOADM3=
	XOSVWM0=-third;			  XOSVWM2=;;
#[uv]kinzler*)		XLOAD=$MAILHOST2; XLOAD2=$MAILHOST; XLOAD3=
#	XLOADM0=-third;	XLOADM=-third;	  XLOADM2=-tall;    XLOADM3=
#	XOSVWM0=-third;			  XOSVWM2=-tall;;
athanor*)		XLOAD=$MAILHOST3; XLOAD2=$MAILHOST; XLOAD3=
	XLOADM0=-third;	XLOADM=-third;	  XLOADM2=-tall;    XLOADM3=
	XOSVWM0=-third;			  XOSVWM2=-tall;;
aludel*)
	XOSVWM0=;	XLOAD=;		  XLOAD2=;	    XLOAD3=
	XLOADM0=-twice;	XLOADM=-twice;	  XLOADM2=;	    XLOADM3=;;
#[dp]kinzler*)
##	XOSVWM0=;	XLOAD=alamode;	  XLOAD2=alembic;   XLOAD3=
##	XLOADM0=-half;	XLOADM=-half;	  XLOADM2=;	    XLOADM3=;;
#	XOSVWM0=;	XLOAD=$SH;	  XLOAD2=;	    XLOAD3=
#	XLOADM0=-twice;	XLOADM=-twice;	  XLOADM2=;	    XLOADM3=;;
#alembic*)
#	XOSVWM0=-third;	XLOAD=alamode;	  XLOAD2=trendy;    XLOAD3=transi2
#	XLOADM0=-third;	XLOADM=-third;	  XLOADM2=-half;    XLOADM3=-half;;
#alamode*)
#	XOSVWM0=;	XLOAD=alembic;	  XLOAD2=trendy;    XLOAD3=
#	XLOADM0=-half;	XLOADM=-half;	  XLOADM2=;	    XLOADM3=;;
#alamoff*|trendy*|transit*)
#	XOSVWM0=-third;	XLOAD=alamode;	  XLOAD2=alembic;   XLOAD3=
#	XLOADM0=-third;	XLOADM=-third;	  XLOADM2=-half;    XLOADM3=;;
esac
case "$host" in
aludel*)	XOSVWM0=' +battery';;
esac

# xloaddash - start all or absent placed xload and xosview clients
# Steve Kinzler, steve@kinzler.com, May 12
# https://kinzler.com/me/home.html#x11

# Formerly used in .twmrc:
# "g. Load BR"	 !"xload -name xload-local$XLOADM0 &"
# "h. Load TR"	 !"xrsh ${XLOAD-localhost}  xload -name xload$XLOADM &"
# "i. Load TL"	 !"xrsh ${XLOAD2-localhost} xload -name xload-other$XLOADM2 &"
# "j. Load BL"	 !"xrsh ${XLOAD3-localhost} xload -name xload-other2$XLOADM3 &"
# "k. Osview RR" !"xosview -name xosview-local$XOSVWM0 &"
# "l. Osview RL" !"xrsh ${XLOAD-localhost} xosview -name xosview-dflt &"
# "m. Osview TL" !"xrsh ${XLOAD2-localhost} xosview -name xosview-other &"
# "n. Osview BL" !"xrsh ${XLOAD3-localhost} xosview -name xosview-other2 &"

tmp=/tmp/xld$$
trap "rm -f $tmp; exit" 0 1 2 13 15

case "$1" in
-h)	echo "usage: $0 [ -all ]" 1>&2; exit 1;;
-all)	:		    > $tmp;;
*)	xwininfo -root -all > $tmp;;
esac

grep -s \"xload-local$XLOADM0\" $tmp > /dev/null ||
  { xload -name xload-local$XLOADM0 & }
if test -n "$XLOADM0"
then
	grep -s \"xosview-local$XOSVWM0\" $tmp > /dev/null ||
	  { xosview -name xosview-local$XOSVWM0 & }
fi

grep -s \"xload$XLOADM\" $tmp > /dev/null ||
  { $xrsh $XLOAD xload -name xload$XLOADM & }
if test "$XLOADM" = -third -o "$XLOADM" = -once
then
	grep -s \"xosview-dflt\" $tmp > /dev/null ||
	  { $xrsh $XLOAD xosview -name xosview-dflt & }
fi

#if (temp -n) > /dev/null 2>&1
#then
#	temp -x -name xtherm-placed &
#else
	grep -s \"xload-other$XLOADM2\" $tmp > /dev/null ||
	  { $xrsh $XLOAD2 xload -name xload-other$XLOADM2 & }
	if test -n "$XLOADM2"
	then
		grep -s \"xosview-other$XOSVWM2\" $tmp > /dev/null ||
		  { $xrsh $XLOAD2 xosview -name xosview-other$XOSVWM2 & }
	fi

	if test -n "$XLOAD3"
	then
		grep -s \"xload-other2$XLOADM3\" $tmp > /dev/null ||
		  { $xrsh $XLOAD3 xload -name xload-other2$XLOADM3 & }
		if test -n "$XLOADM3"
		then
			grep -s \"xosview-other2\" $tmp > /dev/null ||
			  { $xrsh $XLOAD3 xosview -name xosview-other2 & }
		fi
	fi
#fi
