#!/usr/bin/env bash
# Host-side Docker orchestration. Never source downloaded release metadata.
set -euo pipefail
set +x
umask 077
BASE=https://git.sn-s.net/neo/neo-roundtable/raw/branch/main
REGISTRY=git.sn-s.net
STAGING=
ROOT=/opt/containers/neo-roundtable
declare -A manifest=()
COMPOSE=()
RUN=(docker run --rm --network none)
helper_image=
error() { printf '%s\n' "$*" >&2; return 1; }
ok() { printf '✓ %s\n' "$*"; }
fetch() { curl --proto '=https' --proto-redir '=https' --connect-timeout 10 --max-time 120 -fsSL "$1" -o "$2"; }
staging() {
    RUN=(docker run --rm --network none)
    local parent=${TMPDIR:-/tmp}
    [[ ! -d /dev/shm || ! -w /dev/shm ]] || parent=/dev/shm
    STAGING=$(mktemp -d "$parent/neo-admin.XXXXXXXX")
    trap 'unset token password; rm -rf -- "$STAGING"' EXIT

}
pin_docker() {
    local endpoint=${DOCKER_HOST:-}
    [[ -n $endpoint ]] || endpoint=$(docker context inspect --format '{{.Endpoints.docker.Host}}')
    [[ $endpoint == unix://* ]] || { error 'Use a local Unix-socket Docker engine; remote contexts are not supported.'; return 1; }
    export DOCKER_HOST=$endpoint
    unset DOCKER_CONTEXT
}
terminal() { { : </dev/tty; } 2>/dev/null || error 'A terminal is required for installation.'; }
question() {
    local name=$1 prompt=$2 hidden=${3:-0} value
    if [[ $hidden == 1 ]]; then IFS= read -r -s -p "$prompt: " value </dev/tty; printf '\n' >/dev/tty
    else IFS= read -r -p "$prompt: " value </dev/tty; fi
    [[ -n $value && $value != *$'\r'* ]] || error 'An answer is required.'
    printf -v "$name" '%s' "$value"
}
read_manifest() {
    local file=$1 key value name
    manifest=()
    [[ $(wc -c < "$file") -le 16384 ]] || error 'Manifest is too large.'
    while IFS='=' read -r key value || [[ -n $key ]]; do
        [[ $key =~ ^[a-z][a-z0-9_]{0,63}$ && -n $value && ! -v manifest[$key] ]] || { error 'Invalid or duplicate manifest field.'; return 1; }
        case "$key" in
            format) [[ $value == 1 ]] ;;
            platform) [[ $value == linux/amd64 ]] ;;
            version) [[ $value =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]] ;;
            revision|admin_sha256|compose_sha256) if [[ $key == revision ]]; then [[ $value =~ ^[0-9a-f]{40}$ ]]; else [[ $value =~ ^[0-9a-f]{64}$ ]]; fi ;;
            schema) [[ $value =~ ^[1-9][0-9]{0,4}$ ]] ;;
            postgres_major) [[ $value == 18 ]] ;;
            admin_url|compose_url) [[ $value == "$BASE"/releases/* && $value != *[[:space:]\'\"\`\$\;\&\?\#]* && $value != *..* ]] ;;
            image_roundtable|image_ocr|image_transcription|image_contacts)
                name=${key#image_}; [[ $name != contacts ]] || name=contacts-workshop
                [[ $value =~ ^git\.sn-s\.net/neo-distribution/$name@sha256:[0-9a-f]{64}$ ]] ;;
            image_postgres) [[ $value =~ ^docker\.io/library/postgres@sha256:[0-9a-f]{64}$ ]] ;;
            pull_bytes_roundtable|pull_bytes_ocr|pull_bytes_transcription|pull_bytes_contacts|pull_bytes_postgres|unpacked_bytes_roundtable|unpacked_bytes_ocr|unpacked_bytes_transcription|unpacked_bytes_contacts|unpacked_bytes_postgres)
                [[ $value =~ ^[1-9][0-9]{0,14}$ ]] ;;
            *) error 'Unknown manifest field.'; return 1 ;;
        esac || { error "Invalid manifest field: $key"; return 1; }
        manifest[$key]=$value
    done < "$file"
    for key in format platform version revision schema postgres_major admin_url admin_sha256 compose_url compose_sha256; do [[ -v manifest[$key] ]] || { error "Missing manifest field: $key"; return 1; }; done
    for name in roundtable ocr transcription contacts postgres; do
        for key in image_$name pull_bytes_$name unpacked_bytes_$name; do [[ -v manifest[$key] ]] || { error "Missing manifest field: $key"; return 1; }; done
    done
    [[ ${manifest[admin_url]} == "$BASE/releases/${manifest[version]}/neo-admin" && ${manifest[compose_url]} == "$BASE/releases/${manifest[version]}/compose.yml" ]] || error 'Release URLs must match the version.'
}
latest() { fetch "$BASE/stable.manifest" "$STAGING/release.manifest"; read_manifest "$STAGING/release.manifest"; helper_image=${manifest[image_roundtable]}; }
verify_file() { local actual; actual=$(sha256sum "$1"); [[ ${actual%% *} == "$2" ]] || error 'Release checksum mismatch.'; }
image_space() {
    local name c=0 u=0 root available required
    for name in roundtable ocr transcription contacts postgres; do
        if ! docker image inspect "${manifest[image_$name]}" >/dev/null 2>&1; then
            c=$((c+${manifest[pull_bytes_$name]})); u=$((u+${manifest[unpacked_bytes_$name]}))
        fi
    done
    root=$(docker info --format '{{.DockerRootDir}}')
    [[ -d $root ]] || { error 'Docker storage must be on this host so free disk can be checked.'; return 1; }
    available=$(df -Pk "$root" | awk 'END {print $4*1024}')
    required=$(((c+u)*12/10+1073741824))
    awk -v a="$available" -v r="$required" 'BEGIN{exit !(a>=r)}' || { error "Docker disk needs $required additional bytes; free space first."; return 1; }
    IMAGE_RESERVE_BYTES=$required; DOCKER_DEVICE=$(stat -c %d "$root")
    ok "Docker disk reserve: $required bytes"
}
subnet_check() {
    local subnet=172.28.0.0/16 network existing
    # Compare the fixed production subnet as IPv4 ranges.
    local a b c d prefix ip lo hi eip elo ehi eprefix
    IFS='./' read -r a b c d prefix <<< "$subnet"
    ip=$(((a<<24)+(b<<16)+(c<<8)+d)); lo=$((ip & (0xFFFFFFFF << (32-prefix)))); hi=$((lo + (1 << (32-prefix))-1))
    while IFS= read -r network; do
        [[ -n $network ]] || continue
        while IFS= read -r existing; do
            [[ $existing == *.*/* ]] || continue
            IFS='./' read -r a b c d eprefix <<< "$existing"
            eip=$(((a<<24)+(b<<16)+(c<<8)+d)); elo=$((eip & (0xFFFFFFFF << (32-eprefix)))); ehi=$((elo+(1 << (32-eprefix))-1))
            ((lo>ehi || elo>hi)) || { error "Docker subnet $subnet clashes with existing network $network ($existing); nothing changed."; return 1; }
        done < <(docker network inspect --format '{{range .IPAM.Config}}{{println .Subnet}}{{end}}' "$network")
    done < <(docker network ls -q)
}
host_checks() {
    local mode=$1 os_id
    [[ $(uname -m) == x86_64 && $(uname -s) == Linux ]] || { error 'Only Linux amd64 is supported.'; return 1; }; ok 'Linux amd64'
    os_id=$(sed -n 's/^ID=//p' /etc/os-release | tr -d '"')
    [[ $os_id == debian ]] || { error 'This installer currently supports Debian; use a supported Debian host.'; return 1; }; ok 'Debian'
    command -v docker >/dev/null || { error 'Please install Docker properly https://docs.docker.com/engine/install/debian/'; return 1; }
    pin_docker
    if ! docker info >/dev/null 2>&1 || ! docker compose version >/dev/null 2>&1; then error 'Docker and its Compose plugin must be running and usable by this user.'; return 1; fi; ok 'Docker and Compose'
    curl --proto '=https' --connect-timeout 10 --max-time 30 -sS -D "$STAGING/registry.headers" -o /dev/null "https://$REGISTRY/v2/"
    grep -qi 'realm="https://git.sn-s.net/v2/token"' "$STAGING/registry.headers" || { error 'Registry must advertise its HTTPS token endpoint.'; return 1; }; ok 'Registry reachable over HTTPS'
    if [[ $mode == install ]]; then
        [[ ! -e $ROOT || ( -d $ROOT && -z $(ls -A -- "$ROOT") ) ]] || { error 'Install folder is not empty; use update or adopt.'; return 1; }
        command -v ss >/dev/null || { error 'Install iproute2 so the listening port can be checked.'; return 1; }
        [[ -z $(ss -H -ltn '( sport = :3060 )') ]] || { error '127.0.0.1:3060 is occupied; nothing changed.'; return 1; }
        local project=neo-roundtable
        [[ -z $(docker ps -aq --filter "label=com.docker.compose.project=$project") && -z $(docker volume ls -q --filter "label=com.docker.compose.project=$project") ]] || { error 'An existing Compose project or data volume was found; use adopt.'; return 1; }
        if docker volume inspect "${project}_roundtable-postgres-data" >/dev/null 2>&1; then error 'An old database volume exists; use adopt.'; return 1; fi
        local parent=$ROOT available
        while [[ ! -d $parent ]]; do parent=$(dirname "$parent"); done
        [[ -w $parent ]] || { error "Install folder parent $parent must be writable by this user."; return 1; }
        available=$(df -Pk "$parent" | awk 'END {print $4*1024}')
        awk -v a="$available" 'BEGIN{exit !(a>=1073741824)}' || { error 'Install filesystem needs at least 1 GiB free for initial data.'; return 1; }
        subnet_check
        ok 'Empty install folder, unused project, subnet and port 3060'
    fi
    image_space
}
login() { local directory=$1; mkdir -p "$directory"; chmod 700 "$directory"; printf '%s' "$token" | DOCKER_CONFIG="$directory" docker login "$REGISTRY" --username neo-pull --password-stdin >/dev/null; chmod 600 "$directory/config.json"; }
verify_token() {
    export DOCKER_CONFIG=$STAGING/auth
    login "$DOCKER_CONFIG"
    local name
    for name in roundtable ocr transcription contacts; do docker manifest inspect "${manifest[image_$name]}" > "$STAGING/$name.manifest" || { error "Token cannot read the $name image manifest."; return 1; }; done
    ok 'Token reads all four image manifests'
}
pull_release() { local name; for name in roundtable ocr transcription contacts postgres; do docker pull --platform linux/amd64 "${manifest[image_$name]}"; done; }
export_images() {
    export NEO_IMAGE_ROUNDTABLE=${manifest[image_roundtable]} NEO_IMAGE_OCR=${manifest[image_ocr]} NEO_IMAGE_TRANSCRIPTION=${manifest[image_transcription]} NEO_IMAGE_CONTACTS=${manifest[image_contacts]} NEO_IMAGE_POSTGRES=${manifest[image_postgres]}
}
helper() { "${COMPOSE[@]}" -f "$STAGING/helper.yml" run --rm -T --no-deps --entrypoint roundtable roundtable admin "$@"; }
make_override() {
    local target=$1 name service services pgdata default_pgdata
    services=$("${COMPOSE[@]}" config --services)
    printf 'services:\n' > "$target"
    for name in roundtable ocr transcription contacts postgres; do
        service=$name; [[ $name != contacts ]] || service=contacts-workshop
        # Adoption changes images only; it must not invent incomplete sidecars
        # when an older source Compose does not declare them.
        grep -Fxq "$service" <<< "$services" || continue
        printf '  %s:\n    image: %s\n    pull_policy: never\n' "$service" "${manifest[image_$name]}" >> "$target"
        if [[ $service == postgres && -f $STAGING/current.identity ]]; then
            pgdata=$(helper identity-pgdata < "$STAGING/current.identity")
            default_pgdata=$(docker image inspect --format '{{range .Config.Env}}{{println .}}{{end}}' "${manifest[image_postgres]}" | sed -n 's/^PGDATA=//p')
            # Preserve custom image defaults explicitly, without needlessly
            # changing the Compose hash when the new default already matches.
            if [[ $pgdata != "\"$default_pgdata\"" ]]; then
                printf '    environment:\n      PGDATA: %s\n' "$pgdata" >> "$target"
            fi
        fi
    done
}
setup_compose() {
    export_images
    cd "$ROOT"
    local source_file
    if [[ -f .neo-source ]]; then IFS= read -r source_file < .neo-source
    elif [[ -f compose.yml ]]; then source_file=compose.yml
    elif [[ -f docker-compose.yml ]]; then source_file=docker-compose.yml
    else error 'No existing Compose configuration; use install.'; return 1; fi
    [[ $source_file == compose.yml || $source_file == docker-compose.yml ]] || { error 'Invalid source Compose marker.'; return 1; }
    COMPOSE=(docker compose -f "$ROOT/$source_file")
    if [[ -f .neo-images.yml ]]; then COMPOSE+=(-f "$ROOT/.neo-images.yml"); fi
    printf 'services:\n  roundtable:\n    image: %s\n    pull_policy: never\n' "$helper_image" > "$STAGING/helper.yml"
}
identity() {
    local mode=${1:-running} pg app major project id ids image replacement replacement_major pgdata
    local -a observed
    pg=$("${COMPOSE[@]}" ps -aq postgres); app=$("${COMPOSE[@]}" ps -aq roundtable)
    [[ -n $pg && -n $app && $pg != *$'\n'* && $app != *$'\n'* ]] || { error 'Cannot prove data identity: retain the existing containers and repair the Compose project/mount configuration before retrying. Do not create a new database.'; return 1; }
    if [[ $mode != recovery && $(docker inspect --format '{{.State.Running}}' "$pg") != true ]]; then
        error 'Postgres is stopped; use restore or rollback for recovery.'; return 1
    fi
    image=$(docker inspect --format '{{.Image}}' "$pg")
    [[ $image == sha256:* && $(docker image inspect --format '{{.Id}}' "$image") == "$image" ]] || { error 'Cannot verify the retained Postgres image; recover that exact local image before retrying. Do not recreate the database.'; return 1; }
    major=$("${RUN[@]}" --entrypoint postgres "$image" --version); major=${major##* }; major=${major%%.*}
    replacement=$("${COMPOSE[@]}" config --format json | "${RUN[@]}" -i --entrypoint roundtable "$helper_image" admin compose-postgres-image)
    replacement=$(docker image inspect --format '{{.Id}}' "$replacement")
    replacement_major=$("${RUN[@]}" --entrypoint postgres "$replacement" --version); replacement_major=${replacement_major##* }; replacement_major=${replacement_major%%.*}
    [[ $major == 18 && $replacement_major == "$major" ]] || { error 'Verified Postgres image majors differ or are unsupported; repair the original image reference before retrying.'; return 1; }
    pgdata=$(docker image inspect --format '{{range .Config.Env}}{{println .}}{{end}}' "$replacement" | sed -n 's/^PGDATA=//p')
    observed=("$pg" "$app")
    project=$(docker inspect --format '{{index .Config.Labels "com.docker.compose.project"}}' "$pg") || return 1
    ids=$(docker ps -aq --no-trunc --filter "label=com.docker.compose.project=$project" --filter 'label=com.docker.compose.oneoff=False') || return 1
    while IFS= read -r id; do
        [[ -z $id || $id == "$pg" || $id == "$app" ]] || observed+=("$id")
    done <<< "$ids"
    { docker inspect "${observed[@]}"; "${COMPOSE[@]}" config --format json; } | "${RUN[@]}" -i --entrypoint roundtable "$helper_image" admin adopt-check "$major" "$pgdata"
}
guard() {
    local mode=${1:-running}
    if [[ $mode == recovery && ! -f .neo-identity ]]; then error 'Cannot prove recovery identity: restore the recorded .neo-identity and original Compose configuration first. Do not recreate the database.'; return 1; fi
    identity "$mode" > "$STAGING/current.identity" || { error 'Identity could not be proven. Repair the original Compose project, image and mounts, then retry; do not recreate the database.'; return 1; }
    if [[ -f .neo-identity ]]; then cmp -s .neo-identity "$STAGING/current.identity" || { error 'Recorded data identity differs; nothing started or replaced. Repair the original Compose project/mounts before retrying.'; return 1; }; fi
    local pg project legacy mounted state
    pg=$("${COMPOSE[@]}" ps -aq postgres)
    if [[ $mode == recovery ]]; then
        if [[ $(docker inspect --format '{{.State.Running}}' "$pg") != true ]]; then
            docker start "$pg" >/dev/null
        fi
        local deadline=$((SECONDS+60))
        until docker exec "$pg" pg_isready -q; do
            ((SECONDS<deadline)) || { error 'Verified Postgres did not become ready. Repair its startup using docker logs; do not recreate it or change its mounts.'; return 1; }
            sleep 1
        done
    fi
    project=$(docker inspect --format '{{index .Config.Labels "com.docker.compose.project"}}' "$pg")
    legacy=${project}_roundtable-postgres-data
    mounted=$(docker inspect --format '{{range .Mounts}}{{if eq .Type "volume"}}{{println .Name}}{{end}}{{end}}' "$pg")
    if ! grep -Fxq "$legacy" <<< "$mounted" && docker volume inspect "$legacy" >/dev/null 2>&1; then
        # Do not start, traverse or copy an unused cluster to guess its contents.
        # An empty current database plus a retained previous volume needs the
        # operator's evidence before any service/data replacement.
        state=$(helper data-state) || { error 'Cannot inspect the current database; nothing replaced.'; return 1; }
        [[ $state == populated ]] || { error "Current database is empty and unused legacy volume $legacy exists; inspect it before continuing. Nothing replaced."; return 1; }
    fi
}
locked() { exec 9>"$ROOT/.neo-lock"; flock -n 9 || error 'Another neo-admin operation is running.'; }
backup_now() {
    helper capacity "${DOCKER_DEVICE:-0}" "${IMAGE_RESERVE_BYTES:-0}"
    helper backup | tee "$STAGING/backup.log"
    backup_file=$(sed -n 's/^Backup completed: //p' "$STAGING/backup.log" | tail -n 1)
    [[ $backup_file == /backups/roundtable-*.tar.gz && $backup_file != *..* && $backup_file != *$'\n'* ]] || error 'Backup did not report a valid archive.'
}
recovery_guidance() {
    local schema previous archive
    echo 'Application failed to become healthy.' >&2
    if [[ -f .neo-previous/schema && -f .neo-previous/backup ]]; then
        IFS= read -r previous < .neo-previous/schema
        IFS= read -r archive < .neo-previous/backup
        if schema=$(helper schema) && [[ $schema == "$previous" ]]; then
            echo 'Run: ./neo-admin rollback' >&2
        else printf 'Run: ./neo-admin restore %q\n' "$archive" >&2; fi
    else echo 'Run: ./neo-admin logs' >&2; fi
}
health() {
    local mode=${1:-replace} service
    local -a services=() options=()
    if [[ $mode == recovery ]]; then
        options+=(--no-deps)
        while IFS= read -r service; do
            if [[ $service != postgres && -n $("${COMPOSE[@]}" ps -aq "$service") ]]; then services+=("$service"); fi
        done < <("${COMPOSE[@]}" config --services)
        [[ ${#services[@]} -gt 0 ]] || { error 'No retained application containers; repair the original Compose configuration.'; return 1; }
    fi
    if ! "${COMPOSE[@]}" up -d --no-build --pull never --wait --wait-timeout 180 "${options[@]}" "${services[@]}"; then recovery_guidance; return 1; fi
    # Healthz is served on both origins; no user data or credentials are sent.
    local deadline=$((SECONDS+180))
    until helper health http://roundtable:8080/healthz; do
        ((SECONDS<deadline)) || { recovery_guidance; return 1; }
        sleep 2
    done
    helper status || { recovery_guidance; return 1; }
}
save_log() {
    local app; app=$("${COMPOSE[@]}" ps -aq roundtable)
    [[ -z $app ]] || docker logs --tail 5000 "$app" > "$ROOT/logs/previous-container.log" 2>&1
}
install_command() {
    terminal
    command -v docker >/dev/null || { error 'Please install Docker properly https://docs.docker.com/engine/install/debian/'; return 1; }
    latest; host_checks install
    local token password username rt_domain neo_domain
    question token 'Access token' 1; verify_token
    question username 'Admin user'; [[ $username =~ ^[a-zA-Z][a-zA-Z0-9_-]{1,63}$ ]] || error 'Invalid admin username.'
    question password 'Admin password (at least 12 characters)' 1; [[ ${#password} -ge 12 ]] || error 'Password must be at least 12 characters.'
    question rt_domain 'Neo Roundtable domain'; question neo_domain 'Neo app domain'
    # Recheck immediately before the first persistent write.
    host_checks install
    fetch "${manifest[admin_url]}" "$STAGING/neo-admin"; verify_file "$STAGING/neo-admin" "${manifest[admin_sha256]}"
    fetch "${manifest[compose_url]}" "$STAGING/compose.yml"; verify_file "$STAGING/compose.yml" "${manifest[compose_sha256]}"
    # The first image supplies the Neo-specific validator/config writer. This
    # bootstrap pull happens after all answers, using disposable auth only.
    docker pull --platform linux/amd64 "$helper_image"
    printf '%s\n%s\n' "$rt_domain" "$neo_domain" | "${RUN[@]}" -i --entrypoint roundtable "$helper_image" admin validate-domains
    host_checks install
    mkdir -p "$ROOT"; chmod 700 "$ROOT"
    printf '%s\n%s\n' "$rt_domain" "$neo_domain" | "${RUN[@]}" -i -v "$ROOT:/target" --entrypoint roundtable "$helper_image" admin init /target "$(id -u)" "$(id -g)"
    cp "$STAGING/compose.yml" "$ROOT/compose.yml"; cp "$STAGING/neo-admin" "$ROOT/neo-admin"; chmod 700 "$ROOT/neo-admin"
    login "$ROOT/vault/docker-auth"; export DOCKER_CONFIG=$ROOT/vault/docker-auth; unset token
    pull_release
    cp "$STAGING/release.manifest" "$ROOT/release.manifest"
    export_images; setup_compose
    make_override "$ROOT/.neo-images.yml"; setup_compose; locked
    "${COMPOSE[@]}" up -d --no-build --pull never --wait postgres
    helper preflight
    health
    printf '%s\n%s\n' "$password" "$password" | "${COMPOSE[@]}" exec -T roundtable roundtable admin create-admin "$username"
    unset password
    identity > .neo-identity
    printf 'The server runs on 127.0.0.1:3060; put your preferred reverse proxy in front of it.\n'
}
update_command() {
    local operation=$1 backup_file old_schema
    latest
    if [[ $operation == update && -f $ROOT/release.manifest && ! -f $ROOT/.neo-rolled-back ]] && cmp -s "$ROOT/release.manifest" "$STAGING/release.manifest"; then echo 'Already current; nothing to update.'; return; fi
    host_checks update
    local token
    if [[ $operation == adopt && ! -f $ROOT/vault/docker-auth/config.json ]]; then
        terminal; question token 'Access token' 1; verify_token
    else export DOCKER_CONFIG=$ROOT/vault/docker-auth; fi
    fetch "${manifest[admin_url]}" "$STAGING/neo-admin"; verify_file "$STAGING/neo-admin" "${manifest[admin_sha256]}"
    # Pull only the new helper before checks/backup. The old stack stays intact.
    docker pull --platform linux/amd64 "$helper_image"
    setup_compose; locked; guard
    old_schema=$(helper schema); backup_now
    image_space
    pull_release
    helper preflight
    guard
    if [[ -n ${token:-} ]]; then login "$ROOT/vault/docker-auth"; export DOCKER_CONFIG=$ROOT/vault/docker-auth; unset token; fi
    mkdir -p logs .neo-previous
    save_log
    if [[ -f release.manifest ]]; then cp release.manifest .neo-previous/release.manifest; fi
    if [[ -f .neo-images.yml ]]; then cp .neo-images.yml .neo-previous/images.yml; else
        # Adopted source builds may have no registry digest: retain exact local IDs.
        local service image
        printf 'services:\n' > .neo-previous/images.yml
        for service in postgres roundtable ocr transcription contacts-workshop; do
            image=$(docker inspect --format '{{.Image}}' "$("${COMPOSE[@]}" ps -aq "$service")" 2>/dev/null || true)
            [[ -z $image ]] || printf '  %s:\n    image: %s\n    pull_policy: never\n' "$service" "$image" >> .neo-previous/images.yml
        done
    fi
    printf '%s\n' "$old_schema" > .neo-previous/schema
    printf '%s\n' "$backup_file" > .neo-previous/backup
    cp "$STAGING/current.identity" .neo-identity
    # First adoption must retain an operable host launcher even if serve fails.
    if [[ ! -f release.manifest ]]; then
        cp "$STAGING/release.manifest" release.manifest
        cp "$STAGING/neo-admin" neo-admin; chmod 700 neo-admin
        touch .neo-rolled-back
    fi
    make_override "$STAGING/images.yml"; mv "$STAGING/images.yml" .neo-images.yml
    # The original Compose file, .env and all mount sources remain intact.
    setup_compose; guard; health
    cp "$STAGING/release.manifest" release.manifest
    cp "$STAGING/neo-admin" neo-admin.new; chmod 700 neo-admin.new; mv neo-admin.new neo-admin
    rm -f .neo-rolled-back
    echo 'Update complete.'
}
rollback_command() {
    cd "$ROOT"
    [[ -f .neo-previous/schema && -f .neo-previous/backup && -f .neo-previous/images.yml ]] || { error 'Nothing to roll back to.'; return 1; }
    setup_compose; locked
    COMPOSE+=(-f "$ROOT/.neo-previous/images.yml")
    guard recovery
    local schema previous backup_file
    schema=$(helper schema); IFS= read -r previous < .neo-previous/schema; IFS= read -r backup_file < .neo-previous/backup
    [[ $schema == "$previous" ]] || { printf 'Schema changed. Run: ./neo-admin restore %q\n' "$backup_file" >&2; return 1; }
    cp .neo-previous/images.yml .neo-images.yml; setup_compose; health recovery
    if [[ -f .neo-previous/release.manifest ]]; then cp .neo-previous/release.manifest release.manifest; else touch .neo-rolled-back; fi
}
validate_purge_plan() {
    local project=$1 real_root type source resolved component owner owners id failed=0 covered i j
    local -a types=() sources=()
    real_root=$(realpath -e -- "$ROOT") || return 1
    [[ $real_root != / ]] || { error 'Unsafe installation root; purge refused.'; return 1; }
    while IFS=$'\t' read -r type source; do
        if [[ $type == bind ]]; then
            resolved=$(realpath -e -- "$source") || resolved=
            component=$source
            while [[ $component == */ && $component != / ]]; do component=${component%/}; done
            while [[ $component != / && $component != . ]]; do
                if [[ -L $component ]]; then resolved=; break; fi
                component=$(dirname -- "$component") || return 1
            done
            if [[ $source != /* || ! -d $source || -z $resolved || $resolved != "$real_root/"* ]]; then
                printf '%q: kept, delete by hand (external, symlinked or unresolved bind); purge refused.\n' "$source" >&2
                failed=1; continue
            fi
            source=$resolved
        elif [[ $type == volume ]]; then
            owner=$(docker volume inspect --format '{{index .Labels "com.docker.compose.project"}}' "$source") || return 1
            [[ $owner == "$project" ]] || { error "Volume $source is not project-owned; kept, delete by hand. Purge refused."; failed=1; continue; }
            owners=$(docker ps -a --filter "volume=$source" --format '{{.ID}} {{.Label "com.docker.compose.project"}}') || return 1
            while read -r id owner; do
                [[ -z $id || $owner == "$project" ]] || { error "Volume $source is used outside this project; kept, delete by hand. Purge refused."; failed=1; }
            done <<< "$owners"
        else error 'Unknown purge mount type; purge refused.'; return 1; fi
        types+=("$type"); sources+=("$source")
    done < "$STAGING/purge.candidates"
    [[ $failed == 0 ]] || return 1
    # Only collapse nested bind paths after every source has passed validation.
    : > "$STAGING/purge.plan"
    for i in "${!sources[@]}"; do
        covered=0
        if [[ ${types[i]} == bind ]]; then
            for j in "${!sources[@]}"; do
                if [[ ${types[j]} == bind && ${sources[i]} == "${sources[j]}/"* ]]; then covered=1; break; fi
            done
        fi
        [[ $covered == 1 ]] || printf '%s\t%s\n' "${types[i]}" "${sources[i]}" >> "$STAGING/purge.plan"
    done
}
main() {
    local operation=${1:-help}; shift || true
    staging
    case "$operation" in
        install) [[ $# == 0 ]]; install_command ;;
        check)
            [[ $# == 0 ]]; latest
            if [[ -f $ROOT/release.manifest || -f $ROOT/compose.yml || -f $ROOT/docker-compose.yml ]]; then host_checks update; else host_checks install; fi ;;
        update|adopt) [[ $# == 0 ]]; update_command "$operation" ;;
        help|--help|-h) echo 'neo-admin install|check|status|update|rollback|backup|restore <archive>|reset-pwd <user>|logs|uninstall [--purge]|adopt' ;;
        status|backup|rollback|restore|reset-pwd|logs|uninstall)
            [[ -f $ROOT/release.manifest ]] || { error 'No managed release; use adopt.'; return 1; }
            pin_docker
            read_manifest "$ROOT/release.manifest"; helper_image=${manifest[image_roundtable]}; export DOCKER_CONFIG=$ROOT/vault/docker-auth
            setup_compose
            case "$operation" in
                status)
                    helper status
                    local app; app=$("${COMPOSE[@]}" ps -aq roundtable)
                    [[ -z $app ]] || { printf 'Running image '; docker inspect --format '{{.Image}}' "$app"; printf 'Running build metadata (version, revision):\n'; docker exec "$app" cat /usr/local/share/roundtable-build-info 2>/dev/null || echo unavailable; }
                    "${COMPOSE[@]}" ps ;;
                logs) "${COMPOSE[@]}" logs --tail 200 ;;
                rollback) rollback_command ;;
                backup) locked; guard; backup_now ;;
                reset-pwd) [[ $# == 1 ]]; locked; guard; "${COMPOSE[@]}" exec roundtable roundtable admin reset-pwd "$1" ;;
                restore)
                    [[ $# == 1 && $1 == /backups/roundtable-*.tar.gz && $1 != *..* ]] || { error 'Use the /backups/roundtable-*.tar.gz path printed by backup.'; return 1; }
                    locked
                    # Restore with the image associated with the backup when reverting a migration.
                    if [[ -f .neo-previous/backup && $(cat .neo-previous/backup) == "$1" ]]; then
                        cp .neo-previous/images.yml "$STAGING/restore.yml"
                    else cp .neo-images.yml "$STAGING/restore.yml"; fi
                    COMPOSE+=(-f "$STAGING/restore.yml")
                    guard recovery; helper capacity
                    local -a writers=(roundtable)
                    if "${COMPOSE[@]}" config --services | grep -Fxq contacts-workshop; then writers+=(contacts-workshop); fi
                    "${COMPOSE[@]}" stop "${writers[@]}"
                    "${COMPOSE[@]}" -f "$STAGING/restore.yml" run --rm -T --no-deps --entrypoint roundtable roundtable restore --app-stopped "$1"
                    cp "$STAGING/restore.yml" .neo-images.yml
                    if [[ -f .neo-previous/release.manifest && $(cat .neo-previous/backup) == "$1" ]]; then cp .neo-previous/release.manifest release.manifest; fi
                    setup_compose; health recovery ;;
                uninstall)
                    locked; guard
                    if [[ ${1:-} == --purge ]]; then
                        helper purge-plan < .neo-identity > "$STAGING/purge.candidates"
                        local type source project
                        project=$("${COMPOSE[@]}" config --format json | "${RUN[@]}" -i --entrypoint roundtable "$helper_image" admin compose-project)
                        validate_purge_plan "$project"
                        terminal; local answer; question answer 'Permanently delete this installation and its data? Type DELETE'
                        [[ $answer == DELETE ]] || { error 'Purge cancelled.'; return 1; }
                        "${COMPOSE[@]}" down
                        while IFS=$'\t' read -r type source; do
                            if [[ $type == volume ]]; then docker volume rm "$source"
                            else "${RUN[@]}" --mount "type=bind,src=$source,dst=/erase" --entrypoint roundtable "$helper_image" admin purge-mount /erase; fi
                        done < "$STAGING/purge.plan"
                        if [[ -f .neo-fresh ]]; then "${RUN[@]}" -v "$ROOT:/target" --entrypoint roundtable "$helper_image" admin purge /target; fi
                        echo 'Recorded installation data purged; source checkout (if any) kept.'
                    elif [[ $# == 0 ]]; then "${COMPOSE[@]}" down; echo 'Containers removed; data, keys and backups kept.'
                    else error 'Usage: neo-admin uninstall [--purge]'; fi ;;
            esac ;;
        *) error 'Unknown command; run neo-admin help.' ;;
    esac
}
if [[ ${BASH_SOURCE[0]} == "$0" ]]; then main "$@"; fi
