#!/bin/bash
### usage: build-macos [opts..]
###
### Cross compiles the macOS distribution from a Linux host

set -euo pipefail
cd "$(dirname "$0")/.."
source script/lib/functions.sh
source script/lib/error.sh

# configure me!
PROFILE=release
TARGET=x86_64-apple-darwin
ARCH=x86_64

# check for MACOS_KITS
[ -n "${MACOS_KITS:-}" ] || die "MACOS_KITS not set, cannot proceed. Hailey will document this whole thing eventually"
[ -d "$MACOS_KITS/osxcross/bin" ] || die "$MACOS_KITS/osxcross/bin does not exist, cannot proceed"

# grab version number from git
version="$(git describe --tags)"
info "Building version $version"

# put cross compiler into PATH
export PATH="$MACOS_KITS/osxcross/bin:$PATH"

# set cross compile vars
export PKG_CONFIG_LIBDIR="$MACOS_KITS/gtk/$ARCH/lib/pkgconfig"
export PKG_CONFIG_ALLOW_CROSS=1
export CC=x86_64-apple-darwin23-clang
export CARGO_TARGET_X86_64_APPLE_DARWIN_LINKER="$CC"

# pass --release to cargo if PROFILE==release:
cargo_args=()
[[ "$PROFILE" == "release" ]] && cargo_args+="--release"

# do the build
log-command cargo build --target="$TARGET" "${cargo_args[@]}" --all

# assemble macOS .app bundle
info "Assembling 'Tangara Companion.app' bundle"

build_dir="target/$TARGET/$PROFILE"
bundle="$build_dir/Tangara Companion.app"

# clean the slate:
rm -rf "$bundle"
mkdir -p "$bundle"

# copy app icon
resources="$bundle/Contents/Resources"
icon="$resources/Tangara Companion.png"
mkdir -p "$resources"
cp data/assets/icon.png "$icon"

# write Info.plist
sed -e "s/{{VERSION}}/$version/g" "pkg/macos/Info.plist" > "$bundle/Contents/Info.plist"

info "Installing executables"

# copy executable into bundle
executable="$bundle/Contents/MacOS/tangara-companion"
install -D "$build_dir/tangara-companion" "$executable"

# copy rsvg loader into bundle manually
# we only have to do this because its a runtime loaded library, and so for the
# purposes of install-dylibs.rb, it's a 'root' - not directly depended on by
# anything
gdk_pixbuf="gdk-pixbuf-2.0/2.10.0"
svg_loader_lib="$gdk_pixbuf/loaders/libpixbufloader-svg.so"
loaders_cache="$gdk_pixbuf/loaders.cache"
install -D "$MACOS_KITS/gtk/$ARCH/lib/$svg_loader_lib" "$bundle/Contents/lib/$svg_loader_lib"
install -D "$MACOS_KITS/gtk/$ARCH/lib/$loaders_cache" "$bundle/Contents/lib/$loaders_cache"

info "Bundling dynamic libraries"

# install all dylib dependencies
pkg/macos/install-dylibs.rb \
    "$bundle/Contents/lib" \
    -- \
    "../MacOS/tangara-companion" \
    "$svg_loader_lib"

# build distribution zip including Licenses at top level next to .app
info "zipping app bundle"
staging_dir="/tmp/tangaracompanion-build-$$"
zip_name="tangaracompanion-$version-macos-x86_64.zip"
mkdir -p "$staging_dir"
cp -R "$bundle" "$staging_dir"
(cd "$staging_dir" && zip -r9 "$zip_name" *)

# copy into dist dir
dist_dir="dist/$version"
mkdir -p "$dist_dir"
mv "$staging_dir/$zip_name" "$dist_dir/$zip_name"

# copy cli into dist dir also
cli_bin="tangara-cli-$version-macos-$ARCH"
cp "$build_dir/tangara" "$dist_dir/$cli_bin"

win "zipped app bundle written to $dist_dir/$zip_name"
win "cli executable written to $dist_dir/$cli_bin"
