#!/usr/bin/env nix-shell
#!nix-shell -i bash -p coreutils curl gitMinimal gzip gnugrep gnused xmlformat yq

set -euo pipefail
set -x

XML_URL='https://monoroch.net/kinshi/housouKinshiYougo.xml'
TMP="$(mktemp -d)"

PROJECT_ROOT="$(git rev-parse --show-toplevel)"
DATA_DIR="$PROJECT_ROOT/housou-kinshi-yougo"

function cleanup {
  rm -rf "$TMP"
}
trap cleanup EXIT

curl -L -o "$TMP/housou-kinshi-yougo.xml" "$XML_URL"
xmlformat "$TMP/housou-kinshi-yougo.xml" > "$DATA_DIR/housou-kinshi-yougo.xml"

xq -r '.housouKinshiYougoList."@date"' "$DATA_DIR/housou-kinshi-yougo.xml" > "$DATA_DIR/version.txt"

cleanup
