#!/usr/bin/env bash
# TofuFactory one-line installer. Served at https://releases.tofufactory.dev/install.sh
#
#   curl -fsSL https://releases.tofufactory.dev/install.sh | sudo bash
#
# This only configures the *signed* apt repository and installs the package.
# Everything after (updates, removal) is plain apt — `apt upgrade` / `apt remove`.
set -euo pipefail

REPO_URL="https://releases.tofufactory.dev/apt"
KEYRING="/usr/share/keyrings/tofufactory.gpg"
LIST="/etc/apt/sources.list.d/tofufactory.list"

if [ "$(id -u)" -ne 0 ]; then
    echo "This installer needs root. Re-run:  curl -fsSL $REPO_URL/../install.sh | sudo bash" >&2
    exit 1
fi

for tool in curl gpg apt-get; do
    command -v "$tool" >/dev/null || { echo "Missing required tool: $tool" >&2; exit 1; }
done

echo ">> Adding the TofuFactory apt repository (signed)…"
install -d -m 0755 "$(dirname "$KEYRING")"
curl -fsSL "$REPO_URL/key.gpg" | gpg --dearmor -o "$KEYRING"
echo "deb [signed-by=$KEYRING] $REPO_URL stable main" > "$LIST"

echo ">> Installing tofufactory…"
apt-get update
apt-get install -y tofufactory

echo
echo "TofuFactory installed. Launch it from your app menu, or run:  tofufactory"
echo "Updates land via the normal:  sudo apt upgrade"
