#!/usr/bin/env bash

set -Eeou pipefail

# This script installs Chromium/Puppeteer browser dependencies on Debian-based systems.
# It uses 'apt-get satisfy' with versioned dependency specs and alternatives, which
# correctly resolves renamed/transitional packages (e.g. libasound2 -> libasound2t64
# on Ubuntu Noble) via Provides: relationships.
#
# Dependencies are read from the deb.deps file (shared with the packaging build).
#
# See https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md#chrome-headless-doesnt-launch-on-unix

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
DEPS_FILE="${SCRIPT_DIR}/deb.deps"

if [ ! -f "$DEPS_FILE" ]; then
  echo "Error: Dependency file not found at '$DEPS_FILE'" >&2
  echo "The deb.deps file should be installed alongside this script." >&2
  exit 1
fi

# Read dependencies from file into an array, skipping comments and empty lines
mapfile -t DEPS < <(grep -vE '^[[:space:]]*#|^[[:space:]]*$' "$DEPS_FILE")

echo "Updating package list..."
apt-get update -qq

echo "Installing browser dependencies via apt-get satisfy..."
apt-get satisfy -yq "${DEPS[@]}"

echo
echo "Successfully installed browser dependencies"
