#! /bin/sh
# aoe-interfaces - set or list the allowed AoE network interfaces
# Copyright 2009, CORAID, Inc., and licensed under GPL v.2.

zero="`basename $0`"
devf=@devdir@/interfaces
sysf=/sys/module/aoe/parameters/aoe_iflist

if test -z "$*"; then
	if test -r "$sysf"; then
		cat "$sysf"
	else
		# can't read from interfaces device
		false
	fi
	exit
fi

if test "$1" = "-c"; then
	shift
	if test "$#" != "0"; then
		echo "$zero Error: -c flag takes no arguments" 1>&2
		exit 1
	fi
fi
netifs="$*"

err=no
for i in $netifs; do
	test -d "/sys/class/net/$i" || {
		echo "$zero Error: \"$i\" is not a network interface" 1>&2
		err=yes
	}
done
if test "$err" = "yes"; then
	exit 1
fi

if test -w "$sysf"; then
	printf '%s\0' "$netifs" > "$sysf"
else
	if test ! -w "$devf"; then
		echo 1>&2 "$zero: $devf does not exist or is not writeable."
		exit 1
	fi
	if test ! -c "$devf"; then
		exec 1>&2
		echo "$zero: $devf is not a character device file"
		echo "$zero: use udev or aoe-mkdevs to create it"
		exit 1
	fi
	printf '%s\0' "$netifs" > "$devf"
fi
