#!/bin/sh
# Installs Fabled Physician from Fabled-Git's package registry:
#
#   curl -fsSL https://git.fabledsword.com/api/packages/bvandeusen/generic/fabled-physician-install/dev/install.sh | sudo sh
#
# It adds Physician's package repository and its signing key to the system's
# own package manager (apt, dnf or pacman), then installs the package, which
# brings what the app needs with it. From then on the system's normal updates
# deliver new versions, and the app says when one is waiting.
#
# The repository line it writes is the channel: "dev" here. Run the other
# channel's script to move to it. Needs only curl or wget beforehand.
set -eu

CHANNEL="${PHYSICIAN_CHANNEL:-dev}"
REGISTRY=https://git.fabledsword.com/api/packages/bvandeusen
PACKAGE=fabled-physician

if [ "$(id -u)" != 0 ]; then
    echo "Run this with sudo." >&2
    exit 1
fi
case "$CHANNEL" in
    stable|dev) ;;
    *) echo "Unknown channel: $CHANNEL (stable or dev)" >&2; exit 1 ;;
esac
if [ -d /opt/fabled-physician ]; then
    echo "A development install of Physician is here (/opt/fabled-physician)." >&2
    echo "Remove it first with: sudo scripts/dev-uninstall.sh" >&2
    exit 1
fi

fetch() {
    if command -v curl >/dev/null; then
        curl -fsSL "$1"
    elif command -v wget >/dev/null; then
        wget -qO- "$1"
    else
        echo "This needs curl or wget." >&2
        exit 1
    fi
}

if command -v apt-get >/dev/null; then
    echo "Adding Physician's $CHANNEL repository to apt"
    install -d -m 0755 /etc/apt/keyrings
    fetch "$REGISTRY/debian/repository.key" > /etc/apt/keyrings/fabled-physician.asc
    cat > /etc/apt/sources.list.d/fabled-physician.sources <<EOF
Types: deb
URIs: $REGISTRY/debian
Suites: $CHANNEL
Components: main
Signed-By: /etc/apt/keyrings/fabled-physician.asc
EOF
    apt-get update -q
    DEBIAN_FRONTEND=noninteractive apt-get install -y -q "$PACKAGE"
elif command -v dnf >/dev/null; then
    echo "Adding Physician's $CHANNEL repository to dnf"
    fetch "$REGISTRY/rpm/$CHANNEL.repo" > /etc/yum.repos.d/fabled-physician.repo
    dnf install -y "$PACKAGE"
elif command -v pacman >/dev/null; then
    echo "Adding Physician's $CHANNEL repository to pacman"
    key=$(mktemp)
    trap 'rm -f "$key"' EXIT
    fetch "$REGISTRY/arch/repository.key" > "$key"
    pacman-key --add "$key"
    fingerprint=$(gpg --with-colons --show-keys "$key" | awk -F: '/^fpr:/ { print $10; exit }')
    pacman-key --lsign-key "$fingerprint"
    # One section, replaced on a second run so the channel can change.
    sed -i '/^# Fabled Physician$/,/^$/d' /etc/pacman.conf
    printf '\n# Fabled Physician\n[fabled-physician]\nSigLevel = Required\nServer = %s\n\n' \
        "$REGISTRY/arch/$CHANNEL/\$arch" >> /etc/pacman.conf
    # Arch upgrades as a whole: installing from a refreshed database without
    # upgrading the rest can leave libraries out of step. pacman asks first.
    pacman -Syu --needed "$PACKAGE" </dev/tty
else
    echo "This works with apt (Ubuntu, Debian), dnf (Fedora) or pacman (Arch)." >&2
    exit 1
fi

echo "Done. Open \"Fabled Physician\" from your applications, or run: physician"
