#!/usr/bin/env sh
set -eu

cd "$(dirname "$0")"
output="./tools/sow-toolkit"

tool_sources_are_newer() {
  if [ ! -x "$output" ]; then
    return 0
  fi

  for path in go.mod go.sum cmd internal; do
    if [ ! -e "$path" ]; then
      continue
    fi
    if [ -n "$(find "$path" -type f -newer "$output" -print -quit)" ]; then
      return 0
    fi
  done

  return 1
}

mkdir -p tools .cache/go-build
if ! tool_sources_are_newer; then
  echo "sow-toolkit is up to date."
  exit 0
fi

echo "Building sow-toolkit..."
GOCACHE="${PWD}/.cache/go-build" go build -o "$output" ./cmd/nwn-tool
