#!/usr/bin/env bash

set -euo pipefail

if [[ $# -lt 1 || $# -gt 2 ]]; then
  echo "Usage: $0 PROTOMAPS_SOURCE_URL [OUTPUT_FILE]" >&2
  exit 2
fi

if ! command -v pmtiles >/dev/null 2>&1; then
  echo "The pmtiles CLI is required: https://docs.protomaps.com/pmtiles/cli" >&2
  exit 1
fi

source_url=$1
output_file=${2:-tiles/mandan.pmtiles}

if [[ -e "$output_file" ]]; then
  echo "Refusing to overwrite existing file: $output_file" >&2
  exit 1
fi

# Mandan city plus a modest margin for labels and roads. PMTiles extracts
# tile-aligned bounds, so the resulting archive includes some nearby context.
pmtiles extract \
  "$source_url" \
  "$output_file" \
  --bbox=-101.05,46.74,-100.75,46.93

pmtiles show "$output_file"
