#!/usr/bin/env bash
#
# Derive the iOS app version from the latest git tag, mirroring the Android app
# (app/build.gradle.kts derives versionName/versionCode from
# `git describe --tags --abbrev=0`).
#
# Run this before building/generating so the git-derived version is stamped into
# the app + widget bundles:
#
#   cd iosApp
#   ./version.sh            # writes iosApp/Version.local.xcconfig
#   xcodegen generate
#
# It writes two Xcode build settings into `iosApp/Version.local.xcconfig`
# (git-ignored):
#   MARKETING_VERSION        -> CFBundleShortVersionString (the git tag, e.g. 4.0.0)
#   CURRENT_PROJECT_VERSION  -> CFBundleVersion (integer build number)
#
# `Version.xcconfig` (committed, read by every target) optionally #includes this
# file, so these values override the committed 1.0 / 1 fallbacks when present.
# Unlike the previous `${...}` XcodeGen env-substitution approach, forgetting to
# run this script no longer produces an EMPTY CFBundleVersion (which makes the
# app-extension install fail with "bundleVersion must be set in placeholder
# attributes for an app extension placeholder") — the committed fallbacks keep
# the build installable.
#
# The values are guaranteed non-empty (falling back to 1.0 / 1 when no tag is
# reachable). This script can be `source`d or executed; it exports the same two
# variables too, for backward compatibility with tooling that reads them.

# Resolve this script's directory so it works regardless of the current
# working directory (and whether sourced or executed).
_version_script_dir="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" && pwd)"

# The latest reachable tag (same command Android uses). Empty if none/no git.
_version_name="$(git -C "$_version_script_dir" describe --tags --abbrev=0 2>/dev/null || true)"
if [ -z "$_version_name" ]; then
  _version_name="1.0"
fi

# Build number = major*100000000 + minor*100000 + patch, mirroring the Android
# versionCode in app/build.gradle.kts. Missing components default to 0.
_version_major="$(printf '%s' "$_version_name" | cut -d. -f1)"
_version_minor="$(printf '%s' "$_version_name" | cut -d. -f2)"
_version_patch="$(printf '%s' "$_version_name" | cut -d. -f3)"
_version_major="${_version_major:-1}"
_version_minor="${_version_minor:-0}"
_version_patch="${_version_patch:-0}"
_version_code=$(( _version_major * 100000000 + _version_minor * 100000 + _version_patch ))

export MARKETING_VERSION="$_version_name"
export CURRENT_PROJECT_VERSION="$_version_code"

# Write the git-ignored local override that Version.xcconfig optionally includes.
_version_xcconfig="$_version_script_dir/Version.local.xcconfig"
cat > "$_version_xcconfig" <<EOF
// Generated by iosApp/version.sh — do not commit (git-ignored).
// Overrides the committed defaults in Version.xcconfig with the git-derived
// version. Re-run version.sh to refresh.
MARKETING_VERSION = $MARKETING_VERSION
CURRENT_PROJECT_VERSION = $CURRENT_PROJECT_VERSION
EOF

echo "iOS version from git: MARKETING_VERSION=$MARKETING_VERSION CURRENT_PROJECT_VERSION=$CURRENT_PROJECT_VERSION"
echo "Wrote $_version_xcconfig"

unset _version_script_dir _version_name _version_major _version_minor _version_patch _version_code _version_xcconfig
