#!/bin/sh
set -e

SSH_USER="${SSH_USER:-player}"
APP_CMD="python3 /app/proxy/app.py"
RESTART_FLAG="/tmp/proxy.restart"

while true; do
    su -s /bin/sh -c "exec $APP_CMD" "$SSH_USER" &
    APP_PID=$!

    while kill -0 "$APP_PID" 2>/dev/null; do
        if [ -f "$RESTART_FLAG" ]; then
            rm -f "$RESTART_FLAG"
            kill "$APP_PID" 2>/dev/null || true
            wait "$APP_PID" 2>/dev/null || true
            break
        fi
        sleep 0.2
    done

    if kill -0 "$APP_PID" 2>/dev/null; then
        wait "$APP_PID" 2>/dev/null || true
    fi

    sleep 0.2
 done
