#!/bin/sh

CONFIG="owrtremote"
SECTION="main"

log() {
	printf '%s\n' "$*" >&2
}

die() {
	printf 'ОШИБКА: %s\n' "$*" >&2
	exit 1
}

usage() {
	cat <<'EOF'
Использование:
  owrt-remote status
  owrt-remote doctor
  owrt-remote install-xray-tmp
  owrt-remote stop-xray
  owrt-remote render-client [--stdout]
  owrt-remote run
  owrt-remote heartbeat
  owrt-remote heartbeat-loop

Настройки лежат в /etc/config/owrtremote.
EOF
}

uci_get() {
	local key default value
	key="$1"
	default="$2"
	value="$(uci -q get "$CONFIG.$SECTION.$key" 2>/dev/null || true)"
	[ -n "$value" ] && {
		printf '%s' "$value"
		return
	}
	printf '%s' "$default"
}

truthy() {
	case "$1" in
		1|yes|true|on|enabled) return 0 ;;
		*) return 1 ;;
	esac
}

json_escape() {
	printf '%s' "$1" | sed 's/\\/\\\\/g; s/"/\\"/g; s/	/\\t/g'
}

json_string() {
	printf '"%s"' "$(json_escape "$1")"
}

safe_num() {
	case "$1" in
		''|*[!0-9]*) printf '%s' "$2" ;;
		*) printf '%s' "$1" ;;
	esac
}

safe_id() {
	printf '%s' "$1" | tr -cs 'A-Za-z0-9._-' '-'
}

host_model() {
	if [ -r /tmp/sysinfo/model ]; then
		cat /tmp/sysinfo/model
		return
	fi
	if [ -r /proc/device-tree/model ]; then
		tr -d '\000' </proc/device-tree/model
		return
	fi
	printf 'OpenWrt router'
}

board_name() {
	if [ -r /tmp/sysinfo/board_name ]; then
		cat /tmp/sysinfo/board_name
		return
	fi
	printf 'unknown'
}

release_text() {
	if [ -r /etc/openwrt_release ]; then
		(
			. /etc/openwrt_release 2>/dev/null
			printf '%s %s' "${DISTRIB_ID:-OpenWrt}" "${DISTRIB_RELEASE:-unknown}"
		)
		return
	fi
	printf 'OpenWrt unknown'
}

uptime_seconds() {
	if [ -r /proc/uptime ]; then
		cut -d. -f1 /proc/uptime
		return
	fi
	printf '0'
}

load_text() {
	if [ -r /proc/loadavg ]; then
		awk '{print $1" "$2" "$3}' /proc/loadavg
		return
	fi
	printf '0 0 0'
}

memory_text() {
	if [ -r /proc/meminfo ]; then
		awk '
			/^MemTotal:/ { total=$2 }
			/^MemAvailable:/ { avail=$2 }
			/^MemFree:/ && avail=="" { avail=$2 }
			END {
				if (total == "") total = 0
				if (avail == "") avail = 0
				printf "%s/%s kB", avail, total
			}
		' /proc/meminfo
		return
	fi
	printf '0/0 kB'
}

flash_text() {
	local mount text
	for mount in /overlay /; do
		text="$(
			df -k "$mount" 2>/dev/null | awk '
				function mb(k) {
					if (k == "" || k == 0) return "0 MB"
					return sprintf("%.1f MB", k / 1024)
				}
				END {
					if (NF >= 5) {
						total=$(NF-4); avail=$(NF-2); pct=$(NF-1)
						printf "%s free / %s (%s used)", mb(avail), mb(total), pct
					}
				}
			'
		)"
		if [ -n "$text" ]; then
			printf '%s' "$text"
			return
		fi
	done
	printf 'unknown'
}

temperature_text() {
	local file raw max
	max=""
	for file in /sys/class/thermal/thermal_zone*/temp /sys/devices/virtual/thermal/thermal_zone*/temp /sys/class/hwmon/hwmon*/temp*_input; do
		[ -r "$file" ] || continue
		raw="$(cat "$file" 2>/dev/null | tr -cd '0-9-')"
		[ -n "$raw" ] || continue
		case "$raw" in
			-*|'') continue ;;
		esac
		[ -z "$max" ] && max="$raw"
		[ "$raw" -gt "$max" ] && max="$raw"
	done
	if [ -n "$max" ]; then
		awk -v t="$max" 'BEGIN { if (t > 1000) printf "%.1f C", t / 1000; else printf "%s C", t }'
		return
	fi
	printf 'unknown'
}

ps_all() {
	ps w 2>/dev/null || ps 2>/dev/null || true
}

xray_running() {
	local config
	config="$(uci_get xray_config /etc/xray/owrt-remote-client.json)"
	ps_all | grep '[x]ray' | grep -F "$config" >/dev/null 2>&1
}

stop_xray() {
	local config pids pid alive
	config="$(uci_get xray_config /etc/xray/owrt-remote-client.json)"
	pids="$(ps_all | grep '[x]ray' | grep -F "$config" | awk '{print $1}')"
	if [ -z "$pids" ]; then
		printf '%s\n' "OK: Xray уже остановлен"
		return 0
	fi
	for pid in $pids; do
		kill "$pid" >/dev/null 2>&1 || true
	done
	sleep 1
	alive=""
	for pid in $pids; do
		if kill -0 "$pid" >/dev/null 2>&1; then
			kill -9 "$pid" >/dev/null 2>&1 || true
			alive="$alive $pid"
		fi
	done
	if [ -n "$alive" ]; then
		printf 'OK: Xray остановлен принудительно:%s\n' "$alive"
	else
		printf '%s\n' "OK: Xray остановлен"
	fi
}

ssh_running() {
	ps_all | grep '[d]ropbear' >/dev/null 2>&1
}

xray_bin_path() {
	local configured candidate
	configured="$(uci_get xray_bin /usr/bin/xray)"
	if [ -x "$configured" ]; then
		printf '%s' "$configured"
		return
	fi
	for candidate in /usr/bin/xray /usr/bin/xray-core /usr/local/bin/xray /opt/bin/xray /tmp/owrt-xray/xray /tmp/xray; do
		if [ -x "$candidate" ]; then
			printf '%s' "$candidate"
			return
		fi
	done
	candidate="$(command -v xray 2>/dev/null || command -v xray-core 2>/dev/null || true)"
	if [ -n "$candidate" ] && [ -x "$candidate" ]; then
		printf '%s' "$candidate"
		return
	fi
	printf '%s' "$configured"
}

fetch_file() {
	local url dst
	url="$1"
	dst="$2"
	if command -v wget >/dev/null 2>&1; then
		wget -q -O "$dst" "$url"
		return
	fi
	if command -v curl >/dev/null 2>&1; then
		curl -fsSL "$url" -o "$dst"
		return
	fi
	die "для скачивания нужен wget или curl"
}

xray_release_asset() {
	case "$(uname -m 2>/dev/null)" in
		aarch64|arm64) printf '%s\n' "Xray-linux-arm64-v8a.zip" ;;
		armv7l|armv7*) printf '%s\n' "Xray-linux-arm32-v7a.zip" ;;
		armv6l|armv6*) printf '%s\n' "Xray-linux-arm32-v6.zip" ;;
		armv5l|armv5*) printf '%s\n' "Xray-linux-arm32-v5.zip" ;;
		x86_64|amd64) printf '%s\n' "Xray-linux-64.zip" ;;
		i386|i686) printf '%s\n' "Xray-linux-32.zip" ;;
		*) return 1 ;;
	esac
}

install_xray_tmp() {
	local dir zip asset url version
	dir="/tmp/owrt-xray"
	zip="$dir/xray.zip"
	asset="$(xray_release_asset)" || die "не знаю готовый Xray asset для $(uname -m 2>/dev/null)"
	url="https://github.com/XTLS/Xray-core/releases/latest/download/$asset"

	mkdir -p /var/lock /var/run "$dir"
	if ! command -v unzip >/dev/null 2>&1; then
		opkg update || die "не смог обновить opkg для установки unzip"
		opkg install unzip || die "не смог поставить unzip"
	fi

	printf 'Скачиваю Xray: %s\n' "$asset"
	fetch_file "$url" "$zip"
	( cd "$dir" && unzip -o "$zip" xray >/dev/null ) || die "не смог распаковать Xray"
	chmod +x "$dir/xray"
	version="$("$dir/xray" version 2>&1)" || die "скачанный Xray не запускается"
	version="$(printf '%s\n' "$version" | sed -n '1p')"

	uci set "$CONFIG.$SECTION.xray_bin=$dir/xray"
	uci commit "$CONFIG"
	printf 'OK: Xray поставлен в %s\n' "$dir/xray"
	printf 'OK: %s\n' "$version"
	printf 'OK: xray_bin сохранен в /etc/config/owrtremote\n'
}

ensure_xray_available() {
	local configured candidate found
	configured="$(uci_get xray_bin /usr/bin/xray)"
	[ -x "$configured" ] && return 0

	for candidate in /usr/bin/xray /usr/bin/xray-core /usr/local/bin/xray /opt/bin/xray /tmp/owrt-xray/xray /tmp/xray; do
		if [ -x "$candidate" ]; then
			found="$candidate"
			break
		fi
	done

	if [ -z "$found" ]; then
		found="$(command -v xray 2>/dev/null || command -v xray-core 2>/dev/null || true)"
	fi

	if [ -n "$found" ] && [ -x "$found" ]; then
		[ "$found" = "$configured" ] || {
			uci set "$CONFIG.$SECTION.xray_bin=$found"
			uci commit "$CONFIG"
			log "Xray найден: $found"
		}
		return 0
	fi

	log "Xray не найден ($configured), ставлю временный Xray в /tmp..."
	install_xray_tmp
}

service_enabled() {
	/etc/init.d/owrt-remote enabled >/dev/null 2>&1
}

render_client_config() {
	local out stdout router_id vps_host vps_port uuid ssh_uuid encryption flow reverse_tag ssh_reverse_tag admin_host admin_port ssh_host ssh_port xray_config
	stdout="0"
	if [ "${1:-}" = "--stdout" ]; then
		stdout="1"
	fi

	router_id="$(safe_id "$(uci_get router_id main)")"
	vps_host="$(uci_get vps_host '')"
	vps_port="$(safe_num "$(uci_get vps_port 8443)" 8443)"
	uuid="$(uci_get vless_uuid '')"
	ssh_uuid="$(uci_get ssh_vless_uuid '')"
	encryption="$(uci_get vless_encryption none)"
	flow="$(uci_get vless_flow '')"
	reverse_tag="$(safe_id "$(uci_get reverse_tag reverse-in)")"
	ssh_reverse_tag="$(safe_id "$(uci_get ssh_reverse_tag "${reverse_tag}-ssh")")"
	admin_host="$(uci_get admin_host 127.0.0.1)"
	admin_port="$(safe_num "$(uci_get admin_port 80)" 80)"
	ssh_host="$(uci_get ssh_host 127.0.0.1)"
	ssh_port="$(safe_num "$(uci_get ssh_port 22)" 22)"
	xray_config="$(uci_get xray_config /etc/xray/owrt-remote-client.json)"

	[ -n "$vps_host" ] || die "vps_host пустой"
	[ -n "$uuid" ] || die "vless_uuid пустой"
	[ -n "$ssh_uuid" ] || ssh_uuid="$uuid"

	out="${TMPDIR:-/tmp}/owrt-remote-client.$$"
	cat >"$out" <<EOF
{
  "log": {
    "loglevel": "warning"
  },
  "inbounds": [],
  "outbounds": [
    {
      "tag": "router-admin",
      "protocol": "freedom",
      "settings": {
        "redirect": "$(json_escape "$admin_host"):$admin_port"
      }
    },
    {
      "tag": "router-ssh",
      "protocol": "freedom",
      "settings": {
        "redirect": "$(json_escape "$ssh_host"):$ssh_port"
      }
    },
    {
      "tag": "vps-interconn",
      "protocol": "vless",
      "settings": {
        "address": "$(json_escape "$vps_host")",
        "port": $vps_port,
        "id": "$(json_escape "$uuid")",
        "encryption": "$(json_escape "$encryption")",
        "flow": "$(json_escape "$flow")",
        "reverse": {
          "tag": "$(json_escape "$reverse_tag")"
        }
      },
      "streamSettings": {
        "network": "tcp",
        "security": "none"
      }
    },
    {
      "tag": "vps-ssh-interconn",
      "protocol": "vless",
      "settings": {
        "address": "$(json_escape "$vps_host")",
        "port": $vps_port,
        "id": "$(json_escape "$ssh_uuid")",
        "encryption": "$(json_escape "$encryption")",
        "flow": "$(json_escape "$flow")",
        "reverse": {
          "tag": "$(json_escape "$ssh_reverse_tag")"
        }
      },
      "streamSettings": {
        "network": "tcp",
        "security": "none"
      }
    }
  ],
  "routing": {
    "rules": [
      {
        "type": "field",
        "inboundTag": [
          "$(json_escape "$reverse_tag")"
        ],
        "outboundTag": "router-admin"
      },
      {
        "type": "field",
        "inboundTag": [
          "$(json_escape "$ssh_reverse_tag")"
        ],
        "outboundTag": "router-ssh"
      }
    ]
  },
  "remarks": "OpenWrt Remote client for $(json_escape "$router_id")"
}
EOF

	if [ "$stdout" = "1" ]; then
		cat "$out"
		rm -f "$out"
		return
	fi

	mkdir -p "$(dirname "$xray_config")"
	cp "$out" "$xray_config"
	chmod 0600 "$xray_config"
	rm -f "$out"
	printf '%s\n' "$xray_config"
}

run_xray() {
	local enabled xray_bin xray_config
	enabled="$(uci_get enabled 0)"
	truthy "$enabled" || die "сервис выключен: включи option enabled '1'"

	ensure_xray_available
	xray_bin="$(xray_bin_path)"
	xray_config="$(render_client_config)"
	[ -x "$xray_bin" ] || die "xray не найден или не исполняемый: $xray_bin"

	if "$xray_bin" help 2>/dev/null | grep -q 'run'; then
		exec "$xray_bin" run -config "$xray_config"
	fi
	exec "$xray_bin" -config "$xray_config"
}

heartbeat_json() {
	local router_id router_name role public_url hub_url xray_state service_state ssh_state
	router_id="$(safe_id "$(uci_get router_id main)")"
	router_name="$(uci_get router_name "$router_id")"
	role="$(uci_get role node)"
	public_url="$(uci_get public_url '')"
	hub_url="$(uci_get hub_url '')"

	if xray_running; then
		xray_state="running"
	else
		xray_state="stopped"
	fi
	if service_enabled; then
		service_state="enabled"
	else
		service_state="disabled"
	fi
	if ssh_running; then
		ssh_state="running"
	else
		ssh_state="stopped"
	fi

	cat <<EOF
{
  "id": $(json_string "$router_id"),
  "name": $(json_string "$router_name"),
  "role": $(json_string "$role"),
  "model": $(json_string "$(host_model)"),
  "board": $(json_string "$(board_name)"),
  "release": $(json_string "$(release_text)"),
  "hostname": $(json_string "$(hostname 2>/dev/null || printf openwrt)"),
  "public_url": $(json_string "$public_url"),
  "admin_host": $(json_string "$(uci_get admin_host 127.0.0.1)"),
  "admin_port": $(safe_num "$(uci_get admin_port 80)" 80),
  "ssh_host": $(json_string "$(uci_get ssh_host 127.0.0.1)"),
  "ssh_port": $(safe_num "$(uci_get ssh_port 22)" 22),
  "hub_url": $(json_string "$hub_url"),
  "uptime": $(safe_num "$(uptime_seconds)" 0),
  "load": $(json_string "$(load_text)"),
  "memory": $(json_string "$(memory_text)"),
  "flash": $(json_string "$(flash_text)"),
  "temperature": $(json_string "$(temperature_text)"),
  "xray": $(json_string "$xray_state"),
  "ssh": $(json_string "$ssh_state"),
  "service": $(json_string "$service_state")
}
EOF
}

post_json() {
	local url token file
	url="$1"
	token="$2"
	file="$3"

	if command -v wget >/dev/null 2>&1; then
		wget -q -O - \
			--header="Authorization: Bearer $token" \
			--header="Content-Type: application/json" \
			--post-file="$file" "$url"
		return
	fi
	if command -v curl >/dev/null 2>&1; then
		curl -fsS \
			-H "Authorization: Bearer $token" \
			-H "Content-Type: application/json" \
			--data-binary "@$file" "$url"
		return
	fi
	if command -v uclient-fetch >/dev/null 2>&1; then
		uclient-fetch -q -O - \
			--header="Authorization: Bearer $token" \
			--header="Content-Type: application/json" \
			--post-file="$file" "$url"
		return
	fi
	die "для heartbeat нужен uclient-fetch, wget или curl"
}

send_heartbeat() {
	local hub_url token endpoint tmp
	hub_url="$(uci_get hub_url '')"
	token="$(uci_get hub_token '')"
	[ -n "$hub_url" ] || die "hub_url пустой"
	[ -n "$token" ] || die "hub_token пустой"

	endpoint="${hub_url%/}/api/heartbeat"
	tmp="${TMPDIR:-/tmp}/owrt-remote-heartbeat.$$"
	heartbeat_json >"$tmp"
	post_json "$endpoint" "$token" "$tmp"
	rm -f "$tmp"
}

heartbeat_loop() {
	local interval
	interval="$(safe_num "$(uci_get heartbeat_interval 30)" 30)"
	[ "$interval" -lt 10 ] && interval=10
	while :; do
		send_heartbeat >/dev/null 2>&1 || true
		sleep "$interval"
	done
}

status() {
	printf 'router_id: %s\n' "$(uci_get router_id main)"
	printf 'router_name: %s\n' "$(uci_get router_name '')"
	printf 'enabled: %s\n' "$(uci_get enabled 0)"
	printf 'hub_url: %s\n' "$(uci_get hub_url '')"
	printf 'vps_host: %s\n' "$(uci_get vps_host '')"
	printf 'vps_port: %s\n' "$(uci_get vps_port 8443)"
	printf 'xray_bin: %s\n' "$(xray_bin_path)"
	printf 'xray_config: %s\n' "$(uci_get xray_config /etc/xray/owrt-remote-client.json)"
	if xray_running; then
		printf 'xray_state: running\n'
	else
		printf 'xray_state: stopped\n'
	fi
	if service_enabled; then
		printf 'service: enabled\n'
	else
		printf 'service: disabled\n'
	fi
}

doctor() {
	local ok xray_bin
	ok=1
	ensure_xray_available
	xray_bin="$(xray_bin_path)"

	[ -x "$xray_bin" ] || {
		log "нет Xray: $xray_bin"
		ok=0
	}
	[ -n "$(uci_get hub_url '')" ] || {
		log "hub_url пустой"
		ok=0
	}
	[ -n "$(uci_get hub_token '')" ] || {
		log "hub_token пустой"
		ok=0
	}
	[ -n "$(uci_get vps_host '')" ] || {
		log "vps_host пустой"
		ok=0
	}
	[ -n "$(uci_get vless_uuid '')" ] || {
		log "vless_uuid пустой"
		ok=0
	}

	if [ "$ok" = "1" ]; then
		printf '%s\n' "OK: конфиг выглядит готовым"
	else
		exit 1
	fi
}

cmd="${1:-help}"
shift || true

case "$cmd" in
	help|-h|--help)
		usage
		;;
	status)
		status
		;;
	doctor)
		doctor
		;;
	install-xray-tmp)
		install_xray_tmp
		;;
	stop-xray)
		stop_xray
		;;
	render-client)
		render_client_config "$@"
		;;
	run)
		run_xray
		;;
	heartbeat)
		send_heartbeat
		;;
	heartbeat-loop)
		heartbeat_loop
		;;
	*)
		usage >&2
		exit 2
		;;
esac
