#!/bin/bash
# Wrapper script for Ubuntu 24 compatibility with Electron 39
# This script sets required flags before launching the application

# Detect Ubuntu 24+
detect_ubuntu24() {
  if [ -f /etc/os-release ]; then
    . /etc/os-release
    if [ "$ID" = "ubuntu" ]; then
      VERSION=$(echo "$VERSION_ID" | cut -d. -f1)
      if [ "$VERSION" -ge 24 ] 2>/dev/null; then
        return 0
      fi
    fi
  fi
  return 1
}

# Get the directory where this script is located
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
ORIGINAL_EXECUTABLE="$SCRIPT_DIR/tandem.bin"

# Build arguments array - start with required flags
ARGS=("--no-sandbox")

# Add Ubuntu 24 specific flags
if detect_ubuntu24; then
  # Disable zygote to avoid LaunchProcess: failed to execvp errors
  ARGS+=("--no-zygote")
  # Force X11 instead of Wayland to avoid segmentation fault
  ARGS+=("--ozone-platform=x11")
fi

# Pass through all original arguments (user-provided flags come after our flags)
ARGS+=("$@")

# Execute the original executable with flags
exec "$ORIGINAL_EXECUTABLE" "${ARGS[@]}"
