#!/bin/bash
set -euo pipefail

if [[ "${GITHUB_REF_TYPE:-}" == "tag" && -n "${GITHUB_REF_NAME:-}" ]]; then
	__version="${GITHUB_REF_NAME#v}"
else
	if [[ -n "${GITHUB_SHA:-}" ]]; then
		__commit="${GITHUB_SHA:0:8}"
	else
		__commit="$(git rev-parse --short=8 HEAD 2>/dev/null || echo 'dev')"
	fi
	__version="0.0.0-${__commit}"
fi

# CREATE_POD_SPECS_DEBUG lets the unit test (and local runs) inspect the computed
# version without writing any podspec files.
if [[ ${CREATE_POD_SPECS_DEBUG:-0} == 1 ]]; then
	echo "$__version"
	exit 0
fi

for name in libcrypto libevent libssl libtor libz oonimkall; do
	sed -e "s/@VERSION@/$__version/g" \
		"./MOBILE/ios/${name}-template.podspec" >"./MOBILE/ios/${name}.podspec"
done
