#!/bin/bash

# Toggles a per-app compose between default networking and gluetun-routed
# networking by editing marker regions: GLUETUN_OFF_*  and GLUETUN_ON_*.
#
# default → keep OFF region as-is, ON region stays commented (no-op).
# gluetun → comment out everything in OFF region, uncomment ON region,
#           force traefik.enable to false (app reached via gluetun ports).
tagsProcessorNetworkMode()
{
    local file="$1"
    local mode="$2"

    if [[ -z "$file" || ! -f "$file" ]]; then
        return 0
    fi
    if [[ "$mode" != "gluetun" ]]; then
        return 0
    fi

    local tmp="${file}.netmode.$$"
    sudo awk '
        BEGIN { in_off=0; in_on=0 }
        /# *GLUETUN_OFF_BEGIN/ { in_off=1; print; next }
        /# *GLUETUN_OFF_END/   { in_off=0; print; next }
        /# *GLUETUN_ON_BEGIN/  { in_on=1;  print; next }
        /# *GLUETUN_ON_END/    { in_on=0;  print; next }
        {
            if (in_off) {
                if ($0 ~ /^[[:space:]]*#/) { print; next }
                match($0, /^[[:space:]]*/)
                indent = substr($0, RSTART, RLENGTH)
                rest = substr($0, RLENGTH + 1)
                print indent "# " rest
                next
            }
            if (in_on) {
                match($0, /^[[:space:]]*/)
                indent = substr($0, 1, RLENGTH)
                rest = substr($0, RLENGTH + 1)
                sub(/^#[[:space:]]?/, "", rest)
                print indent rest
                next
            }
            print
        }
    ' "$file" | sudo tee "$tmp" >/dev/null

    sudo mv "$tmp" "$file"

    tagsManagerUpdateUniversalTag "$file" "TRAEFIK_ENABLE_TAG" "false"
}

# Rebuilds gluetun's GLUETUN_FORWARDED_PORTS region from every installed
# app whose CFG_<APP>_NETWORK is gluetun. Region is fully rewritten on every
# call so removed apps drop out automatically.
tagsProcessorGluetunForwardedPorts()
{
    local gluetun_compose="${containers_dir}gluetun/docker-compose.yml"
    if [[ ! -f "$gluetun_compose" ]]; then return 0; fi
    if ! command -v sqlite3 >/dev/null 2>&1; then return 0; fi
    if [[ ! -f "$docker_dir/$db_file" ]]; then return 0; fi

    local installed_apps
    installed_apps=$(sudo sqlite3 "$docker_dir/$db_file" \
        "SELECT name FROM apps WHERE status = 1 ORDER BY name;" 2>/dev/null)

    local routed_lines=""
    while IFS= read -r app; do
        [[ -z "$app" || "$app" == "gluetun" ]] && continue
        local app_config_file="${containers_dir}${app}/${app}.config"
        [[ -f "$app_config_file" ]] || continue
        local net
        net=$(grep -E "^CFG_${app^^}_NETWORK=" "$app_config_file" 2>/dev/null | cut -d'=' -f2 | tr -d '"')
        [[ "$net" != "gluetun" ]] && continue

        local ports
        ports=$(sudo sqlite3 "$docker_dir/$db_file" \
            "SELECT resource_value FROM network_resources WHERE app_name = '$app' AND resource_type = 'port' AND status = 'active';" 2>/dev/null)
        while IFS= read -r p; do
            [[ -z "$p" ]] && continue
            local ext_port int_port access
            ext_port=$(echo "$p" | cut -d':' -f1)
            int_port=$(echo "$p" | cut -d':' -f2)
            access=$(echo "$p" | cut -d':' -f3)
            [[ "$access" == "disabled" ]] && continue
            [[ -z "$ext_port" || "$ext_port" == "random" ]] && continue
            routed_lines+="      - \"${ext_port}:${int_port}\" # gluetun-routed: ${app}"$'\n'
        done <<< "$ports"
    done <<< "$installed_apps"

    local tmp="${gluetun_compose}.fwd.$$"
    sudo awk -v block="$routed_lines" '
        BEGIN { in_region=0 }
        /# *GLUETUN_FORWARDED_PORTS_BEGIN/ {
            print
            printf "%s", block
            in_region=1
            next
        }
        /# *GLUETUN_FORWARDED_PORTS_END/ { in_region=0; print; next }
        { if (!in_region) print }
    ' "$gluetun_compose" | sudo tee "$tmp" >/dev/null

    if ! sudo cmp -s "$tmp" "$gluetun_compose"; then
        sudo mv "$tmp" "$gluetun_compose"
        if sudo docker ps --format '{{.Names}}' 2>/dev/null | grep -q '^gluetun-service$'; then
            isNotice "Gluetun forwarded ports changed; recreating gluetun-service to apply."
            (cd "${containers_dir}gluetun" && sudo docker compose up -d --force-recreate gluetun-service >/dev/null 2>&1) || true
            # Recreating gluetun gives it a new container ID, which orphans
            # every `network_mode: container:gluetun-service` reference. Re-
            # attach all routed apps so they share the new netns instead of
            # silently getting their own.
            appGluetunRecreateRouted
        fi
    else
        sudo rm -f "$tmp"
    fi
}
