#!/usr/bin/env bash
# Update the Debian LXC and its openHop Repeater installation.

set -Eeuo pipefail

readonly REPO_DIR="/root/openhop-repeater"
readonly CONSOLE_REPO_DIR="/root/pymc_console"
readonly CONSOLE_REPO_URL="https://github.com/Treehouse-00/pymc_console-dist.git"
readonly CONSOLE_UI_DIR="/opt/pymc_console/web/html"

if [[ "$EUID" -ne 0 ]]; then
    echo "This command must be run as root." >&2
    exit 1
fi

if [[ ! -d "$REPO_DIR/.git" || ! -f "$REPO_DIR/manage.sh" ]]; then
    echo "openHop Repeater source checkout not found at $REPO_DIR." >&2
    exit 1
fi

BRANCH=$(git -C "$REPO_DIR" branch --show-current)
if [[ -z "$BRANCH" ]]; then
    echo "The openHop Repeater checkout is in detached HEAD state; update aborted." >&2
    exit 1
fi

if [[ -n "$(git -C "$REPO_DIR" status --porcelain)" ]]; then
    echo "The openHop Repeater checkout has local changes; update aborted." >&2
    exit 1
fi

CONSOLE_INSTALLED=false
CONSOLE_CHECKOUT=false
[[ -f "$CONSOLE_UI_DIR/index.html" ]] && CONSOLE_INSTALLED=true
[[ -d "$CONSOLE_REPO_DIR/.git" && -f "$CONSOLE_REPO_DIR/manage.sh" ]] \
    && CONSOLE_CHECKOUT=true

echo "This update will:"
echo "  - update installed Debian packages"
echo "  - fast-forward openHop Repeater branch: $BRANCH"
echo "  - run openHop Repeater manage.sh upgrade"
if [[ "$CONSOLE_INSTALLED" == "true" && "$CONSOLE_CHECKOUT" == "true" ]]; then
    echo "  - fast-forward and upgrade openHop Console"
elif [[ "$CONSOLE_INSTALLED" == "true" ]]; then
    echo "  - create /root/pymc_console and upgrade openHop Console"
else
    echo "  - leave openHop Console unchanged (not installed)"
fi
echo ""
read -rp "Continue? [y/N]: " reply
if [[ ! "$reply" =~ ^[Yy]([Ee][Ss])?$ ]]; then
    echo "Update cancelled."
    exit 0
fi

echo "==> Checking openHop Repeater branch: $BRANCH"
git -C "$REPO_DIR" fetch origin --prune
if ! git -C "$REPO_DIR" show-ref --verify --quiet "refs/remotes/origin/$BRANCH"; then
    echo "Remote branch origin/$BRANCH does not exist; update aborted." >&2
    exit 1
fi
if ! git -C "$REPO_DIR" merge-base --is-ancestor HEAD "origin/$BRANCH"; then
    echo "The local branch cannot be fast-forwarded to origin/$BRANCH; update aborted." >&2
    exit 1
fi

if [[ "$CONSOLE_INSTALLED" == "true" ]]; then
    if [[ "$CONSOLE_CHECKOUT" != "true" ]]; then
        if [[ -e "$CONSOLE_REPO_DIR" ]]; then
            echo "$CONSOLE_REPO_DIR exists but is not a valid Console checkout; update aborted." >&2
            exit 1
        fi
        echo "==> Creating openHop Console checkout"
        git clone --depth 1 --single-branch --branch main --no-tags \
            "$CONSOLE_REPO_URL" "$CONSOLE_REPO_DIR"
        CONSOLE_CHECKOUT=true
    fi

    CONSOLE_BRANCH=$(git -C "$CONSOLE_REPO_DIR" branch --show-current)
    if [[ -z "$CONSOLE_BRANCH" ]]; then
        echo "The openHop Console checkout is in detached HEAD state; update aborted." >&2
        exit 1
    fi
    if [[ -n "$(git -C "$CONSOLE_REPO_DIR" status --porcelain)" ]]; then
        echo "The openHop Console checkout has local changes; update aborted." >&2
        exit 1
    fi
    git -C "$CONSOLE_REPO_DIR" fetch origin --prune
    if ! git -C "$CONSOLE_REPO_DIR" show-ref --verify --quiet \
        "refs/remotes/origin/$CONSOLE_BRANCH"; then
        echo "Remote Console branch origin/$CONSOLE_BRANCH does not exist; update aborted." >&2
        exit 1
    fi
    if ! git -C "$CONSOLE_REPO_DIR" merge-base --is-ancestor \
        HEAD "origin/$CONSOLE_BRANCH"; then
        echo "The local Console branch cannot be fast-forwarded; update aborted." >&2
        exit 1
    fi
fi

export DEBIAN_FRONTEND=noninteractive

echo "==> Updating Debian packages"
apt-get update
apt-get upgrade -y

echo "==> Updating openHop Repeater branch: $BRANCH"
git -C "$REPO_DIR" pull --ff-only origin "$BRANCH"

echo "==> Running openHop Repeater upgrade"
cd "$REPO_DIR"
OPENHOP_UPGRADE_REF="$BRANCH" TERM="${TERM:-xterm}" bash manage.sh upgrade

if [[ "$CONSOLE_INSTALLED" == "true" ]]; then
    echo "==> Updating openHop Console branch: $CONSOLE_BRANCH"
    git -C "$CONSOLE_REPO_DIR" pull --ff-only origin "$CONSOLE_BRANCH"
    echo "==> Running openHop Console upgrade"
    cd "$CONSOLE_REPO_DIR"
    ASSUME_YES=1 NO_COLOR=1 bash manage.sh upgrade
fi

# Keep the documented management copy in /opt aligned with the source used for
# this successful upgrade.
install -m 0755 "$REPO_DIR/manage.sh" /opt/openhop_repeater/manage.sh

# Refresh the installed helper for the next run without replacing this script
# until all update steps have completed successfully.
install -m 0755 "$REPO_DIR/scripts/openhop-update" /usr/local/bin/openhop-update.new
mv /usr/local/bin/openhop-update.new /usr/local/bin/openhop-update
ln -sfn /usr/local/bin/openhop-update /usr/local/bin/update

echo "==> Update complete"
