#!/usr/bin/env sh
#
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
#
# Desktop launcher for Linux .desktop entries. Injects IME environment variables when
# the session did not provide them (common when launched from a desktop icon), then
# execs the real Electron binary. Do not route GUI launches through bin/buddycn
# (CLI shim) — that goes through cli.js and changes the process model.

# Resolve install root (script lives in <root>/bin/)
if [ ! -L "$0" ]; then
	VSCODE_PATH="$(dirname "$0")/.."
else
	if command -v readlink >/dev/null 2>&1; then
		VSCODE_PATH="$(dirname "$(readlink -f "$0")")/.."
	else
		VSCODE_PATH="/usr/share/buddycn"
	fi
fi

ELECTRON="$VSCODE_PATH/buddycn"

# Source shared IME helper (same directory). Fail soft if missing.
IME_HELPER="$(dirname "$0")/code-desktop-ime.sh"
if [ -f "$IME_HELPER" ]; then
	# shellcheck source=/dev/null
	. "$IME_HELPER"
	codebuddy_ensure_linux_ime_env
fi

if [ ! -x "$ELECTRON" ] && [ ! -f "$ELECTRON" ]; then
	echo "Unable to find CodeBuddy CN executable at $ELECTRON" 1>&2
	exit 1
fi

exec "$ELECTRON" "$@"
