#!/bin/sh
# Source with REPO_ROOT set. Exports the same release metadata as Gradle/Xcode.
YOKAN_VERSION="${YOKAN_VERSION:-$(awk '$1 == "YOKAN_VERSION" && $2 == "=" {print $3; exit}' "$REPO_ROOT/version.xcconfig")}"
YOKAN_VERSION="${YOKAN_VERSION#v}"
if ! printf '%s\n' "$YOKAN_VERSION" | awk -F. '
  NF != 3 { exit 1 }
  { for (i = 1; i <= 3; i++) if ($i !~ /^(0|[1-9][0-9]*)$/) exit 1 }
  $1 > 2099 || $2 > 999 || $3 > 999 { exit 1 }
'; then
  echo "error: Yokan version must be MAJOR.MINOR.PATCH within platform version-code limits" >&2
  exit 2
fi
YOKAN_RELEASE_CHANNEL="$(printf '%s\n' "$YOKAN_VERSION" | awk -F. '{print ($1 > 0 ? "stable" : ($2 > 0 ? "beta" : "alpha"))}')"
YOKAN_VERSION_CODE="${YOKAN_VERSION_CODE:-$(printf '%s\n' "$YOKAN_VERSION" | awk -F. '{code = $1 * 1000000 + $2 * 1000 + $3; printf "%.0f", (code > 0 ? code : 1)}')}"
if ! printf '%s\n' "$YOKAN_VERSION_CODE" | awk '$0 !~ /^[1-9][0-9]*$/ || $0 > 2100000000 {exit 1}'; then
  echo "error: Yokan versionCode must be between 1 and 2100000000" >&2
  exit 2
fi
export YOKAN_VERSION YOKAN_RELEASE_CHANNEL YOKAN_VERSION_CODE
