#!/usr/bin/env bash
#
# Default install dir: /usr/local/frps/plugin/frp-block-proxy-plugin (override with INSTALL_DIR).
#
# Replace the installed frp-block-proxy-plugin binary with the build from Cartech CDN:
#   stop service → download → replace → start
#
# Usage (as root):
#   ./update-frp-block-proxy-plugin.sh
#   PLUGIN_DOWNLOAD_URL=https://example.com/frp-block-proxy-plugin ./update-frp-block-proxy-plugin.sh
#
# Default URL matches Cartech plugin index:
#   https://dl.cartech.one/dl/CAR%20PROXY%20SERVER/plugin/CAR%20PROXY%20CONTROL/frp-block-proxy-plugin
#

set -euo pipefail

PLUGIN_DOWNLOAD_URL="${PLUGIN_DOWNLOAD_URL:-https://dl.cartech.one/dl/CAR%20PROXY%20SERVER/plugin/CAR%20PROXY%20CONTROL/frp-block-proxy-plugin}"
INSTALL_DIR="${INSTALL_DIR:-/usr/local/frps/plugin/frp-block-proxy-plugin}"
BINARY_NAME="${BINARY_NAME:-frp-block-proxy-plugin}"
SERVICE_NAME="${SERVICE_NAME:-frp-block-proxy-plugin}"
PLUGIN_BIN="${PLUGIN_BIN:-${INSTALL_DIR}/${BINARY_NAME}}"

if [[ "${EUID:-0}" -ne 0 ]]; then
	echo "Error: run as root (e.g. sudo $0)" >&2
	exit 1
fi

download_to() {
	local url="$1"
	local out="$2"
	if command -v wget >/dev/null 2>&1; then
		wget -4 --no-check-certificate -qO "$out" "$url"
	elif command -v curl >/dev/null 2>&1; then
		curl -4fsSL -o "$out" "$url"
	else
		echo "Error: need wget or curl" >&2
		return 1
	fi
}

# Do not use `list-unit-files | grep`: output format differs across systemd versions and the
# match can fail even when the unit exists (false "not installed").
plugin_unit_file_present() {
	[[ -f "/etc/systemd/system/${SERVICE_NAME}.service" ]] ||
		[[ -f "/usr/lib/systemd/system/${SERVICE_NAME}.service" ]] ||
		[[ -f "/lib/systemd/system/${SERVICE_NAME}.service" ]]
}

stop_plugin() {
	echo "Stopping ${SERVICE_NAME} via systemctl ..."
	systemctl stop "${SERVICE_NAME}" 2>/dev/null || true
}

start_plugin() {
	systemctl daemon-reload 2>/dev/null || true
	echo "Starting ${SERVICE_NAME} via systemctl ..."
	if systemctl start "${SERVICE_NAME}"; then
		return 0
	fi
	if plugin_unit_file_present; then
		systemctl daemon-reload
		if systemctl start "${SERVICE_NAME}"; then
			return 0
		fi
	fi
	echo "Error: could not start ${SERVICE_NAME}.service (run install-frp-block-proxy-plugin.sh install first)." >&2
	systemctl status "${SERVICE_NAME}" --no-pager -l 2>&1 | head -30 >&2 || true
	exit 1
}

mkdir -p "$INSTALL_DIR"

tmp="${INSTALL_DIR}/.${BINARY_NAME}.download.$$"
trap 'rm -f "$tmp"' EXIT

echo "Downloading ${BINARY_NAME} from:"
echo "  ${PLUGIN_DOWNLOAD_URL}"
if ! download_to "${PLUGIN_DOWNLOAD_URL}" "$tmp"; then
	echo "Download failed." >&2
	exit 1
fi
if [[ ! -s "$tmp" ]]; then
	echo "Downloaded file is empty." >&2
	exit 1
fi
chmod 755 "$tmp"

if [[ -f "$PLUGIN_BIN" ]]; then
	cp -a "$PLUGIN_BIN" "${PLUGIN_BIN}.bak.$(date +%Y%m%d%H%M%S)"
	echo "Backed up existing binary to ${PLUGIN_BIN}.bak.*"
fi

stop_plugin
mv -f "$tmp" "$PLUGIN_BIN"
trap - EXIT
chown root:root "$PLUGIN_BIN" 2>/dev/null || true
chmod 755 "$PLUGIN_BIN"

start_plugin
echo "Done. Plugin binary: ${PLUGIN_BIN}"
