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

if [[ "${CONFIGURATION:-Debug}" != "Release" ]]; then
	exit 0
fi

if [[ "${HT_IOS_DISTRIBUTION_CHANNEL:-app-store}" != "app-store" ]]; then
	echo "iOS Release build is restricted to internal distribution."
	exit 0
fi

if [[ "${IOS_PUBLIC_RELEASE_ALLOWED:-NO}" != "YES" ]]; then
	echo >&2 "Refusing App Store build: IOS_PUBLIC_RELEASE_ALLOWED is not YES."
	exit 1
fi

root="${SRCROOT:-.}"
for required in \
	"$root/LICENSE" \
	"$root/LICENSES/GPL-3.0-only.txt" \
	"$root/LICENSES/LGPL-2.1-only.txt" \
	"$root/THIRD_PARTY_NOTICES.md" \
	"$root/scripts/package-source-offer.sh"; do
	if [[ ! -s "$required" ]]; then
		echo >&2 "Refusing App Store build: missing GPL release artifact $required."
		exit 1
	fi
done

if [[ "${HTP_GPL_SOURCE_URL:-}" != https://portal.hivetechnology.net/open-source/ht-portal-vpn/*-source.tar.gz ]]; then
	echo >&2 "Refusing App Store build: HTP_GPL_SOURCE_URL is missing or invalid."
	exit 1
fi

echo "iOS App Store GPL release checks satisfied: $HTP_GPL_SOURCE_URL"
