#!/bin/bash


# anolis:summary=Launch the default coding agent in a terminal
# anolis:args=[--inline] [--pick]
# anolis:examples=anolis agent | anolis agent --inline

# Downstream: Anolis desktop adaptation; see UPSTREAM.json and patches.
source /usr/libexec/anolis-agent/env || exit 1

inline=false
pick=false

# Flags only. A bare prompt would shadow the subcommands under this group, so
# prompts go through anolis-agent-prompt, which passes one here as --prompt.
while (($#)); do
  case "$1" in
    --inline)
      inline=true
      shift
      ;;
    --pick)
      pick=true
      shift
      ;;
    --prompt)
      prompt=${2:?--prompt needs a value}
      shift 2
      ;;
    *)
      echo "Unexpected argument: $1" >&2
      echo "To pass a prompt: anolis agent prompt \"$*\"" >&2
      exit 1
      ;;
  esac
done

# Agents refuse to remember trust for $HOME, so launches from the keybinding or
# menu start in the work directory instead of re-asking on every session.
if [[ $PWD == "$HOME" || ! -x $PWD ]]; then
  cd "$HOME/Work" 2>/dev/null || cd "$HOME" || exit 1
fi

agent=$(anolis-default-agent)

# Anolis ships without a default, so there is nothing to launch until one is
# picked. --pick offers the choice instead of the error, which is what the
# keybinding wants: a keypress that opens nothing explains nothing.
if [[ -n $agent && $agent != qwen && $agent != qoder ]]; then
  echo "Unsupported default agent: $agent" >&2
  exit 1
fi

# First run is a complete selection -> install -> credentials -> launch flow.
# A shortcut has no input terminal, so open one before asking for a secret.
if [[ -z $agent ]] || ! /usr/bin/python3 /usr/libexec/anolis-agent/auth-config.py "$agent" --check; then
  onboarding=(anolis-agent-onboard)
  [[ -n $agent ]] && onboarding+=(--provider "$agent")
  [[ -n ${prompt:-} ]] && onboarding+=(--prompt "$prompt")
  if [[ -t 0 && -t 2 ]]; then
    [[ $inline == true ]] && onboarding+=(--inline)
    exec "${onboarding[@]}"
  elif [[ $inline == true ]]; then
    exec "${onboarding[@]}" --inline
  else
    exec anolis-launch-tui --app-id=org.anolis.agent "${onboarding[@]}" --inline
  fi
fi

agent_binary=$(anolis-agent-resolve "$agent") || exit 127

# Keep each provider's normal permission prompts; no automatic approval bypass.
case "$agent" in
qwen | qoder)
  command=("$agent_binary")
  if [[ $agent == qwen ]]; then command+=(--approval-mode default); else command+=(--permission-mode default); fi
  [[ -n ${prompt:-} ]] && command+=(--prompt-interactive "$prompt")
  ;;
*)
  echo "Unsupported default agent: $agent (supported: qwen, qoder)" >&2
  exit 1
  ;;
esac

# Qwen/Qoder retain their own authentication and configuration directories.
# This adaptation does not inject approval bypass flags or copy credentials.
if [[ $inline == "true" ]]; then
  exec /usr/libexec/anolis-agent/auth-exec.py "$agent" "${command[@]}"
else
  # A fixed app-id rather than the default org.anolis.<binary>, so every agent
  # window shares one class for window rules and themes to single out.
  exec anolis-launch-tui --app-id=org.anolis.agent /usr/libexec/anolis-agent/auth-exec.py "$agent" "${command[@]}"
fi
