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

set -euo pipefail
set -x

RADK_URL='http://ftp.edrdg.org/pub/Nihongo/radkfile.gz'
KRAD_URL='http://ftp.edrdg.org/pub/Nihongo/kradfile.gz'
TMP="$(mktemp -d)"

PROJECT_ROOT="$(git rev-parse --show-toplevel)"
DATA_DIR="$PROJECT_ROOT/radkfile"

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

curl -L -o "$TMP/radkfile.gz" "$RADK_URL"
gzip -dkc "$TMP/radkfile.gz" > "$TMP/radkfile"
iconv -f EUC-JP -t UTF-8 -o "$DATA_DIR/radkfile" "$TMP/radkfile"

curl -L -o "$TMP/kradfile.gz" "$KRAD_URL"
gzip -dkc "$TMP/kradfile.gz" > "$TMP/kradfile"
iconv -f EUC-JP -t UTF-8 -o "$DATA_DIR/kradfile" "$TMP/kradfile"

# Retrieve the latest line from the changelog

declare -A MONTH_MAP=(
  [Jan]="01"
  [Feb]="02"
  [Mar]="03"
  [Apr]="04"
  [May]="05"
  [Jun]="06"
  [Jul]="07"
  [Aug]="08"
  [Sep]="09"
  [Oct]="10"
  [Nov]="11"
  [Dec]="12"
)

sed -n "$DATA_DIR/kradfile" -e '/^#\+ CHANGES (some of them) #\+/,/^#\{10,\}/p' \
  | sed '1d;$d' \
  | sed 's/^# //' \
  | sed '/^[^A-Z]/d' \
  | cut -d' ' -f1-2 \
  | tail -n 1 \
  | while read -r MONTH YEAR; do
      echo "$YEAR-${MONTH_MAP[$MONTH]}"
    done \
  > "$DATA_DIR/version.txt"

cleanup
