#!/bin/bash
# AdaDo Browser Launcher

# Set AdaDo Browser specific environment
export CHROME_WRAPPER="/usr/bin/adado-browser"

# Policy paths
export CHROME_POLICY_FILE="/etc/chromium/policies/managed/adado.json"

# Find ungoogled-chromium binary
CHROMIUM_BIN=""
# 2026-10-25 (Claude, Dan live: "I think youre getting confused... I
# already had my custom chromium already installed and working with the
# right logo... now I seem to have two different chromiums"): REAL bug
# fix. The desktop icon has only ever pointed at this launcher, which
# never checked for the real, already-built, correctly-branded chrome at
# /opt/adado-browser/chrome -- it fell through to generic system/snap
# chromium instead, which is where the wrong-logo second browser came
# from. Check the real binary FIRST.
if [ -x "/opt/adado-browser/chrome" ]; then
    CHROMIUM_BIN="/opt/adado-browser/chrome"
elif [ -x "/usr/bin/chromium" ]; then
    CHROMIUM_BIN="/usr/bin/chromium"
elif [ -x "/usr/bin/chromium-browser" ]; then
    CHROMIUM_BIN="/usr/bin/chromium-browser"
elif [ -x "/snap/bin/chromium" ]; then
    CHROMIUM_BIN="/snap/bin/chromium"
else
    echo "Error: ungoogled-chromium not found!"
    echo "Please install ungoogled-chromium or chromium first."
    echo ""
    echo "On Debian/Ubuntu:"
    echo "  sudo apt install chromium"
    echo ""
    echo "Or download ungoogled-chromium from:"
    echo "  https://github.com/ungoogled-software/ungoogled-chromium/releases"
    exit 1
fi

# Non-blocking mandatory-evergreen update check (ADADO-814/2630) -- never
# delays or blocks browser launch; auto-update.sh itself rate-limits to
# once per hour and fails silently on any network/server error.
if [ -x /usr/share/adado-browser/scripts/auto-update.sh ]; then
    /usr/share/adado-browser/scripts/auto-update.sh check >/dev/null 2>&1 &
fi

# 2026-10-25 (Claude, Dan live): load every real extension that's
# actually present -- our packaged ada-account, plus ublock if it's
# installed at the legacy /opt/adado-browser/extensions path (not part of
# this package; left in place, not duplicated).
EXT_LIST="/usr/share/adado-browser/extensions/ada-account"
if [ -d "/opt/adado-browser/extensions/ublock" ]; then
    EXT_LIST="/opt/adado-browser/extensions/ublock,${EXT_LIST}"
fi

# Launch with AdaDo branding and configuration
exec "$CHROMIUM_BIN" \
    --user-data-dir="$HOME/.config/adado-browser" \
    --class="AdaDo Browser" \
    --disable-background-networking \
    --disable-breakpad \
    --disable-crash-reporter \
    --disable-domain-reliability \
    --disable-sync \
    --disable-features=AutofillServerCommunication,MediaRouter \
    --enable-features=WebUIDarkMode \
    --load-extension="${EXT_LIST}" \
    "$@"
