#!/usr/bin/env bash
set -euo pipefail

REPO_URL="https://repo.mtoolls.org/fasttunnel/release/fasttunnel.repo"
REPO_FILE="/etc/yum.repos.d/fasttunnel.repo"
CHANNEL="${FASTTUNNEL_CHANNEL:-stable}"

if ! command -v dnf >/dev/null 2>&1; then
    echo "FastTunnel installer: dnf is required (CentOS Stream/RHEL/Rocky/Alma/Fedora)." >&2
    exit 1
fi

if command -v curl >/dev/null 2>&1; then
    curl -fsSL "$REPO_URL" -o "$REPO_FILE"
elif command -v wget >/dev/null 2>&1; then
    wget -qO "$REPO_FILE" "$REPO_URL"
else
    echo "FastTunnel installer: curl or wget is required." >&2
    exit 1
fi

if [[ "$CHANNEL" == "testing" ]]; then
    dnf -y --enablerepo=fasttunnel-testing install fasttunnel
else
    dnf -y install fasttunnel
fi

echo
printf 'FastTunnel installed. Configure /etc/fasttunnel/fasttunnel.env, then run:\n  systemctl enable --now fasttunnel\n'
