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

PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
TILE_ID="${AGRARIAN_TILE_ID:-gz_us_ca_pacifica_utm10n_e544_n4160}"
PACKAGE_VERSION="${AGRARIAN_TILE_PACKAGE_VERSION:-0}"
BUILD_ROOT="${PROJECT_ROOT}/BuildArtifacts/TileDelivery"
PUBLIC_ROOT="${BUILD_ROOT}/public"
PACKAGE_ROOT="${PUBLIC_ROOT}/tiles/${TILE_ID}/v${PACKAGE_VERSION}"
ARCHIVE_PATH="${BUILD_ROOT}/agrarian-ground-zero-tile-delivery.tar.gz"

log() {
  printf '[agrarian-tile-package] %s\n' "$*"
}

require_file() {
  local path="$1"
  if [[ ! -f "${path}" ]]; then
    printf 'Missing required file: %s\n' "${path}" >&2
    exit 1
  fi
}

REGISTRY="${PROJECT_ROOT}/Data/Tiles/ground_zero_tiles.json"
SCHEMA="${PROJECT_ROOT}/Data/Tiles/tile_registry.schema.json"
HEIGHTMAP="${PROJECT_ROOT}/Data/Terrain/Unreal/${TILE_ID}/${TILE_ID}_unreal_1009.r16"
HEIGHTMAP_META="${PROJECT_ROOT}/Data/Terrain/Unreal/${TILE_ID}/${TILE_ID}_unreal_heightmap_metadata.json"
TERRAIN_META="${PROJECT_ROOT}/Data/Terrain/Generated/${TILE_ID}/${TILE_ID}_terrain_metadata.json"
LANDFORM="${PROJECT_ROOT}/Data/Terrain/Analysis/${TILE_ID}/${TILE_ID}_landform_analysis.json"
WATER="${PROJECT_ROOT}/Data/Terrain/Analysis/${TILE_ID}/${TILE_ID}_water_shoreline_analysis.json"
EDGES="${PROJECT_ROOT}/Data/Terrain/Analysis/${TILE_ID}/${TILE_ID}_neighbor_edge_verification.json"

require_file "${REGISTRY}"
require_file "${SCHEMA}"
require_file "${HEIGHTMAP}"
require_file "${HEIGHTMAP_META}"
require_file "${TERRAIN_META}"
require_file "${LANDFORM}"
require_file "${WATER}"
require_file "${EDGES}"

rm -rf "${BUILD_ROOT}"
mkdir -p "${PACKAGE_ROOT}/terrain" "${PUBLIC_ROOT}/schemas"

cp "${REGISTRY}" "${PUBLIC_ROOT}/ground_zero_tiles.json"
cp "${SCHEMA}" "${PUBLIC_ROOT}/schemas/tile_registry.schema.json"
cp "${HEIGHTMAP}" "${PACKAGE_ROOT}/terrain/"
cp "${HEIGHTMAP_META}" "${PACKAGE_ROOT}/terrain/"
cp "${TERRAIN_META}" "${PACKAGE_ROOT}/terrain/"
cp "${LANDFORM}" "${PACKAGE_ROOT}/terrain/"
cp "${WATER}" "${PACKAGE_ROOT}/terrain/"
cp "${EDGES}" "${PACKAGE_ROOT}/terrain/"

cat > "${PUBLIC_ROOT}/manifest.json" <<JSON
{
  "service": "agrarian-tile-delivery",
  "schema_version": 1,
  "environment": "mvp",
  "default_tile_id": "${TILE_ID}",
  "tiles_registry": "/ground_zero_tiles.json",
  "tile_packages": [
    {
      "tile_id": "${TILE_ID}",
      "package_version": ${PACKAGE_VERSION},
      "status": "mvp_static_package",
      "base_url": "/tiles/${TILE_ID}/v${PACKAGE_VERSION}/",
      "terrain": {
        "heightmap_r16": "terrain/${TILE_ID}_unreal_1009.r16",
        "heightmap_metadata": "terrain/${TILE_ID}_unreal_heightmap_metadata.json",
        "terrain_metadata": "terrain/${TILE_ID}_terrain_metadata.json",
        "landform_analysis": "terrain/${TILE_ID}_landform_analysis.json",
        "water_shoreline_analysis": "terrain/${TILE_ID}_water_shoreline_analysis.json",
        "neighbor_edge_verification": "terrain/${TILE_ID}_neighbor_edge_verification.json"
      }
    }
  ]
}
JSON

(
  cd "${PUBLIC_ROOT}"
  find . -type f ! -name SHA256SUMS -print0 | sort -z | xargs -0 sha256sum > SHA256SUMS
)

# The project usually lives on a CIFS-mounted Unraid share. Give copied files a
# short settle window before tar reads them, or GNU tar can report false
# "file changed as we read it" warnings on the network filesystem.
sleep 3

tar -C "${PUBLIC_ROOT}" -czf "${ARCHIVE_PATH}" .

log "Package complete: ${ARCHIVE_PATH}"
log "Static root: ${PUBLIC_ROOT}"
log "Manifest: ${PUBLIC_ROOT}/manifest.json"
