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

START=90
STOP=15
USE_PROCD=1
NAME=jederproxy

. $IPKG_INSTROOT/lib/functions/jederproxy.sh

uci_get_global_config() {
    local key="$1"
    local default_value="$2"
    local value=$(uci -q get ${NAME}.@general[0].$key)
    echo ${value:=$default_value}
}

start_jederproxy_process() {
    process=$1
    cwd=$2
    cmd=$3
    shift 3

    log "Starting $process from $cmd inside $cwd"
    procd_open_instance
    procd_set_param respawn 1 1 0

    procd_set_param env JP_TARGET_WD="$cwd"
    procd_set_param command "/usr/libexec/jederproxy/jederproxy_jail"
    procd_append_param command "$cmd"
    for arg in "$@"; do
        procd_append_param command "$arg"
    done

    local rlimit_nofile
    local rlimit_data
    rlimit_nofile="$(uci_get_global_config rlimit_nofile)"
    rlimit_data="$(uci_get_global_config rlimit_data)"

    if [ ! -z "${rlimit_nofile}" ] ; then
        log "set limit nofile=${rlimit_nofile}"
        procd_set_param limits "nofile=${rlimit_nofile}"
    fi
    if [ ! -z "${rlimit_data}" ] ; then
        log "set limit data=${rlimit_data}"
        procd_set_param limits "data=${rlimit_data}"
    fi

    procd_set_param stdout 1
    procd_set_param stderr 1
    procd_set_param file /etc/config/jederproxy
    procd_set_param pidfile /var/run/jederproxy.pid
    procd_close_instance
}

setup_dnsmasq() {
    local dns_port=$(uci_get_global_config dns_port)
    local dns_count=$(uci_get_global_config dns_count 0)

    echo "# Generated dnsmasq configurations by luci-app-jederproxy" > /tmp/dnsmasq.d/jederproxy.conf
    echo "strict-order" >> /tmp/dnsmasq.d/jederproxy.conf
    echo "server=/#/127.0.0.1#${dns_port}" >> /tmp/dnsmasq.d/jederproxy.conf
    local cur_port
    for cur_port in $(seq ${dns_port} $(expr ${dns_port} + ${dns_count})); do
        echo "server=127.0.0.1#${cur_port}" >> /tmp/dnsmasq.d/jederproxy.conf
    done

    log $(cat /tmp/dnsmasq.d/jederproxy.conf)
    /etc/init.d/dnsmasq restart > /dev/null 2>&1
}

flush_dnsmasq() {
    [ -f "/tmp/dnsmasq.d/jederproxy.conf" ] && rm /tmp/dnsmasq.d/jederproxy.conf
    /etc/init.d/dnsmasq restart > /dev/null 2>&1
}

system_configure() {
    [ "$(uci_get_global_config transparent_proxy_enable)" == "1" ] || return 0
    log "Setting dnsmasq and firewall for transparent proxy..."
    [ "$(uci_get_global_config dnsmasq_takeover_enable)" == "1" ] && setup_dnsmasq
    setup_firewall
}

system_deconfigure() {
    log "Resetting dnsmasq and firewall configurations..."
    [ "$(uci_get_global_config dnsmasq_takeover_enable)" == "1" ] && flush_dnsmasq
    flush_firewall
}

start_service() {
    local active_server server_type executable_path config_type config_directory config_file
    config_load $NAME

    mkdir -p /var/etc/jederproxy

    active_server=$(uci_get_global_config server)

    config_get server_type "$active_server" server_type
    config_get executable_path "$active_server" executable_path
    config_get config_type "$active_server" config_type
    config_get config_directory "$active_server" config_directory
    config_get config_file "$active_server" config_file

    local working_directory=""
    if [ "$config_type" = "directory" ]; then
        working_directory="$config_directory"
    elif [ "$config_type" = "file" ]; then
        working_directory=$(dirname "$config_file")
    fi

    if [ "$server_type" = "mihomo" ]; then
        if [ "$config_type" = "directory" ]; then
        start_jederproxy_process mihomo "$working_directory" "$executable_path" -d "$config_directory"
        elif [ "$config_type" = "file" ]; then
        start_jederproxy_process mihomo "$working_directory" "$executable_path" -f "$config_file"
        fi
    elif [ "$server_type" = "xray" ]; then
        if [ "$config_type" = "directory" ]; then
        start_jederproxy_process xray "$working_directory" "$executable_path" -confdir "$config_directory"
        elif [ "$config_type" = "file" ]; then
        start_jederproxy_process xray "$working_directory" "$executable_path" run -c "$config_file"
        fi

    fi
    system_configure || system_deconfigure
}

stop_service() {
    system_deconfigure
}

reload_service() {
    stop
    start
}

service_triggers() {
    procd_add_reload_trigger "jederproxy"
}
