#!/bin/sh
# Normal Chromium window. Do not add --kiosk or --app.
# Profile is per-bot / per-screen so two Team desktops never share cookies
# or Chromium's SingletonLock (that dialog is "Profile error occurred").
HOME="${HOME:-/home/lazyboy}"
display="${DISPLAY#:}"
display="${display:-1}"
if [ -n "${LAZYBOY_BROWSER_PROFILE:-}" ]; then
  PROFILE="$LAZYBOY_BROWSER_PROFILE"
elif [ -n "${LAZYBOY_BOT_ID:-}" ]; then
  PROFILE="$HOME/.browser-profiles/bots/$LAZYBOY_BOT_ID"
elif [ -r "/tmp/lazyboy/screen-${display}.profile" ]; then
  PROFILE="$(cat "/tmp/lazyboy/screen-${display}.profile")"
else
  PROFILE="$HOME/.browser-profiles/displays/${display}"
fi
mkdir -p "$PROFILE"
# The bot drives this same window over DevTools (port 9221 + display number,
# configured by this launcher). Every launch path (boot, panel launcher,
# xdg-open, launch_app) must open it, otherwise the model cannot attach to the
# browser the human is looking at and would have to restart it.
DEVTOOLS_PORT=$((9221 + display))
live=$(pgrep -f "chromium.*--user-data-dir=${PROFILE}" 2>/dev/null | head -1)
lock="$(readlink "$PROFILE/SingletonLock" 2>/dev/null || true)"
lock_pid="${lock##*-}"
if [ -n "$live" ]; then
  # A second Chromium on the same profile is the "Profile error occurred" dialog.
  # Only hand URLs to the existing process (flags are meaningless to it and an
  # empty argument list would open a stray New Tab in the user's window).
  urls=""
  for arg in "$@"; do
    case "$arg" in
      -*) ;;
      *) urls="$urls $arg" ;;
    esac
  done
  if [ -n "$urls" ] && [ -n "$lock_pid" ] && kill -0 "$lock_pid" 2>/dev/null; then
    # shellcheck disable=SC2086
    exec /usr/bin/chromium --no-sandbox --user-data-dir="$PROFILE" $urls
  fi
  exit 0
fi
rm -f "$PROFILE/SingletonLock" "$PROFILE/SingletonCookie" "$PROFILE/SingletonSocket"
exec /usr/bin/chromium \
  --no-sandbox \
  --remote-debugging-address=127.0.0.1 \
  --remote-debugging-port="$DEVTOOLS_PORT" \
  --test-type \
  --disable-gpu \
  --disable-dev-shm-usage \
  --disable-features=TranslateUI \
  --no-first-run \
  --no-default-browser-check \
  --disable-session-crashed-bubble \
  --hide-crash-restore-bubble \
  --disable-infobars \
  --force-renderer-accessibility \
  --password-store=basic \
  --lang=zh-TW \
  --accept-lang=zh-TW,zh,en-US,en \
  --start-maximized \
  --user-data-dir="$PROFILE" \
  "$@"
