#!/bin/sh /etc/rc.common

USE_PROCD=1

START=90
STOP=10

# 从 UCI 获取配置参数
enabled="$(uci get nezha-agent.config.enabled 2>/dev/null || echo '0')"
secret="$(uci get nezha-agent.config.secret 2>/dev/null || echo '')"
debug="$(uci get nezha-agent.config.debug 2>/dev/null || echo 'false')"
debug=$([ "debug" = "1" ] && echo "true" || echo "false")
disable_auto_update="$(uci get nezha-agent.config.disable_auto_update 2>/dev/null || echo 'false')"
disable_auto_update=$([ "$disable_auto_update" = "1" ] && echo "true" || echo "false")
disable_command_execute="$(uci get nezha-agent.config.disable_command_execute 2>/dev/null || echo 'false')"
disable_command_execute=$([ "$disable_command_execute" = "1" ] && echo "true" || echo "false")
disable_force_update="$(uci get nezha-agent.config.disable_force_update 2>/dev/null || echo 'false')"
disable_force_update=$([ "$disable_force_update" = "1" ] && echo "true" || echo "false")
disable_nat="$(uci get nezha-agent.config.disable_nat 2>/dev/null || echo 'false')"
disable_nat=$([ "$disable_nat" = "1" ] && echo "true" || echo "false")
disable_send_query="$(uci get nezha-agent.config.disable_send_query 2>/dev/null || echo 'false')"
disable_send_query=$([ "$disable_send_query" = "1" ] && echo "true" || echo "false")
gpu="$(uci get nezha-agent.config.gpu 2>/dev/null || echo 'false')"
gpu=$([ "$gpu" = "1" ] && echo "true" || echo "false")
insecure_tls="$(uci get nezha-agent.config.insecure_tls 2>/dev/null || echo 'false')"
insecure_tls=$([ "$insecure_tls" = "1" ] && echo "true" || echo "false")
ip_report_period="$(uci get nezha-agent.config.ip_report_period 2>/dev/null || echo '1800')"
report_delay="$(uci get nezha-agent.config.report_delay 2>/dev/null || echo '1')"
host="$(uci get nezha-agent.config.host 2>/dev/null || echo 'data.example.com:8008')"
skip_conn="$(uci get nezha-agent.config.skip_conn 2>/dev/null || echo 'false')"
skip_conn=$([ "$skip_conn" = "1" ] && echo "true" || echo "false")
skip_procs="$(uci get nezha-agent.config.skip_procs 2>/dev/null || echo 'false')"
skip_procs=$([ "$skip_procs" = "1" ] && echo "true" || echo "false")
temperature="$(uci get nezha-agent.config.temperature 2>/dev/null || echo 'false')"
temperature=$([ "$temperature" = "1" ] && echo "true" || echo "false")
tls="$(uci get nezha-agent.config.tls 2>/dev/null || echo 'false')"
tls=$([ "$tls" = "1" ] && echo "true" || echo "false")
gitee="$(uci get nezha-agent.config.gitee 2>/dev/null || echo 'false')"
gitee=$([ "$gitee" = "1" ] && echo "true" || echo "false")
use_ipv6_countrycode="$(uci get nezha-agent.config.use_ipv6_countrycode 2>/dev/null || echo 'false')"
use_ipv6_countrycode=$([ "$use_ipv6_countrycode" = "1" ] && echo "true" || echo "false")
uuid="$(uci get nezha-agent.config.uuid 2>/dev/null || echo '')"

config_file="/etc/config/nezha-agent.yaml"

start_service() {
    # 如果 enabled 为 0，直接退出，不启动服务
    [ "$enabled" = "1" ] || return

    # 打开 procd 实例
    procd_open_instance

    # 输出到配置文件
echo "debug: $debug" > "$config_file"
echo "client_secret: $secret" >> "$config_file"
echo "disable_auto_update: $disable_auto_update" >> "$config_file"
echo "disable_command_execute: $disable_command_execute" >> "$config_file"
echo "disable_force_update: $disable_force_update" >> "$config_file"
echo "disable_nat: $disable_nat" >> "$config_file"
echo "disable_send_query: $disable_send_query" >> "$config_file"  
echo "gpu: $gpu" >> "$config_file"	
echo "insecure_tls: $insecure_tls" >> "$config_file"
echo "ip_report_period: $ip_report_period" >> "$config_file"
echo "report_delay: $report_delay" >> "$config_file"
echo "server: $host" >> "$config_file"
echo "skip_connection_count: $skip_conn" >> "$config_file"
echo "skip_procs_count: $skip_procs" >> "$config_file"
echo "temperature: $temperature" >> "$config_file"
echo "use_ipv6_country_code: $use_ipv6_countrycode" >> "$config_file"
echo "uuid: $uuid" >> "$config_file"
echo "tls: $tls" >> "$config_file"
echo "use_gitee_to_upgrade: $gitee" >> "$config_file"
    

	# 构建基础命令行
    command="/usr/bin/nezha-agentv1 -c $config_file"

    # 输出生成的完整命令行以调试
    echo "Generated command: $command"

    # 设定procd参数
    procd_set_param command $command
    procd_set_param respawn

    # 设定核心转储限制
    [ -e /proc/sys/kernel/core_pattern ] && {
        procd_set_param limits core="unlimited"
    }

    procd_close_instance
}

stop_service() {
    procd_kill
}

service_triggers() {
    # 触发服务重载
    procd_add_reload_trigger "nezha-agent"
}

reload_service() {
	restart
}
