#!/bin/sh

## app-versioning: platform-partitioned Build Info Script (vendored for Workouts)
##
## A vendored variant of the shared app-versioning script (see
## ../indie-skills/skills/app-versioning/scripts/update_build_info.sh). Workouts'
## iOS app and Mac app share ONE App Store Connect record (same bundle id,
## dev.rzen.indie.Workouts — universal purchase), and therefore one build-number
## sequence: stamping both with the raw commit count would make the second
## upload collide ("bundle version already used"). Partition the space instead:
##   - iOS + watchOS -> EVEN build numbers (commit-count * 2)
##   - macOS         -> ODD  build numbers (commit-count * 2 + 1)
## watchOS is grouped with iOS (not given its own parity) because the embedded
## watch app's CFBundleVersion must match its host iOS app's exactly. The
## platform is read from PLATFORM_NAME, so this one script serves every
## code-bearing target (iOS app, watch app, watch widget, iOS widget, Mac app)
## via a single postBuildScripts anchor — no per-target parameters needed.
##
## Sets, on every build, in both the target Info.plist and its dSYM:
##   - CFBundleVersion = commit-count*2 (iOS/watchOS) or commit-count*2+1 (macOS)
##   - BuildDate       = today's date (for IndieAbout's "Built …" line)
##   - BuildHash       = short git commit hash
##
## Wire as a POST-BUILD Run Script phase with these Input Files:
##   $(TARGET_BUILD_DIR)/$(INFOPLIST_PATH)
##   $(DWARF_DSYM_FOLDER_PATH)/$(DWARF_DSYM_FILE_NAME)/Contents/Info.plist
##
## Release tagging is opt-in, not automatic. Set RELEASE_TAGGING=1 in the
## build environment (e.g. `RELEASE_TAGGING=1 xcodebuild ...`) to have this
## script run `git tag <marketing-version>`. Default builds never tag.
set -eu

git=$(sh /etc/profile; which git || true)
if [ -z "$git" ] || [ ! -x "$git" ]; then
  echo "error: update_build_info.sh: could not resolve a 'git' executable — refusing to stamp a build number." >&2
  exit 1
fi

number_of_commits=$("$git" rev-list HEAD --count)
case "$number_of_commits" in
  ''|*[!0-9]*)
    echo "error: update_build_info.sh: 'git rev-list HEAD --count' returned '$number_of_commits', not a positive integer — refusing to stamp a build number." >&2
    exit 1
    ;;
esac
if [ "$number_of_commits" -le 0 ]; then
  echo "error: update_build_info.sh: 'git rev-list HEAD --count' returned $number_of_commits — refusing to stamp a build number." >&2
  exit 1
fi

git_commit=$("$git" rev-parse --short HEAD)

# Platform-aware build number (see header): iOS/watchOS even, macOS odd.
case "${PLATFORM_NAME:-}" in
  macosx*) build_number=$((number_of_commits * 2 + 1)) ;;
  *)       build_number=$((number_of_commits * 2)) ;;
esac

target_plist="$TARGET_BUILD_DIR/$INFOPLIST_PATH"
dsym_plist="$DWARF_DSYM_FOLDER_PATH/$DWARF_DSYM_FILE_NAME/Contents/Info.plist"

bundle_version=$(/usr/libexec/PlistBuddy -c "Print :CFBundleShortVersionString" "$target_plist" 2>/dev/null || true)
build_date=$(date +%F)

echo "app-versioning: v$bundle_version-$git_commit b$build_number (${PLATFORM_NAME:-}) $build_date"

if [ "${RELEASE_TAGGING:-}" = "1" ]; then
  if [ -n "$bundle_version" ]; then
    "$git" tag "$bundle_version"
    echo "app-versioning: tagged release $bundle_version"
  else
    echo "warning: app-versioning: RELEASE_TAGGING=1 but CFBundleShortVersionString could not be read from $target_plist; skipping git tag"
  fi
fi

if [ ! -f "$target_plist" ] && [ ! -f "$dsym_plist" ]; then
  echo "warning: update_build_info.sh: neither the target Info.plist ($target_plist) nor the dSYM Info.plist ($dsym_plist) exists — check that both are wired as Input Files on this Run Script build phase"
fi

for plist in "$target_plist" "$dsym_plist"; do
  if [ -f "$plist" ]; then
    /usr/libexec/PlistBuddy -c "Set :CFBundleVersion $build_number" "$plist"

    /usr/libexec/PlistBuddy -c "Set :BuildDate $build_date" "$plist" 2>/dev/null || \
    /usr/libexec/PlistBuddy -c "Add :BuildDate string $build_date" "$plist"

    /usr/libexec/PlistBuddy -c "Set :BuildHash $git_commit" "$plist" 2>/dev/null || \
    /usr/libexec/PlistBuddy -c "Add :BuildHash string $git_commit" "$plist"

    echo "app-versioning: updated $plist"
  fi
done
