#!/usr/bin/env bash
set -Eeuo pipefail
[[ $EUID -ne 0 ]] || { echo 'Run ./update.sh as your normal ArchForge user, NOT with sudo.' >&2; exit 1; }
ROOT=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)
[[ -d "$ROOT/.git" ]] || { echo 'Updates require your original ArchForge checkout.' >&2; exit 1; }
[[ -z "$(git -C "$ROOT" status --porcelain)" ]] || { echo 'Uncommitted changes detected. Commit or stash before updating.' >&2;exit 1; }
BRANCH=$(git -C "$ROOT" branch --show-current)
[[ -n "$BRANCH" ]] || { echo 'Detached HEAD. Check out the desired release branch first.' >&2;exit 1; }
echo "Fetching updates for branch $BRANCH..."
git -C "$ROOT" pull --ff-only
read -rp 'Deploy this version? Type DEPLOY: ' ANS
[[ "$ANS" == DEPLOY ]] || exit 0
sudo bash "$ROOT/scripts/deploy.sh" "$ROOT"
