#!/usr/bin/env bash

# Nomarchy Full Update Script
# 1. Syncs the local repository (if applicable)
# 2. Performs a full system update (NixOS + Home Manager)

set -e

# Detect the repository location
if [ -f "/etc/nixos/flake.nix" ]; then
    REPO_DIR="/etc/nixos"
elif [ -f "/etc/nomarchy/flake.nix" ]; then
    REPO_DIR="/etc/nomarchy"
else
    echo "Error: Nomarchy flake repository not found in /etc/nixos or /etc/nomarchy."
    exit 1
fi

echo "--- Starting Nomarchy Full Update ---"

# 1. Sync repository if it's a git repo
if [ -d "$REPO_DIR/.git" ]; then
    echo "Syncing repository from remote..."
    if ! git -C "$REPO_DIR" pull --ff-only; then
        echo "Warning: git pull failed. Proceeding with local configuration."
    fi
fi

# 2. System update
nomarchy-sys-update

echo "--- Nomarchy Full Update Complete ---"
