#!/bin/sh -x

uid="$1"
test -z "$uid" && uid="null"
name="dwm-popup-$uid"
id_f="/dev/shm/$name.id"

function start() {
	id=`xwit -current -print | cut -d: -f1`
	echo "*** start uid $uid id $id to $id_f ***"
	echo $id > $id_f
	$@;
	rm -f $id_f;
	exit
}

if [ "$2" == "term" ] ; then
	case $uid in
	a) start screen -R -S $name ;;
	s) start screen -R -S $name irssi ;;
	*) start bash ;;
	esac
fi


case $uid in
a) g="100x43+100-20"; f="neep-alt-iso8859-2-24" ;;
s) g="139x25+0+16"; f="neep-alt-iso8859-2-18" ;;
*) g="80x25+100+100"; f="neep-alt-iso8859-2-18" ; echo "*** default ***" ;;
esac

xterm=`which rxvt-2.7.10 || whixh rxvt`
term="$xterm -geometry $g -name $name -fn $f -rv +sb -n $name -ls -e $0 $uid term"
echo "*** using term: $term ***"

if [ ! -e $id_f ] ; then
	$term
else
	id=`cat $id_f`
	info=`xwininfo -id $id`
	echo "*** $id from $id_f = $info ***"
	test ! -z "$info" && (
		op="-unmap"
		echo $info | grep 'Map State: IsViewable' > /dev/null || op="-pop"
		xwit $op -id $id
	) || $term
fi
