#!/bin/sh

set -e

export DISPLAY=${DISPLAY:-:0}
export WAYLAND_DISPLAY=${WAYLAND_DISPLAY:-wayland-1}
export XDG_RUNTIME_DIR=${XDG_RUNTIME_DIR:-/run/user/$(id -u)}

EXO_CLINIC_PORT=${EXO_CLINIC_PORT:-$(cat /etc/egzotech/exo-clinic/port)}
EXO_CLINIC_BACKEND_PORT=${EXO_CLINIC_PORT:-$(cat /etc/egzotech/exo-clinic-backend/port)}

if command -v chromium; then
    START_PROGRAM=chromium
elif command -v google-chrome; then
    START_PROGRAM=google-chrome
else
    # Fallback to chromium
    START_PROGRAM=chromium
fi

START_PARAMS=

if [ $(id -u) -eq 0 ]; then
  START_PARAMS=--no-sandbox
fi

LOAD_URL=$(printf "file:///usr/share/exo-clinic-kiosk/load.html?frontend-port=%s&backend-port=%s" "$EXO_CLINIC_PORT" "$EXO_CLINIC_BACKEND_PORT")

exec $START_PROGRAM $START_PARAMS --remote-debugging-port=5859 --kiosk "$LOAD_URL"
