#!/usr/bin/env bash
set -euo pipefail
if [[ $EUID -eq 0 || -n "${SSH_CONNECTION:-}" || ! $(tty) =~ ^/dev/tty[0-9]+$ ]]; then
    printf '%s\n' 'Use the physical console as si, not sudo/SSH. / 물리 콘솔에서 si로 실행하세요.' >&2
    exit 1
fi
running=$(docker ps --format '{{.Names}}')
if grep -q '^contextra-icerti-' <<< "$running"; then
    printf '%s\n' 'Contextra is running. Confirm maintenance Stop first. / 유지보수 정지가 필요합니다.' >&2
    exit 1
fi
if [[ -S /tmp/.X11-unix/X0 ]]; then
    printf '%s\n' 'Display :0 already exists. No existing display was stopped.' >&2
    exit 1
fi
exec startx "$HOME/.local/opt/contextra-offline/20260928-r01/gui-session.sh" -- :0 -nolisten tcp
