#!/bin/bash
set -euo pipefail

[[ $(uname -s) == Darwin ]] || { echo 'This installer requires macOS.' >&2; exit 1; }
cd "$(dirname "$0")/.."
cargo build --release

app="$HOME/Applications/Herdr.app"
mkdir -p "$app/Contents/MacOS" "$app/Contents/Resources" "$HOME/.local/bin"
# Replace atomically so installing does not interrupt a running Herdr.
cp target/release/herdr-app "$app/Contents/MacOS/Herdr.new"
mv -f "$app/Contents/MacOS/Herdr.new" "$app/Contents/MacOS/Herdr"
chmod +x "$app/Contents/MacOS/Herdr"
cp assets/icons/Herdr.icns "$app/Contents/Resources/Herdr.icns"

cat > "$app/Contents/Info.plist" <<'PLIST'
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0"><dict>
    <key>CFBundleExecutable</key><string>Herdr</string>
    <key>CFBundleIdentifier</key><string>dev.herdr.app</string>
    <key>CFBundleName</key><string>Herdr</string>
    <key>CFBundleDisplayName</key><string>Herdr</string>
    <key>CFBundleIconFile</key><string>Herdr</string>
    <key>CFBundlePackageType</key><string>APPL</string>
    <key>CFBundleShortVersionString</key><string>0.0.1</string>
    <key>CFBundleVersion</key><string>1</string>
    <key>NSPrincipalClass</key><string>NSApplication</string>
    <key>NSHighResolutionCapable</key><true/>
    <key>LSEnvironment</key><dict/></dict></plist>
PLIST
# Finder does not inherit a login shell's PATH; hc must still be discoverable.
/usr/libexec/PlistBuddy -c "Add :LSEnvironment:PATH string $HOME/.local/bin:/opt/homebrew/bin:$PATH" "$app/Contents/Info.plist"
codesign --force --sign - "$app"
ln -sfn "$app/Contents/MacOS/Herdr" "$HOME/.local/bin/herdr-app"
/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -f "$app"
echo "Installed $app. Quit and reopen Herdr to use the new build."
