#!/bin/bash

set -e

SERIAL_PORT=/dev/ttyS0
SERIAL_BAUDRATE=57600
TCP_PORT=3333

export PYTHONPATH=/labor/ccd:$PYTHONPATH

echo "Initializing ST5 to configured baud rate:"
echo ""

python3 - <<EOF
import sys
import sbigpy

uri = "pty:/$SERIAL_PORT?b=$SERIAL_BAUDRATE"
st5 = sbigpy.ST5(uri)
st5.open()
st5.print_status()
EOF

echo ""
echo "Starting TCP-serial proxy."
echo "Press Ctrl+C to stop."
echo ""

socat tcp-listen:$TCP_PORT,reuseaddr,fork $SERIAL_PORT,raw,b$SERIAL_BAUDRATE

echo "TCP-serial proxy stopped."
echo "Reset ST5 CPU before restarting server."
