#!/bin/bash
#
# Compile a .icon bundle using actool and update the app's Info.plist
# to support macOS Sequoia's icon tinting feature.
#
# Usage: compile_icon oss <app_bundle_path>
#   app_bundle_path: Path to the Galaxy.app bundle

set -e

if [ "$#" -ne 2 ]; then
  echo "Usage: compile_icon oss <app_bundle_path>"
    exit 1
fi

CHANNEL="$1"
APP_BUNDLE_PATH="$2"

if [[ "$CHANNEL" != "oss" ]]; then
    echo "Error: only the oss release channel is supported" >&2
    exit 1
fi

# Determine the repository root directory
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"

ICON_BUNDLE_PATH="$REPO_ROOT/app/channels/$CHANNEL/icon/AppIcon.icon"

# Only compile the OSS .icon bundle.
if [[ ! -d "$ICON_BUNDLE_PATH" ]]; then
    echo "Error: .icon bundle not found at $ICON_BUNDLE_PATH" >&2
    exit 1
fi

echo "Compiling Galaxy OSS .icon bundle"

BUNDLED_RESOURCES_DIR="$APP_BUNDLE_PATH/Contents/Resources"
PARTIAL_INFO_PLIST="$(dirname "$APP_BUNDLE_PATH")/partial-icon-info.plist"
ACTOOL_ICON_BUNDLE_PATH="$ICON_BUNDLE_PATH"

# The OSS icon artwork is already shared with the in-app icon picker. Assemble its adaptive
# icon package in a temporary directory so we do not need to keep a second large PNG in Git.
if [[ "$CHANNEL" = "oss" ]]; then
    TEMP_ICON_ROOT="$(mktemp -d)"
    trap 'rm -rf "$TEMP_ICON_ROOT"' EXIT
    ACTOOL_ICON_BUNDLE_PATH="$TEMP_ICON_ROOT/AppIcon.icon"
    mkdir -p "$ACTOOL_ICON_BUNDLE_PATH/Assets"
    cp "$ICON_BUNDLE_PATH/icon.json" "$ACTOOL_ICON_BUNDLE_PATH/icon.json"
    cp "$REPO_ROOT/app/assets/bundled/png/galaxy.png" \
      "$ACTOOL_ICON_BUNDLE_PATH/Assets/Galaxy.png"
fi

# Compile the .icon bundle using actool
xcrun actool \
  --compile "$BUNDLED_RESOURCES_DIR" \
  --platform macosx \
  --minimum-deployment-target 10.14 \
  --app-icon AppIcon \
  --output-partial-info-plist "$PARTIAL_INFO_PLIST" \
  "$ACTOOL_ICON_BUNDLE_PATH"

# Earlier XCode versions won't build the correct asset format for adaptive icons
if [[ ! -f "$BUNDLED_RESOURCES_DIR/Assets.car" ]]; then
    XCODE_VERSION=$(xcodebuild -version 2>/dev/null | head -1 || echo "unknown")
    echo "Warning: actool did not produce Assets.car which is required for the latest App icon format." >&2
    echo "(Note that XCode version <26 does not support .icon bundles. Your version: $XCODE_VERSION)." >&2
fi

# Update Info.plist to reference AppIcon instead of the old .icns
plutil -replace CFBundleIconFile -string "AppIcon" "$APP_BUNDLE_PATH/Contents/Info.plist"
plutil -insert CFBundleIconName -string "AppIcon" "$APP_BUNDLE_PATH/Contents/Info.plist" 2>/dev/null || \
  plutil -replace CFBundleIconName -string "AppIcon" "$APP_BUNDLE_PATH/Contents/Info.plist"

# Get the app name from the bundle
APP_NAME=$(basename "$APP_BUNDLE_PATH" .app)

echo "Icon compiled successfully."
