#!/bin/bash

set -e

ACLDIR="/var/cache/xsltdai/arp-acls"
TFTPDIR="/srv/nfs/tftp/acl/generated"
TFTPPATH="acl/generated"
CFGDIR="/srv/nfs/tftp/cisco"

if [ "$#" -eq 0 ]; then
	echo "Usage: $0 arp-acl-filename [arp-acl-filename...]" >&2
	exit 1
fi

rsync -rltO --include '[a-z]*-arp' --exclude '*' --delete "${ACLDIR}"/ "${TFTPDIR}"/

cd "${CFGDIR}"

for acl in "$@" ; do
	DEVICES="$(fgrep -l "ip arp inspection filter $acl " *.cfg | sed 's/\.cfg$//')"
	if [ -z "$DEVICES" ]; then
		echo "Warning: acl '$acl' is not bound on any device. skipping.."
		continue
	fi
	echo "uploading ${acl} to" ${DEVICES} "..."
	ciscomerge "${TFTPPATH}"/"$acl" $DEVICES
	ALLDEVICES="$(echo "$ALLDEVICES" ; echo "$DEVICES")"
done

ALLDEVICES="$(echo "$ALLDEVICES" | sort -u)"

if [ -n "$ALLDEVICES" ]; then
	ciscosave $ALLDEVICES
	ciscocopy cisco $ALLDEVICES
fi

exit 0
