#!/bin/bash
set -e

echo "[socat] Creating virtual serial port pair..."
socat PTY,raw,echo=0,link=/tmp/vcom_read PTY,raw,echo=0,link=/tmp/vcom_write &

# Wait until both PTY symlinks exist
for i in $(seq 1 20); do
    if [ -e /tmp/vcom_read ] && [ -e /tmp/vcom_write ]; then
        echo "[socat] Virtual ports ready: /tmp/vcom_read <-> /tmp/vcom_write"
        break
    fi
    sleep 0.5
done

echo "[test_writer] Starting test COM port writer..."
cd /app/test_comport_writer
COM_PORT=/tmp/vcom_write python test_writer.py &

echo "[serial_bridge] Starting Flask SSE server..."
cd /app/serial_bridge
exec python app.py
