#!/usr/bin/sh
#
# The ARCH-Insight launcher, installed as /usr/bin/arch-insight. (#484)
#
# ARCH-Insight is a web application. This package does not carry a copy of it
# and does not carry a browser to run it in -- see README.md, "What a Linux app
# is for a product that is a web app", for the argument. What it carries is
# this: the thing that opens the product in a window of its own, using whatever
# browser engine the distribution already ships and already patches.
#
# POSIX sh, not bash. Two reasons: this runs on Debian, where /bin/sh is dash
# and a bashism is a launcher that fails on exactly one family of
# distributions; and `#!/usr/bin/env bash` is what rpmlint reports as
# `env-script-interpreter`, which would be a filter in the rpmlintrc for a
# dependency this script does not need.

set -eu

# Where the product lives. Overridable, in two ways, because ARCH-Insight is
# sold to organisations and `deploy/k8s/` exists: a customer running their own
# instance needs the launcher to open theirs, and the alternative is telling
# them to edit a file the next package update overwrites.
#
#   ARCH_INSIGHT_URL in the environment   -- one run, or a user's own profile
#   /etc/arch-insight.conf                -- the machine, set once by an admin
#
# The environment wins over the file. The file is %config(noreplace) / a dpkg
# conffile, so an upgrade keeps what the admin wrote.
DEFAULT_URL="https://arch-insight.com/"
CONF="/etc/arch-insight.conf"

ENV_URL="${ARCH_INSIGHT_URL:-}"
if [ -r "$CONF" ]; then
    # shellcheck disable=SC1090  -- a config file, read at run time by design.
    . "$CONF"
fi
URL="${ENV_URL:-${ARCH_INSIGHT_URL:-$DEFAULT_URL}}"

# The trailing slash matters and is not cosmetic: see the WM_CLASS note below.
case "$URL" in
    */) ;;
    *) URL="$URL/" ;;
esac

# The window class this launcher claims.
#
# It is the AppStream component id, and the generated desktop entry says
# `StartupWMClass=` with the same string. Both halves are needed and neither is
# optional: the shell matches a window to the launcher that opened it by
# WM_CLASS, and when they do not match, clicking the launcher opens a window
# that appears in the dock as a *second* item -- under Chromium's name, with
# Chromium's icon -- while the launcher icon keeps its "starting" animation
# until it times out.
#
# Passing `--class` rather than predicting what the browser would have chosen
# is deliberate. Chromium derives a class from the URL when `--app=` is used
# (`chrome-arch-insight.com__-Default` for this one, with the trailing slash
# and the default profile); that string is undocumented, it changes with the
# profile, and a launcher whose icon silently stops grouping because somebody
# added a path to the URL is a bug nobody can search for. The URL is still
# normalised to a trailing slash above so that the derived name is at least
# predictable on the platforms that ignore `--class`.
#
# Ignored on Wayland by some browser builds, which take the app id from the
# desktop file instead -- which is this same string, so the two agree either
# way. That is the whole reason the id was chosen as the class.
WMCLASS="com.arch-insight.ArchInsight"

# Chromium-family browsers, in the order a Linux desktop is likely to have
# them. `--app=URL` is the one thing that makes this a window rather than a
# tab: no tab strip, no address bar, its own entry in the window switcher --
# the same frame an installed PWA gets, because it is the same mechanism.
#
# The browser's profile is shared with ordinary browsing of the same origin,
# and that is correct here rather than a leak: a user already signed in to
# arch-insight.com in their browser is signed in when this window opens. The
# Android package has the opposite problem for the same reason -- see
# deploy/android/README.md, "When the web view already holds a different
# session" -- because there a native account picker can disagree with the
# browser. Nothing here can disagree with anything: there is no native identity
# on this platform, and deliberately so.
for browser in \
    chromium chromium-browser \
    google-chrome-stable google-chrome \
    brave-browser vivaldi-stable microsoft-edge-stable
do
    if command -v "$browser" >/dev/null 2>&1; then
        exec "$browser" --class="$WMCLASS" --app="$URL"
    fi
done

# Firefox has no `--app`. Site-specific-browser mode (`--ssb`) was removed
# before it ever shipped enabled, and `--kiosk` is full-screen with no way out
# for a user who just wanted a window. So this is an ordinary Firefox window
# on the right page -- which is the honest degradation, and it is still a
# window of its own with the right class on X11.
for browser in firefox firefox-esr; do
    if command -v "$browser" >/dev/null 2>&1; then
        exec "$browser" --class "$WMCLASS" --new-window "$URL"
    fi
done

# Whatever the desktop calls its default browser. From here the window is a
# tab in a browser the user already had open, the launcher icon does not group,
# and the product still opens -- which is the point. `xdg-utils` is the
# package's one hard dependency for exactly this line.
if command -v xdg-open >/dev/null 2>&1; then
    exec xdg-open "$URL"
fi

# Reached only on a machine with no browser at all. Say so, name the URL, and
# fail: a launcher that exits 0 having done nothing is a launcher the user
# clicks four more times.
echo "arch-insight: no web browser found." >&2
echo "  Install one (chromium, firefox, google-chrome, ...) or open $URL" >&2
echo "  by hand. xdg-utils is installed but no default browser is set." >&2
exit 1
