#!/usr/bin/env bash
#
# bin/install-deps.sh — install Composer dependencies (including the private
# egov/rabbitmq-classes, pulled over SSH from Δημόκριτος' GitLab) without any
# host PHP or Composer.
#
# Why this script exists (the two traps a fresh checkout hits):
#   1. Composer runs in a throwaway container, and that container must resolve your
#      host uid. macOS keeps users in Directory Service, NOT /etc/passwd, so mounting
#      the host /etc/passwd dies with «No user exists for uid 501» (Linux is uid 1000
#      and works by accident). We build a synthetic /etc/passwd + /etc/group from the
#      image's own files plus an entry for your uid — identical on both OSes.
#   2. egov/rabbitmq-classes is fetched over SSH (port 222) from
#      gitlab.skel.iit.demokritos.gr, so you need an SSH key registered there. This
#      script refuses to run until that key exists; the full setup is in
#      docs/runbook.md §0.
#
# Overrides (optional):
#   KED_SSH_KEY   path to the private key (default: ~/.ssh/id_ed25519_demokritos)
#   KED_GITLAB    GitLab host            (default: gitlab.skel.iit.demokritos.gr)
#
set -euo pipefail

GITLAB_HOST="${KED_GITLAB:-gitlab.skel.iit.demokritos.gr}"
SSH_KEY="${KED_SSH_KEY:-$HOME/.ssh/id_ed25519_demokritos}"

die() { echo "install-deps: $*" >&2; exit 1; }

command -v docker >/dev/null 2>&1 || die "docker not found on PATH. Install Docker Desktop (or equivalent) first."

[ -f "$SSH_KEY" ] || die "SSH key not found: $SSH_KEY

The private package egov/rabbitmq-classes is pulled over SSH from $GITLAB_HOST,
so a key registered there is mandatory. To create one and register it:

  ssh-keygen -t ed25519 -C gov-ked -f \"$SSH_KEY\" -N \"\"
  cat \"${SSH_KEY}.pub\"            # paste this public half into GitLab
  # GitLab → Edit profile → SSH Keys → Add new key

Then verify before re-running me:

  ssh -T -p 222 -i \"$SSH_KEY\" -o IdentitiesOnly=yes git@$GITLAB_HOST
  # expect: Welcome to GitLab, @<your-username>!

Full walkthrough (account, key, RSA gotcha): docs/runbook.md §0."

CACHE_DIR="$HOME/.cache/gov-ked-docker"
mkdir -p "$CACHE_DIR"

# Synthetic /etc/passwd + /etc/group = the image's own files + one entry for the host
# uid/gid. Resolves the user identically on macOS (uid 501) and Linux (uid 1000).
echo "install-deps: building /etc/passwd + /etc/group for uid $(id -u) ($(id -un))…"
docker run --rm --entrypoint cat composer:2 /etc/passwd > "$CACHE_DIR/passwd"
printf '%s:x:%s:%s:%s:/tmp:/bin/sh\n' "$(id -un)" "$(id -u)" "$(id -g)" "$(id -un)" >> "$CACHE_DIR/passwd"
docker run --rm --entrypoint cat composer:2 /etc/group > "$CACHE_DIR/group"
printf '%s:x:%s:\n' "$(id -gn)" "$(id -g)" >> "$CACHE_DIR/group"

echo "install-deps: running composer install (SSH key: $SSH_KEY)…"
echo
# KED_KEY / KED_HOST reach the inner shell via -e, so the sh -c stays cleanly
# single-quoted and the variables expand inside the container, not on the host.
if ! docker run --rm -u "$(id -u):$(id -g)" -e COMPOSER_HOME=/tmp \
      -e KED_KEY="/tmp/ssh_key" -e KED_HOST="$GITLAB_HOST" \
      -v "$SSH_KEY":/tmp/ssh_key:ro \
      -v "$CACHE_DIR/passwd":/etc/passwd:ro -v "$CACHE_DIR/group":/etc/group:ro \
      -e HOME=/tmp \
      -v "$PWD":/app -w /app composer:2 sh -c '
        set -e
        export GIT_SSH_COMMAND="ssh -i $KED_KEY -o IdentitiesOnly=yes -o UserKnownHostsFile=/tmp/known_hosts -o StrictHostKeyChecking=accept-new"
        git config --global url."ssh://git@$KED_HOST:222/".insteadOf "https://$KED_HOST/" >/dev/null
        composer install --no-interaction --ignore-platform-req=php --ignore-platform-req=ext-sockets --ignore-platform-req=ext-bcmath
      '; then
  echo
  echo "install-deps: composer install failed." >&2
  echo "Most common cause: the SSH key is not registered on $GITLAB_HOST (or is the wrong" >&2
  echo "type — use ed25519, not an old RSA key). Verify with:" >&2
  echo "  ssh -T -p 222 -i \"$SSH_KEY\" -o IdentitiesOnly=yes git@$GITLAB_HOST" >&2
  echo "and see docs/runbook.md §0." >&2
  exit 1
fi

echo
echo "install-deps: done. Next: cp .env.example .env && ./vendor/bin/sail artisan key:generate"
echo "(docs/runbook.md §0 — Environment)."
