#!/bin/sh

set -eu

PATH=/usr/bin:/usr/sbin:/bin:/sbin
export PATH

usage() {
    cat <<'EOF'
Usage:
  forgeos-app status
  forgeos-app install NAME|FLAKE...
  forgeos-app remove PACKAGE...
  forgeos-app list
  forgeos-app search TERMS...
  forgeos-app upgrade [PACKAGE...]
  forgeos-app rollback
  forgeos-app shell NAME|FLAKE...
  forgeos-app gc

Short names are expanded to nixpkgs#NAME for install and shell.
EOF
}

as_installable() {
    case "$1" in
        *#*|/*|git+*|http://*|https://*)
            printf '%s\n' "$1"
            ;;
        *)
            printf 'nixpkgs#%s\n' "$1"
            ;;
    esac
}

show_mount() {
    awk -v target="$1" '
        $2 == target {
            printf "%-12s <- %s (%s, %s)\n", target, $1, $3, $4
            found = 1
        }
        END {
            if (!found) {
                printf "%-12s <- not mounted\n", target
            }
        }
    ' /proc/mounts
}

find_nix() {
    if command -v nix >/dev/null 2>&1; then
        command -v nix
        return 0
    fi

    if [ -x /nix/var/nix/profiles/default/bin/nix ]; then
        printf '%s\n' /nix/var/nix/profiles/default/bin/nix
        return 0
    fi

    for candidate in /nix/store/*-nix-*/bin/nix; do
        [ -x "$candidate" ] || continue
        printf '%s\n' "$candidate"
        return 0
    done

    return 1
}

diagnose_missing_nix() {
    echo "nix is unavailable; is the ForgeOS app layer mounted?" >&2
    show_mount /forge >&2
    show_mount /nix >&2
    echo "check: lsblk -f" >&2
    echo "check: systemctl status forge.mount forgeos-nix-bootstrap.service nix-daemon.service nix-daemon.socket" >&2
    echo "repair as root: mount LABEL=FORGE_APPS /forge && mount -a && systemctl restart forgeos-nix-bootstrap.service nix-daemon.socket nix-daemon.service" >&2
}

need_nix() {
    if NIX_BIN=$(find_nix); then
        export NIX_BIN
        export NIX_REMOTE=${NIX_REMOTE:-daemon}
        return 0
    fi

    diagnose_missing_nix
    exit 1
}

run_nix() {
    need_nix
    "$NIX_BIN" "$@"
}

cmd=${1:-}
if [ -z "$cmd" ]; then
    usage
    exit 2
fi
shift || true

case "$cmd" in
    status)
        show_mount /
        show_mount /forge
        show_mount /nix
        show_mount /home
        show_mount /root
        show_mount /var
        show_mount /opt
        show_mount /usr/local
        df -h /forge /nix /home 2>/dev/null || true
        ;;
    install)
        [ "$#" -gt 0 ] || {
            usage >&2
            exit 2
        }
        for item in "$@"; do
            run_nix profile install "$(as_installable "$item")"
        done
        ;;
    remove)
        [ "$#" -gt 0 ] || {
            usage >&2
            exit 2
        }
        run_nix profile remove "$@"
        ;;
    list)
        run_nix profile list
        ;;
    search)
        [ "$#" -gt 0 ] || {
            usage >&2
            exit 2
        }
        run_nix search nixpkgs "$@"
        ;;
    upgrade)
        if [ "$#" -eq 0 ]; then
            run_nix profile upgrade '.*'
        else
            run_nix profile upgrade "$@"
        fi
        ;;
    rollback)
        run_nix profile rollback
        ;;
    shell)
        [ "$#" -gt 0 ] || {
            usage >&2
            exit 2
        }
        need_nix
        set -- "$@"
        expanded=
        for item in "$@"; do
            expanded="${expanded}${expanded:+ }$(as_installable "$item")"
        done
        need_nix
        # shellcheck disable=SC2086
        exec "$NIX_BIN" shell $expanded
        ;;
    gc)
        run_nix store gc
        ;;
    help|--help|-h)
        usage
        ;;
    *)
        usage >&2
        exit 2
        ;;
esac
