#!/bin/bash
#
# Usage: $0 <article.wiki

right_section=false
family=""
url=""
link_seen=false
base_url="https://git.netfilter.org/iptables/tree/extensions"

maybe_emit_link() {
	if $right_section && ! $link_seen && wget -q -O /dev/null "${url}"; then
		echo "* [${url} Examples from iptables-translate testsuite]"
		link_seen=true
	fi
}

while read line; do
	case "$line" in
	"== Supported extensions ==")
		right_section=true
		;;
	"== "*)
		right_section=false
		;;
	"=== matches: "*|"=== targets: "*)
		family=$(cut -d' ' -f3 <<< "$line")
		;;
	"==== "*)
		maybe_emit_link
		ext=$(cut -d' ' -f2 <<< "$line")
		url="${base_url}/lib${family}_${ext}.txlate"
		link_seen=false
		;;
	"* [${url} "*)
		link_seen=true
		;;
	"")
		maybe_emit_link
		;;
	esac
	echo "$line"
done
