# You should not edit this file.  Instead, create a file with the same
# name as this one, but with a .rul extension instead of .def.  The
# .rul file will override this one.
#
# However, any changes you make to this file will be preserved.

# Some reused regex'es
re_protos='[a-z\,]\+'
re_ports='[a-z0-9\,:]\+'
re_port='[a-z0-9:]\+'
re_host='[a-z0-9]\+'
re_pool='[a-z0-9_]\+'
re_cfg="[[:space:]]*\($re_protos\)[[:space:]]\+\($re_ports\)[[:space:]]\+\($re_host\)\([[:space:]]\+\($re_port\)\)\?[[:space:]]*"
re_cfgline="portfw_pool_\($re_pool\)[[:space:]]*=[[:space:]]*'$re_cfg'"

# Extract portforward hosts
PFW=$(set | grep '^portfw_ip_' | sed "s/^portfw_ip_\($re_host\).*/\1/")

# remove hosts with no pools attached
if [ -n "$PFW" ]; then
    for i in $PFW; do
	poolhits=$(set | grep '^portfw_pool_' | sed "s/$re_cfgline/\4/" | grep "^$i$")
	if [ -z "$poolhits" ]; then
	    PFW=$(echo $PFW | sed "s/\(  *\|^\)$i\(  *\|$\)/\1/")
	fi
    done
fi

# Extract ips and ports
if [ -n "$PFW" ]; then
    if [ "$SHOWRULES" = "yes" ]; then
	echo "#: These ports are forwarded:"
    fi
    PFWPOOLS=""
    for i in $PFW; do
	eval "ip=\"\$portfw_ip_$i\""
	pools=$(set | grep '^portfw_pool_' | sed "s/$re_cfgline/\1:\4/" | grep ":$i$" | sed "s/:$i$//")
	for pool in $pools; do
	    PFWPOOLS="$PFWPOOLS $pool"
	    eval "config=\"\$portfw_pool_$pool\""
	    protos_bulk=$(echo $config | sed "s/$re_cfg/\1/")
	    protos=$(echo $protos_bulk | sed 's/,/ /g')
	    origports_bulk=$(echo $config | sed "s/$re_cfg/\2/")
	    origports=$(echo $origports_bulk | sed 's/,/ /g')
	    destport=$(echo $config | sed "s/$re_cfg/\5/")
	    [ "$origports" = "$destport" ] && destport=""
	    eval "PFWPROTO_$pool=\"\$protos\""
	    eval "PFWORIGPORTS_$pool=\"$origports\""
	    eval "PFWIP_$pool=\"$ip\""
	    eval "PFWPORT_$pool=\"$destport\""
	    if [ "$SHOWRULES" = "yes" ]; then
        	/bin/echo -e "#:   $protos_bulk \t$origports_bulk \t->\t $ip $destport \t($pool->$i)"
	    fi
	done
    done
fi