#!/bin/sh
# This script is executed as part of the hotplug event with
# HOTPLUG_TYPE=iface, triggered by various scripts when an interface
# is configured (ACTION=ifup) or deconfigured (ACTION=ifdown).  The
# interface is available as INTERFACE, the real device as DEVICE.

[ -z "$ACTION" -o "$ACTION" != "commit" ] && exit 0
[ -n "$VNET_BOOTING" ] && exit 0
[ "$DEVICE" == "lo" ] && exit 0

case "$ACTION" in
	commit)
		{
			flock 8
			local action=""
			local del_list=""
			local set_list=""
			for zact in $ZACTION
			do
				action=""
				zn=${zact%%=*}
				op=${zact##*=}
				for act in ${op//,/ }
				do
					[ "$act" == "delete" ] && action="del"
					[ "$act" == "add" -a "$action" == "del" ] && action="update"
				done
				[ "$action" == "del" ] && del_list=${del_list}","${zn}
				[ "$action" == "update" ] && set_list=${set_list}","${zn}
			done
			[ -n "$del_list" ] && {
				echo "${del_list}" >/tmp/.acl_zone;
				lua /lib/access_ctl/acl_delete_rule.lua del;
				rm /tmp/.acl_zone;
			}
			[ -n "$set_list" ] && {
				echo "${set_list}" >/tmp/.acl_zone_set;
				lua /lib/access_ctl/acl_delete_rule.lua update;
				rm /tmp/.acl_zone_set;
			}
		} 8<>/tmp/interface_lock

	;;
	*)
		# do nothing
	;;
esac