#!/usr/bin/env bash
# Check published installer bytes and install the complete suite in a fresh root.
set -euo pipefail
[[ $# == 1 && $1 =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]] || { echo 'Usage: bin/verify-release <vMAJOR.MINOR.PATCH>' >&2; exit 1; }
tag=$1
if [[ ${ICLOUD_ARCH_ROOT:-} != 1 ]]; then
  exec sudo "$(dirname "$0")/arch-run" bin/verify-release "$tag"
fi
cd "$(dirname "$0")/.."
source packaging/PKGBUILD
download=$url/releases/download/$tag
work=$(mktemp -d)
trap 'rm -rf "$work"' EXIT
bin/make-installers "$work/expected"
for expected in install.sh "$work/expected/"*.sh; do
  name=$(basename "$expected")
  curl -fsSL "$download/$name" -o "$work/$name"
  cmp "$expected" "$work/$name"
done
ICLOUD_RELEASES="$download" bash "$work/install.sh"
for app in "${pkgname[@]}"; do
  [[ $(pacman -Q "$app") == "$app ${tag#v}-$pkgrel" ]]
  echo "Verified: $app ${tag#v} on $(uname -m)"
done
