#!/usr/bin/env bash
# Stage an independently verified release tarball; never install over a live package.
# Only program installation gets umask 022. The staging prefix remains owner-only.
set -euo pipefail
[ "$#" = 2 ] || { echo 'usage: stage-package.sh <verified-tarball> <new-staging-prefix>' >&2; exit 2; }
TARBALL=$1
PREFIX=$2
SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)
[ -f "$TARBALL" ] || { echo 'release tarball is missing' >&2; exit 1; }
case "$PREFIX" in /*) ;; *) echo 'staging prefix must be absolute' >&2; exit 1;; esac
[ ! -e "$PREFIX" ] && [ ! -L "$PREFIX" ] || { echo 'staging prefix already exists' >&2; exit 1; }
mkdir -m 0700 -- "$PREFIX"
(
    # npm combines its own mask with the process mask; set both explicitly.
    umask 022
    npm install --global --prefix "$PREFIX" --umask=022 --no-audit --no-fund "$TARBALL"
)
node "$SCRIPT_DIR/package-access.mjs" "$PREFIX/lib/node_modules/@boujot/happy-coder"
