#!/bin/sh /etc/rc.common
#
# Copyright (C) 2025 Christopher Sollinger
#
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.
#
# OpenWRT initialization script for Quectel RM520N thermal management
# This script provides procd-based service management for the
# Quectel RM520N thermal monitoring daemon with automatic
# kernel module loading and configuration monitoring.
#

# shellcheck disable=SC2034
USE_PROCD=1
START=50
STOP=50
SERVICE_PROVIDES="quectel_rm520n_thermal"
SERVICE_NAME="quectel_rm520n_thermal"

load_config() {
    config_load quectel_rm520n_thermal
    
    # Set defaults
    fallback_register=1
    enabled=1
    auto_start=1

    # Read options from UCI config
    config_get fallback_register settings fallback_register 1
    config_get enabled settings enabled 1
    config_get auto_start settings auto_start 1
}

start_service() {
    load_config
    
    # Check if service is enabled
    if [ "$enabled" != "1" ]; then
        echo "Service is disabled in configuration"
        return 1
    fi
    
    # Load kernel modules if fallback is enabled
    if [ "$fallback_register" = "1" ]; then
        for module in quectel_rm520n_temp quectel_rm520n_temp_sensor quectel_rm520n_temp_sensor_hwmon; do
            if ! lsmod | grep -q "^${module}"; then
                insmod ${module} 2>/dev/null || true
            fi
        done
        
        # Update kernel module thresholds from UCI configuration
        if [ -x /usr/bin/quectel_rm520n_temp ]; then
            /usr/bin/quectel_rm520n_temp config 2>/dev/null || true
        fi
    fi
    
    # Start the daemon with procd
    procd_open_instance
    procd_set_param command /usr/bin/quectel_rm520n_temp daemon
    procd_set_param respawn 3600 60 3
    procd_set_param stdout 1
    procd_set_param stderr 1
    procd_set_param term_timeout 10
    
    # Set environment variables if debug is enabled
    if [ "$debug" = "1" ]; then
        procd_set_param env DEBUG=1
    fi
    
    procd_close_instance
}

stop_service() {
    rm -f /var/run/quectel_rm520n_temp.pid
}

reload_service() {
    echo "Reloading Quectel RM520N thermal service due to config change..."
    
    # Update kernel module thresholds from UCI configuration first
    if [ -x /usr/bin/quectel_rm520n_temp ]; then
        echo "Updating kernel module thresholds from UCI configuration..."
        /usr/bin/quectel_rm520n_temp config
    fi
    
    stop_service
    sleep 1
    start_service
    
    echo "Service reloaded successfully with new configuration"
}

service_triggers() {
    procd_add_reload_trigger "quectel_rm520n_thermal"
    procd_add_config_trigger "config.change" "quectel_rm520n_thermal" /etc/init.d/quectel_rm520n_thermal reload
}

update_config() {
    echo "Updating kernel module thresholds from UCI configuration..."
    if [ -x /usr/bin/quectel_rm520n_temp ]; then
        /usr/bin/quectel_rm520n_temp config
    else
        echo "Error: quectel_rm520n_temp binary not found"
        return 1
    fi
}

default() {
    echo "Usage: $0 {start|stop|restart|reload|status|enable|disable|update_config}"
    echo ""
    echo "Commands:"
    echo "  start        - Start the Quectel RM520N thermal management service"
    echo "  stop         - Stop the service"
    echo "  restart      - Restart the service"
    echo "  reload       - Reload configuration and restart service"
    echo "  status       - Show service status"
    echo "  enable       - Enable service to start on boot"
    echo "  disable      - Disable service from starting on boot"
    echo "  update_config - Manually update kernel module thresholds from UCI config"
    echo ""
    echo "Configuration: /etc/config/quectel_rm520n_thermal"
    echo "Logs: /var/log/messages (filter: quectel_rm520n_temp)"
    echo ""
    echo "Configuration options:"
    echo "  enabled           - Enable/disable the service (1/0)"
    echo "  auto_start        - Auto-start on boot (1/0)"
    echo "  fallback_register - Load kernel modules automatically (1/0)"
    echo "  debug            - Enable debug logging (1/0)"
    echo ""
    echo "Auto-reload: UCI config changes automatically reload the service"
    echo "  Note: When using 'uci commit' from command line, call '/sbin/reload_config' after"
    echo "        to trigger the auto-reload. LuCI web interface works automatically."
}

case "$1" in
    update_config)
        update_config
        ;;
esac
