#!/bin/sh
set -eu

APP_DIR="${WX_BOT_APP_DIR:-/opt/wx-bot-client}"
CONFIG_PATH="${WXBOT_CONFIG:-$HOME/.config/wx-bot-client/config.json}"
export WXBOT_CONFIG="$CONFIG_PATH"

if [ ! -f "$CONFIG_PATH" ]; then
  mkdir -p "$(dirname "$CONFIG_PATH")"
  if [ -f "$APP_DIR/config.example.json" ]; then
    cp "$APP_DIR/config.example.json" "$CONFIG_PATH"
    chmod 600 "$CONFIG_PATH" 2>/dev/null || true
  fi
  echo "Created config template: $CONFIG_PATH" >&2
  echo "Starting wx-bot-client; desktop sessions can complete setup in the wizard." >&2
fi

cd "$APP_DIR"
exec "$APP_DIR/wx-bot-client" "$@"
