#!/usr/bin/env bash
# onx-cpanel-test-connection — Uzak cPanel/WHM sunucusuna SSH bağlantı + cPanel doğrula.
#
# Input (stdin JSON): {host, port, login_user, auth_method:"password", password}
# Output (stdout JSON): {ok:true, cpanel_version:"11.x"}
#
# exit: 0=ok 1=bad input 2=preflight(sshpass yok) 3=bağlantı/cPanel yok
# Deployed to: /usr/local/onoxsoft/bin/onx-cpanel-test-connection

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "${SCRIPT_DIR}/_lib/common.sh"
source "${SCRIPT_DIR}/_lib/ssh-remote.sh"

require_root
onx_json_input
onx_ssh_remote_init

if [[ "${MOCK_MODE}" == "1" ]]; then
    onx_json_out ok true cpanel_version "11.0.0-mock"
    exit 0
fi

# cPanel sürümünü al — bağlandığını + gerçekten cPanel olduğunu doğrular.
VER=$(onx_ssh_run "/usr/local/cpanel/cpanel -V 2>/dev/null || cat /usr/local/cpanel/version 2>/dev/null || true" | head -1 | tr -d '\r\n')

[[ -n "$VER" ]] || onx_die 3 "SSH bağlandı fakat cPanel bulunamadı (cpanel -V boş). Bu sunucu cPanel/WHM mi?"

onx_json_out ok true cpanel_version "$VER"
