#!/usr/bin/env bash
set -euo pipefail

BRAND="Genç Hosting API"
SELF_URL="https://uptime.seonita.com/api/pre.sh"
API_URL_DEFAULT="https://uptime.seonita.com/api/api/v2.php"
DEFAULT_TYPE="cpanel"
DEFAULT_IP="203.0.113.10"
DEFAULT_CORE="2"
SUPPORT_URL=""
INTRO_TEXT="Tek satır komutlarla kendi alan adınız üzerinden lisans işlemlerini yönetin. Shell yardımcısı ürün listeleme, lisans kayıt, yenileme ve IP değişimini destekler."


have_cmd() { command -v "$1" >/dev/null 2>&1; }

print_json() {
  if have_cmd jq; then
    jq .
  elif have_cmd python3; then
    python3 -m json.tool
  elif have_cmd python; then
    python -m json.tool
  else
    cat
  fi
}

say() { printf '%s
' "$*"; }
warn() { printf '[!] %s
' "$*" >&2; }

usage() {
  cat <<EOF
${BRAND} shell yardimcisi

${INTRO_TEXT}

Kullanim:
  curl -fsSL ${SELF_URL} | bash -s -- status
  curl -fsSL ${SELF_URL} | bash -s -- products
  curl -fsSL ${SELF_URL} | bash -s -- licenses --api-key KEY [--type ${DEFAULT_TYPE}] [--ip ${DEFAULT_IP}]
  curl -fsSL ${SELF_URL} | bash -s -- register --api-key KEY --type ${DEFAULT_TYPE} --ip ${DEFAULT_IP}
  curl -fsSL ${SELF_URL} | bash -s -- renew --api-key KEY --type ${DEFAULT_TYPE} --ip ${DEFAULT_IP}
  curl -fsSL ${SELF_URL} | bash -s -- change-ip --api-key KEY --type ${DEFAULT_TYPE} --ip ${DEFAULT_IP} --newip 203.0.113.11
  curl -fsSL ${SELF_URL} | bash -s -- change-core --api-key KEY --ip ${DEFAULT_IP} --core ${DEFAULT_CORE}

Desteklenen komutlar:
  status, products, licenses, register, renew, change-ip, change-core, help

Parametreler:
  --api-url URL     Varsayilan: ${API_URL_DEFAULT}
  --api-key KEY     API uygulama veya musteri anahtari
  --type TYPE       Varsayilan lisans turu: ${DEFAULT_TYPE}
  --ip IP           Hedef IP
  --newip IP        Yeni IP
  --core N          LiteSpeed core degeri

Ortam degiskenleri:
  GH_API_URL, GH_API_KEY, GH_TYPE, GH_IP, GH_NEWIP, GH_CORE
EOF
  if [[ -n "${SUPPORT_URL}" ]]; then
    say "Destek: ${SUPPORT_URL}"
  fi
}

API_URL="${GH_API_URL:-${API_URL_DEFAULT}}"
API_KEY="${GH_API_KEY:-}"
TYPE="${GH_TYPE:-${DEFAULT_TYPE}}"
IP="${GH_IP:-}"
NEWIP="${GH_NEWIP:-}"
CORE="${GH_CORE:-${DEFAULT_CORE}}"

ACTION="${1:-help}"
if [[ $# -gt 0 ]]; then shift; fi

while [[ $# -gt 0 ]]; do
  case "$1" in
    --api-url) API_URL="${2:-}"; shift 2 ;;
    --api-key) API_KEY="${2:-}"; shift 2 ;;
    --type) TYPE="${2:-}"; shift 2 ;;
    --ip) IP="${2:-}"; shift 2 ;;
    --newip) NEWIP="${2:-}"; shift 2 ;;
    --core) CORE="${2:-}"; shift 2 ;;
    -h|--help|help) usage; exit 0 ;;
    *) warn "Bilinmeyen parametre: $1"; usage; exit 1 ;;
  esac
done

api_get() {
  curl -fsSL -H 'Accept: application/json' "$1"
}

api_auth_get() {
  curl -fsSL -H 'Accept: application/json' -H "X-API-Key: ${API_KEY}" "$1"
}

api_auth_post() {
  local url="$1"; shift
  curl -fsSL -X POST -H 'Accept: application/json' -H "X-API-Key: ${API_KEY}" "$url" "$@"
}

ensure_api_key() {
  if [[ -z "${API_KEY}" ]]; then
    warn "Bu komut icin --api-key gerekli."
    exit 1
  fi
}

maybe_detect_ip() {
  if [[ -n "${IP}" ]]; then
    return 0
  fi
  if have_cmd curl; then
    IP="$(curl -fsSL https://api.ipify.org 2>/dev/null || true)"
  fi
}

case "${ACTION}" in
  help|'')
    usage
    ;;
  status)
    api_get "${API_URL}?action=status" | print_json
    ;;
  products)
    api_get "${API_URL}?action=products" | print_json
    ;;
  licenses)
    ensure_api_key
    url="${API_URL}?action=licenses"
    if [[ -n "${TYPE}" ]]; then url="${url}&type=${TYPE}"; fi
    if [[ -n "${IP}" ]]; then url="${url}&ip=${IP}"; fi
    api_auth_get "$url" | print_json
    ;;
  register)
    ensure_api_key
    maybe_detect_ip
    if [[ -z "${TYPE}" || -z "${IP}" ]]; then
      warn "register icin --type ve --ip gerekli."
      exit 1
    fi
    api_auth_post "${API_URL}?action=register_license"       --data-urlencode "type=${TYPE}"       --data-urlencode "ip=${IP}" | print_json
    ;;
  renew)
    ensure_api_key
    maybe_detect_ip
    if [[ -z "${TYPE}" || -z "${IP}" ]]; then
      warn "renew icin --type ve --ip gerekli."
      exit 1
    fi
    api_auth_post "${API_URL}?action=renew_license"       --data-urlencode "type=${TYPE}"       --data-urlencode "ip=${IP}" | print_json
    ;;
  change-ip)
    ensure_api_key
    if [[ -z "${TYPE}" || -z "${IP}" || -z "${NEWIP}" ]]; then
      warn "change-ip icin --type, --ip ve --newip gerekli."
      exit 1
    fi
    api_auth_post "${API_URL}?action=change_license_ip"       --data-urlencode "type=${TYPE}"       --data-urlencode "ip=${IP}"       --data-urlencode "newip=${NEWIP}" | print_json
    ;;
  change-core)
    ensure_api_key
    maybe_detect_ip
    if [[ -z "${IP}" || -z "${CORE}" ]]; then
      warn "change-core icin --ip ve --core gerekli."
      exit 1
    fi
    api_auth_post "${API_URL}?action=change_litespeed_core"       --data-urlencode "ip=${IP}"       --data-urlencode "core=${CORE}" | print_json
    ;;
  *)
    warn "Bilinmeyen komut: ${ACTION}"
    usage
    exit 1
    ;;
esac
