#!/bin/sh
# StatusOwl watch-owl installer.
#
# Usage:
#   curl -fsSL https://packages.statusowl.net/install.sh | sudo sh
#
# Inspect this script before piping to a shell. It adds the StatusOwl package
# repository and GPG key, then installs the `watch-owl` package via the
# system package manager (apt or dnf/yum). No network calls are made to
# hosts other than packages.statusowl.net.

set -eu

REPO_HOST="https://packages.statusowl.net"
GPG_URL="$REPO_HOST/statusowl.gpg"
APT_URL="$REPO_HOST/apt"
YUM_URL="$REPO_HOST/yum/stable"

say() {
    printf '%s\n' "$1"
}

die() {
    printf 'error: %s\n' "$1" >&2
    exit 1
}

require_root() {
    if [ "$(id -u)" -eq 0 ]; then
        return 0
    fi
    if command -v sudo >/dev/null 2>&1; then
        say "Re-executing with sudo..."
        exec sudo -E sh "$0" "$@"
    fi
    die "Re-run as root (sudo)."
}

detect_family() {
    if [ ! -r /etc/os-release ]; then
        die "Cannot read /etc/os-release; unsupported system."
    fi
    # shellcheck disable=SC1091
    . /etc/os-release
    OS_ID="${ID:-}"
    OS_ID_LIKE="${ID_LIKE:-}"
    OS_PRETTY="${PRETTY_NAME:-$OS_ID}"

    case " $OS_ID $OS_ID_LIKE " in
        *" debian "*|*" ubuntu "*|*" linuxmint "*|*" pop "*|*" elementary "*)
            FAMILY="debian"
            ;;
        *" rhel "*|*" fedora "*|*" centos "*|*" rocky "*|*" almalinux "*|*" ol "*)
            FAMILY="rhel"
            ;;
        *)
            die "Unsupported distribution: $OS_ID. Install manually from https://github.com/statusowl/watch-owl/releases"
            ;;
    esac
}

install_debian() {
    command -v curl >/dev/null 2>&1 || die "curl is required but not installed."
    install -m 0755 -d /etc/apt/keyrings
    curl -fsSL "$GPG_URL" -o /etc/apt/keyrings/statusowl.gpg
    chmod 0644 /etc/apt/keyrings/statusowl.gpg
    printf 'deb [signed-by=/etc/apt/keyrings/statusowl.gpg] %s stable main\n' "$APT_URL" \
        > /etc/apt/sources.list.d/statusowl.list
    apt-get update
    apt-get install -y watch-owl
}

install_rhel() {
    cat > /etc/yum.repos.d/statusowl.repo <<EOF
[statusowl]
name=StatusOwl Package Repository
baseurl=$YUM_URL
enabled=1
gpgcheck=1
gpgkey=$GPG_URL
EOF
    if command -v dnf >/dev/null 2>&1; then
        dnf install -y watch-owl
    elif command -v yum >/dev/null 2>&1; then
        yum install -y watch-owl
    else
        die "Neither dnf nor yum found on this system."
    fi
}

main() {
    require_root "$@"
    detect_family

    say "================================================================"
    say "  StatusOwl watch-owl installer"
    say "================================================================"
    say "Detected: $OS_PRETTY ($FAMILY-family)"
    say "Adding repository and GPG key..."
    say "Installing watch-owl..."

    case "$FAMILY" in
        debian) install_debian ;;
        rhel)   install_rhel ;;
    esac

    say "================================================================"
    say "  Installation complete."
    say ""
    say "Next steps:"
    say "  1. Generate an install token in the StatusOwl dashboard"
    say "  2. Run: sudo -u watch-owl watch-owl enroll --token <TOKEN>"
    say "  3. Start the service: sudo systemctl start watch-owl"
    say "================================================================"
}

main "$@"
