#!/bin/sh
# Agent Functual installer, served at https://get.functual.com:
#
#   curl -fsSL https://get.functual.com | sudo sh -s -- <enrollment token> [--instance <name>]
#
# Adds Functual's signed package repository (apt or dnf/yum), installs the "functual" package, then enrolls this
# machine with the token from Functual Account and starts the agent. Without a token it only installs. Safe to run
# again: the repository and package are updated, and an enrolled instance is left as it is.
#
# Everything is inside main(), so a download cut off halfway runs nothing.
set -eu

REPO_URL="https://pkgs.functual.com"
KEY_URL="$REPO_URL/functual.asc"

say() { printf '%s\n' "$*"; }
fail() { printf 'Agent Functual installer: %s\n' "$*" >&2; exit 1; }

fetch() { # url, destination
    if command -v curl >/dev/null 2>&1; then
        curl -fsSL --proto '=https' --tlsv1.2 -o "$2" "$1"
    elif command -v wget >/dev/null 2>&1; then
        wget -q --https-only -O "$2" "$1"
    else
        fail "needs curl or wget"
    fi
}

install_apt() {
    say "Adding the apt repository..."
    fetch "$KEY_URL" /usr/share/keyrings/functual.asc
    chmod 0644 /usr/share/keyrings/functual.asc
    printf 'deb [signed-by=/usr/share/keyrings/functual.asc] %s/apt stable main\n' "$REPO_URL" > /etc/apt/sources.list.d/functual.list
    apt-get update -qq -o Dir::Etc::sourcelist=/etc/apt/sources.list.d/functual.list -o Dir::Etc::sourceparts=- -o APT::Get::List-Cleanup=0
    say "Installing functual..."
    DEBIAN_FRONTEND=noninteractive apt-get install -y -qq functual
}

install_rpm() { # dnf or yum
    say "Adding the yum repository..."
    fetch "$REPO_URL/rpm/functual.repo" /etc/yum.repos.d/functual.repo
    chmod 0644 /etc/yum.repos.d/functual.repo
    rpm --import "$KEY_URL"
    say "Installing functual..."
    "$1" install -y -q functual
}

main() {
    token=""
    instance="default"
    while [ $# -gt 0 ]; do
        case "$1" in
            --instance) [ $# -ge 2 ] || fail "--instance needs a name"; instance="$2"; shift 2 ;;
            --instance=*) instance="${1#*=}"; shift ;;
            -h|--help) say "Usage: curl -fsSL https://get.functual.com | sudo sh -s -- <token> [--instance <name>]"; exit 0 ;;
            -*) fail "unknown option $1" ;;
            *) token="$1"; shift ;;
        esac
    done
    [ -n "$token" ] || token="${FUNCTUAL_ENROLL_TOKEN:-}"

    [ "$(id -u)" -eq 0 ] || fail "run it as root: curl -fsSL https://get.functual.com | sudo sh -s -- <token>"
    [ "$(uname -s)" = Linux ] || fail "this installer is for Linux"
    case "$(uname -m)" in
        x86_64|amd64|aarch64|arm64) ;;
        *) fail "unsupported architecture $(uname -m) (amd64 and arm64 are supported)" ;;
    esac
    [ -d /run/systemd/system ] || say "Note: systemd isn't running here; the agent won't start as a service."

    if command -v apt-get >/dev/null 2>&1; then
        install_apt
    elif command -v dnf >/dev/null 2>&1; then
        install_rpm dnf
    elif command -v yum >/dev/null 2>&1; then
        install_rpm yum
    else
        fail "no apt-get, dnf or yum found; supported: Debian, Ubuntu, RHEL, Fedora, Amazon Linux and relatives"
    fi
    say "Installed $(functual version)."

    if [ -z "$token" ]; then
        say "Enroll this machine with a token from Functual Account: sudo functual enroll <token>"
        return 0
    fi
    if [ -f "/var/lib/functual/$instance/agent.json" ]; then
        say "Instance $instance is already enrolled; left as it is (functual status)."
        return 0
    fi
    # The token goes through the environment, which keeps it out of the process list
    FUNCTUAL_ENROLL_TOKEN="$token" functual enroll --instance "$instance"
}

main "$@"
