#!/bin/sh

. caterva-alterator.sh
. verborum-caterva-valtable

print_port()
{
    if [ -z "$transparent" ] || [ "$transparent" = "#f" ]; then
      return 0
    fi

    if [ "$start_port" = "$end_port" ]; then
        if [ -z "$(read_val SQ_PR_PORT_${start_port})" ]; then
            print_info "Add port ${start_port} to the port redirection script"
            echo "pr_set ${start_port} \$PROXY    # $label"
            def_val SQ_PR_PORT_${start_port} 1
        fi
    else
        if [ -z "$(read_val SQ_PR_PORT_${start_port}_${end_port})" ]; then
            print_info "Add port range ${start_port}-${end_port} to the port redirection script"
            echo "pr_set ${start_port}:${end_port} \$PROXY    # $label"
            def_val SQ_PR_PORT_${start_port}_${end_port} 1
        fi
    fi
}

if [ "$(read_val SQ_MODE)" = "TRANSPARENT" ]; then
    echo
    echo "pr_init"
    metalterator-cmdline /squid/safe-ports action "list" \
    start_port "start_port" \
    end_port "end_port" \
    method "method" \
    transparent "transparent" \
    comment "label" \
    | on_object print_port
    print_info "Turn port redirection on"
    echo "pr_final"
    echo "pr_control on"
fi
