#!/usr/bin/env bash
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
IMAGE_NAME="cadroute-builder-linux"

echo "[cadroute] Building Docker image..."
docker build -t "$IMAGE_NAME" "$SCRIPT_DIR"

echo "[cadroute] Running Linux build..."
docker run --rm \
  --device /dev/fuse \
  --cap-add SYS_ADMIN \
  -v "$PROJECT_ROOT":/project \
  -w /project \
  -v cadroute-electron-cache:/root/.cache/electron \
  -v cadroute-electronbuilder-cache:/root/.cache/electron-builder \
  -v /etc/ssl/certs/ca-certificates.crt:/etc/ssl/certs/ca-certificates.crt:ro \
  -e SSL_CERT_FILE=/etc/ssl/certs/ca-certificates.crt \
  -e NODE_EXTRA_CA_CERTS=/etc/ssl/certs/ca-certificates.crt \
  "$IMAGE_NAME" \
  bash -c "npm ci && npm run build -- --linux --x64"

echo "[cadroute] Artefatti in: $PROJECT_ROOT/dist"
