#! /bin/sh

OUT_IF=eno1

[ "$IFACE" = "pbgl0" ] || exit 0
[ "$ADDRFAM" = "inet" ] || exit 0

do_start () {
    sysctl net.ipv4.ip_forward=1
    iptables --table nat --append POSTROUTING \
             --out-interface $OUT_IF -j MASQUERADE
    iptables --append FORWARD --in-interface $IFACE -j ACCEPT
    dnsmasq -C /etc/network/dnsmasq-for-pbgl.conf \
	    -x /var/run/dnsmasq-for-pbgl.pid      \
	    -h -z -i $IFACE -I lo -2 $IFACE
}

do_stop () {
    kill -TERM $(cat /var/run/dnsmasq-for-pbgl.pid) \
    && rm -f /var/run/dnsmasq-for-pbgl.pid
    sysctl net.ipv4.ip_forward=0
    iptables --table nat --delete POSTROUTING \
             --out-interface $OUT_IF -j MASQUERADE
    iptables --delete FORWARD --in-interface $IFACE -j ACCEPT
}

case "$MODE" in 
    start)
	do_start
	;;
    stop)
	do_stop
	;;
    *)
	echo "Unknown mode: \"$MODE\"" >2
	exit 1
	;;
esac

exit 0
