#! /bin/bash

# chat.sh
# multi-client chat application for RAPP

send_interface ()
{
	if [ "$1" = "" ]; then
		printf '%s: error! missing ID in send_interface()\n' "$0" 1>&2
	else
		printf '::BEGIN:%s\n' "$1"
		cat << EOF
<?xml version="1.0" encoding="iso-8859-1"?>
<xhpd version="1.0">
	<gadget name="chatapp" type="UWGWindow">
		<property name="filename" value="chatapp" />
		<property name="public" value="false" />
		<property name="geometry" value="0,0,512,384" />
		<property name="style" value="border" />
		<property name="position" value="user" />
		<property name="caption" value="RAPP chat" />
		<property name="icon-name" value="rapp-chat" />
		<property name="app-name" value="rappchat" />
		<property name="app-class" value="rappchat" />
		<property name="foreground" value="@foreground" />
		<property name="background" value="@background" />
		<property name="visible" value="1" />

		<gadget name="panel1" type="UWGPanel"><!--{{{-->
			<property name="public" value="false" />
			<property name="geometry" value="0,0,512,384" />
			<property name="layout" value="" />
			<property name="visible" value="1" />
			<property name="foreground" value="@foreground" />
			<property name="background" value="@background" />
			<property name="highlight" value="@highlight" />
			<property name="shadow" value="@shadow" />
			<property name="indent" value="1" />

			<gadget name="messages" type="UWGCMessageWin"><!--{{{-->
				<property name="public" value="false" />
				<property name="geometry" value="8,8,496,336" />
				<property name="layout" value="" />
				<property name="visible" value="1" />
				<property name="font" value="fixed" />
				<property name="foreground" value="white" />
				<property name="background" value="black" />
				<property name="highlight" value="@highlight" />
				<property name="shadow" value="@shadow" />
				
			</gadget><!--}}}-->
			<gadget name="usertext" type="UWGEdit"><!--{{{-->
				<property name="public" value="false" />
				<property name="geometry" value="8,352,400,24" />
				<property name="layout" value="" />
				<property name="visible" value="1" />
				<property name="enabled" value="1" />
				<property name="tab-stop" value="0" />
				<property name="text" value="" />
				<property name="font" value="@normal" />
				<property name="max-size" value="1024" />
				<property name="foreground" value="@foreground" />
				<property name="background" value="@tentry" />
				<property name="highlight" value="@highlight" />
				<property name="shadow" value="@shadow" />
				<property name="allow-type" value="1" />
				
				<action name="on_accept">
					<property name="text" />
					<update name="usertext">
						<property name="text" value="" />
					</update>
				</action>
			</gadget><!--}}}-->
			<gadget name="sendbtn" type="UWGButton"><!--{{{-->
				<property name="public" value="false" />
				<property name="geometry" value="416,352,88,24" />
				<property name="layout" value="" />
				<property name="visible" value="1" />
				<property name="enabled" value="1" />
				<property name="tab-stop" value="1" />
				<property name="caption" value="send" />
				<property name="font" value="@normal" />
				<property name="foreground" value="@foreground" />
				<property name="background" value="@background" />
				<property name="highlight" value="@highlight" />
				<property name="shadow" value="@shadow" />
				<property name="iecolor" value="@extra" />
				<property name="button-style" value="normal" />
				<property name="keybtn" value="0" />
				
				<action name="on_click">
					<property name="text" source="usertext" />
					<update name="usertext">
						<property name="text" value="" />
					</update>
				</action>
			</gadget><!--}}}-->
		</gadget><!--}}}-->

		<action name="on_delete">
			<command name="@termloop"><arg value="0" /></command>
		</action>
	</gadget>
</xhpd>

::END
EOF
	fi
}

printf 'hello world, this goes to standard error.\n' 1>&2
printf 'arguments were "%s" "%s"\n' "$0" "$1" 1>&2

ACTIVECLIENTS=""

while [ : ]; do
	read
	printf 'got: %s\n' "$REPLY" 1>&2

	case "$REPLY" in
	NEWCLIENT:*)
		SID=$(printf '%s' "$REPLY" | cut -d : -f 2-2)
		send_interface "$SID"
		if [ "$ACTIVECLIENTS" ]; then
			ACTIVECLIENTS="$ACTIVECLIENTS $SID"
		else
			ACTIVECLIENTS="$SID"
		fi
		printf '%s: added client [%s] to active sessions\n' "$0" "$SID" 1>&2
		;;
	OLDCLIENT:*)
		SID=$(printf '%s' "$REPLY" | cut -d : -f 2-2)
		TMP="$ACTIVECLIENTS"
		ACTIVECLIENTS=$(printf '%s' "$TMP" | sed -e 's/ '$SID'//g' -e 's/'$SID' //g')
		printf '%s: removed client [%s] from active sessions\n' "$0" "$SID" 1>&2
		;;
	*)
		XSID=$(printf '%s' "$REPLY" | cut -d : -f 1-1)
		SID=""
		for session in $ACTIVECLIENTS; do
			if [ "$session" = "$XSID" ]; then
				SID="$XSID"
				break;
			fi
		done
		if [ "$SID" = "" ]; then
			printf '%s: data from invalid session [%s]\n' "$0" "$XSID" 1>&2
		else
			TXTARG=$(printf '%s' "$REPLY" | sed -e 's/^[^ ]* usertext:text:"\([^"]*\)"$/\1/')
			if [ "$TXTARG" != "" ]; then
				for session in $ACTIVECLIENTS; do
					printf '::BEGIN:%s\n' "$session"
					printf '<command name="put_line" gadget="messages"><arg value="%s: %s" /></command>\n' "$SID" "$TXTARG"
					printf '::END\n'
				done
			fi
		fi
		;;
	esac
done



