#!/bin/sh
# Desktop launcher for the native iced client.
#
# Client resolution order:
#   0. DSH_NATIVE_BIN (legacy DSH_ICED_BIN) when it names an executable;
#   1. the DSHNative binary next to this script (the packaged layout: a deb
#      installs both into /usr/bin);
#   2. the first DSHNative on PATH.
#
# Step 1 is what makes an upgrade replace the running client: resolving the
# client through PATH lets any leftover copy that sorts earlier win — a
# hand-copied ~/.local/bin/DSHNative from an older build, a /snap/bin entry, a
# distro-packaged file. The package would replace /usr/bin/DSHNative, the icon
# would keep starting the stale copy, and the window would keep reporting the
# old version (that is exactly the "installed the new deb, still runs the old
# build" report). The launcher therefore prefers its own directory and only
# falls back to PATH for development checkouts, where the binary is not next to
# the script.
#
# Host resolution (only when no explicit client argument is given):
#   1. DSH_NATIVE_URL (legacy DSH_ICED_URL) starts the client against that Host URL;
#   2. DSH_NATIVE_REPO (legacy DSH_ICED_REPO), the current directory ancestry, or a conventional
#      checkout location is used to start `dsh web` through `--launch`;
#   3. otherwise the client opens its setup screen: it checks whether this machine
#      can run DeepSeek Harness, asks for a source checkout or `npx`, and only then
#      offers the Host URL and relay (mobile access) configuration.
set -eu

APP=DSHNative

# Resolve the client once, before any branch below can exec it.
app_bin=${DSH_NATIVE_BIN:-${DSH_ICED_BIN:-}}
if [ -n "$app_bin" ] && [ ! -x "$app_bin" ]; then
    echo "dshnative-launch: DSH_NATIVE_BIN is not executable: $app_bin" >&2
    exit 127
fi
if [ -z "$app_bin" ]; then
    # `$0` carries a directory for the desktop entry and for an installed
    # /usr/bin path, but a shell user typing the bare command name gets no
    # prefix at all; resolving the script itself through PATH is what keeps
    # that case on the packaged binary instead of a stale copy.
    case $0 in
        */*) self_path=$0 ;;
        *) self_path=$(command -v "$0" 2>/dev/null || true) ;;
    esac
    self_dir=$(CDPATH= cd -- "$(dirname -- "${self_path:-.}")" 2>/dev/null && pwd -P) ||
        self_dir=
    # Two layouts ship this launcher: the deb puts it next to the client in
    # /usr/bin, the snap keeps the launcher in $SNAP/bin and the client in
    # $SNAP/usr/bin. Both are checked before PATH so an outside copy cannot win.
    if [ -n "$self_dir" ]; then
        for candidate in "$self_dir/$APP" "$self_dir/../usr/bin/$APP"; do
            if [ -x "$candidate" ]; then
                app_bin=$candidate
                break
            fi
        done
    fi
fi
if [ -z "$app_bin" ]; then
    app_bin=$(command -v "$APP" 2>/dev/null || true)
fi
if [ -z "$app_bin" ]; then
    echo "dshnative-launch: no $APP client next to $0 or in PATH" >&2
    exit 127
fi

if [ "$#" -gt 0 ]; then
    exec "$app_bin" "$@"
fi

# The client only ships an English and a Simplified Chinese locale, so use the
# session locale as the default when the user did not pass --zh themselves.
locale=${LC_ALL:-${LC_MESSAGES:-${LANG:-}}}
if [ "${locale%%_*}" = zh ]; then
    set -- --zh
fi

url=${DSH_NATIVE_URL:-${DSH_ICED_URL:-}}
if [ -n "$url" ]; then
    exec "$app_bin" --url "$url" "$@"
fi

repo=
explicit_repo=${DSH_NATIVE_REPO:-${DSH_ICED_REPO:-}}
if [ -n "$explicit_repo" ] &&
    [ -f "$explicit_repo/pnpm-workspace.yaml" ] &&
    [ -f "$explicit_repo/apps/cli/package.json" ]; then
    repo=$explicit_repo
fi

if [ -z "$repo" ]; then
    directory=$(pwd)
    while :; do
        if [ -f "$directory/pnpm-workspace.yaml" ] &&
            [ -f "$directory/apps/cli/package.json" ]; then
            repo=$directory
            break
        fi
        [ "$directory" = / ] && break
        directory=$(dirname "$directory")
    done
fi

if [ -z "$repo" ]; then
    for candidate in \
        "${HOME:-/nonexistent}/dev_work/deepseek-harness" \
        "${HOME:-/nonexistent}/deepseek-harness" \
        /opt/deepseek-harness \
        /usr/share/deepseek-harness; do
        if [ -f "$candidate/pnpm-workspace.yaml" ] &&
            [ -f "$candidate/apps/cli/package.json" ]; then
            repo=$candidate
            break
        fi
    done
fi

if [ -z "$repo" ]; then
    # No checkout: the setup screen can still start the published package with
    # `npx @deepseek-ai/dsh web`, so keep the plain launch inside the guide and
    # let the user pick the method there.
    exec "$app_bin" "$@"
fi

export PATH="$repo/node_modules/.bin:$repo/apps/cli/node_modules/.bin:$PATH"
cd "$repo"
exec "$app_bin" --launch "$@"
