#!/usr/bin/env bash
set -euo pipefail

ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
ARTIFACT_DIR="${1:-$ROOT/dist/release}"

if [[ ! -d "$ARTIFACT_DIR" ]]; then
  printf 'artifact directory not found: %s\n' "$ARTIFACT_DIR" >&2
  exit 1
fi

cd "$ARTIFACT_DIR"

files=()
while IFS= read -r file; do
  files+=("${file#./}")
done < <(find . -maxdepth 1 -type f ! -name SHA256SUMS | sort)

if [[ "${#files[@]}" -eq 0 ]]; then
  printf 'no release files found in %s\n' "$ARTIFACT_DIR" >&2
  exit 1
fi

shasum -a 256 "${files[@]}" > SHA256SUMS
printf 'wrote %s/SHA256SUMS\n' "$ARTIFACT_DIR"
