#!/bin/sh

GTKDIALOG=gtkdialog

TMPDIR=/tmp/gtkdialog/examples/"`basename $0`"
mkdir -p "$TMPDIR"

# You may notice that the last line of this demo contains "qecho":
# the "q" is actually quitting "top". I could've just put "q" on
# a separate line but then it sends an unnecessary newline.
echo "clear
echo pid=\$$
whoami
hostname
pwd
ls --color=auto ~/
date
cal
arch
uname -a
/bin/sh --version
top
qecho restarting..." > "$TMPDIR"/demo
echo 0 > "$TMPDIR"/demoindex
echo 13 > "$TMPDIR"/democount
sed -n 1p "$TMPDIR"/demo > "$TMPDIR"/feed

MAIN_DIALOG='
<window title="Terminal Widget" resizable="true">
	<vbox>
		<timer interval="2" visible="false">
			<action>"read < '"$TMPDIR"'/demoindex; index=$REPLY
				read < '"$TMPDIR"'/democount; count=$REPLY
				index=$((index + 1))
				if [ $index -ge $count ]; then
					index=0
				fi
				line=$((index + 1))
				sed -n ${line}p '"$TMPDIR"'/demo > '"$TMPDIR"'/feed
				echo $index > '"$TMPDIR"'/demoindex
			"</action>
			<action>refresh:vte2</action>
		</timer>
		<notebook tab-labels="All Defaults|Colourful|Input File Feed Demo">
			<terminal has-focus="true">
				<variable>vte0</variable>
				<action>echo "vte0=$vte0 - Default child-exited signal"</action>
				<action signal="child-exited">echo "vte0=$vte0 - Resetting and re-executing command"</action>
				<action signal="child-exited">clear:vte0</action>
				<action signal="child-exited">echo "vte0=$vte0 - New PID"</action>
				<action signal="show">echo "vte0=$vte0 - show signal"</action>
			</terminal>
			<terminal argv0="/bin/sh"
				envv0="WELCOME=Welcome to the terminal (VteTerminal) widget :)"
				pointer-autohide="true"
				current-directory-uri="'$HOME'"
				cursor-background-color="#ff0"
				highlight-background-color="#660"
				text-background-color="#332"
				text-foreground-color="#fff"
				hscrollbar-policy="2"
				vscrollbar-policy="2">
				<variable>vte1</variable>
				<input>echo echo \$WELCOME</input>
				<action>echo "vte1=$vte1 - Default child-exited signal"</action>
				<action signal="child-exited">echo "vte1=$vte1 - Resetting and re-executing command"</action>
				<action signal="child-exited">clear:vte1</action>
				<action signal="child-exited">echo "vte1=$vte1 - New PID"</action>
				<action signal="child-exited">refresh:vte1</action>
				<action signal="show">echo "vte1=$vte1 - show signal"</action>
			</terminal>
			<terminal argv0="/bin/sh" sensitive="false"
				cursor-shape="2"
				background-transparent="true"
				background-saturation="0.2"
				background-tint-color="#223"
				text-foreground-color="#fff">
				<variable>vte2</variable>
				<input file>'"$TMPDIR"'/feed</input>
				<action signal="show">echo "vte2=$vte2 - show signal"</action>
			</terminal>
		</notebook>
		<hbox homogeneous="true" space-expand="false" space-fill="false">
			<button use-stock="true" label="gtk-quit">
			</button>
		</hbox>
	</vbox>
</window>
'
export MAIN_DIALOG

case $1 in
	-d | --dump) echo "$MAIN_DIALOG" ;;
	*) $GTKDIALOG --space-expand=true --space-fill=true --program=MAIN_DIALOG ;;
esac
