#!/bin/bash

# Capture chat and send it to stdout
# Typical usage: gchat 2> $GTERM_SOCKET 0<&2 | gfeed > $GTERM_SOCKET &
# The stderr is piped to the graphterm socket and stdin is also read from the socket.
# stdout displays the captured chat, and can be piped to gfeed.

echoerr() { echo -n "$@" 1>&2; }
echoerrlf() { echo "$@" 1>&2; }

headers='{"x_gterm_response": "capture_chat", "x_gterm_parameters": {"cookie": "'"${GTERM_COOKIE}"'"}}'

esc=`printf "\033"`
gterm_code="1155"
gterm_cookie=${GTERM_COOKIE:-${LC_GTERM_COOKIE}}
echoerr "${esc}[?${gterm_code};${gterm_cookie}h"

echoerr "$headers"
echoerrlf ""
echoerrlf ""
echoerr "${esc}[?${gterm_code}l"

while true; do
    sleep 1
    read INPUT
    echo $INPUT
done

