#!/usr/bin/env bash
set -euo pipefail

MODE="${1:-run}"
APP_NAME="BearWave"
BUNDLE_ID="com.spalencsar.BearWave"
MIN_SYSTEM_VERSION="26.0"

ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
DIST_DIR="$ROOT_DIR/dist"
APP_BUNDLE="$DIST_DIR/$APP_NAME.app"
APP_CONTENTS="$APP_BUNDLE/Contents"
APP_MACOS="$APP_CONTENTS/MacOS"
APP_RESOURCES="$APP_CONTENTS/Resources"
APP_BINARY="$APP_MACOS/$APP_NAME"
INFO_PLIST="$APP_CONTENTS/Info.plist"
ENTITLEMENTS="$ROOT_DIR/Sources/BearWaveCore/Resources/BearWave.entitlements"

pkill -x "$APP_NAME" >/dev/null 2>&1 || true

swift build --disable-index-store
BUILD_BINARY="$(swift build --disable-index-store --show-bin-path)/$APP_NAME"
BUILD_BIN_DIR="$(dirname "$BUILD_BINARY")"

rm -rf "$APP_BUNDLE"
mkdir -p "$APP_MACOS" "$APP_RESOURCES"
cp "$BUILD_BINARY" "$APP_BINARY"
chmod +x "$APP_BINARY"

cp "$ROOT_DIR/Sources/BearWaveCore/Resources/Assets/bearwave.png" "$APP_RESOURCES/BearWave.png"
cp "$ROOT_DIR/Sources/BearWaveCore/Resources/Assets/bearwave.png" "$APP_RESOURCES/bearwave.png"
cp "$ROOT_DIR/Sources/BearWaveCore/Resources/Assets/bearwave_line.png" "$APP_RESOURCES/bearwave_line.png"
cp "$ROOT_DIR/AppIcon.icns" "$APP_RESOURCES/AppIcon.icns"
for lproj in "$ROOT_DIR"/Sources/BearWaveCore/Resources/*.lproj; do
  if [ -d "$lproj" ]; then
    cp -R "$lproj" "$APP_RESOURCES/"
  fi
done
if [ -d "$BUILD_BIN_DIR/BearWave_BearWaveCore.bundle" ]; then
  cp -R "$BUILD_BIN_DIR/BearWave_BearWaveCore.bundle" "$APP_BUNDLE/BearWave_BearWaveCore.bundle"
fi

cat >"$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>$APP_NAME</string>
  <key>CFBundleIdentifier</key>
  <string>$BUNDLE_ID</string>
  <key>CFBundleName</key>
  <string>$APP_NAME</string>
  <key>CFBundleDisplayName</key>
  <string>BearWave</string>
  <key>CFBundlePackageType</key>
  <string>APPL</string>
  <key>CFBundleShortVersionString</key>
  <string>1.0.0</string>
  <key>CFBundleVersion</key>
  <string>1</string>
  <key>LSMinimumSystemVersion</key>
  <string>$MIN_SYSTEM_VERSION</string>
  <key>NSPrincipalClass</key>
  <string>NSApplication</string>
  <key>NSAppTransportSecurity</key>
  <dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
    <key>NSAllowsArbitraryLoadsForMedia</key>
    <true/>
  </dict>
  <key>CFBundleLocalizations</key>
  <array>
    <string>en</string>
    <string>de</string>
  </array>
  <key>CFBundleIconFile</key>
  <string>AppIcon</string>
</dict>
</plist>
PLIST

open_app() {
  /usr/bin/open -n "$APP_BUNDLE"
}

case "$MODE" in
  run)
    open_app
    ;;
  --debug|debug)
    lldb -- "$APP_BINARY"
    ;;
  --logs|logs)
    open_app
    /usr/bin/log stream --info --style compact --predicate "process == \"$APP_NAME\""
    ;;
  --telemetry|telemetry)
    open_app
    /usr/bin/log stream --info --style compact --predicate "subsystem == \"$BUNDLE_ID\""
    ;;
  --verify|verify)
    open_app
    sleep 1
    pgrep -x "$APP_NAME" >/dev/null
    ;;
  *)
    echo "usage: $0 [run|--debug|--logs|--telemetry|--verify]" >&2
    exit 2
    ;;
esac
