#!/bin/bash

EBTABLES="/usr/sbin/ebtables-legacy"

[ -x "$EBTABLES" ] || exit 1

echo "# Generated by ebtables-save v1.0 on $(date)"

cnt=""
[ "x$EBTABLES_SAVE_COUNTER" = "xyes" ] && cnt="--Lc"

for table_name in $(grep -E '^ebtable_' /proc/modules | cut -f1 -d' ' | sed s/ebtable_//); do
    table=$($EBTABLES -t $table_name -L $cnt)
    [ $? -eq 0 ] || { echo "$table"; exit -1; }

    chain=""
    rules=""
    while read line; do
	[ -z "$line" ] && continue

	case "$line" in 
	    Bridge\ table:\ *)
		echo "*${line:14}"
		;;
	    Bridge\ chain:\ *)
		chain="${line:14}"
		chain="${chain%%,*}"
		policy="${line##*policy: }"
		echo ":$chain $policy"
		;;
	    *)
		if [ "$cnt" = "--Lc" ]; then
		    line=${line/, pcnt \=/ -c}
		    line=${line/-- bcnt \=/}
		fi
		rules="$rules-A $chain $line\n"
		;;
	esac
    done <<EOF
$table
EOF
    echo -e $rules
done
