#!/bin/sh
### BEGIN INIT INFO
# Provides:          Random MAC address generator
# Required-Start:    $syslog
# Required-Stop:     $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Set a random MAC for tagged interfaces
# Description:       Set a random MAC for interfaces with RANDOM_MAC
### END INIT INFO

# Author: Darren Hart <dvhart@linux.intel.com>
# Based on /etc/init.d/skeleton

PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="Set a random MAC for tagged interfaces"
NAME=genmac
RANPWD=`which ranpwd`
SCRIPTNAME=/etc/init.d/$NAME

# Exit if ranpwd is not installed
[ -x "$RANPWD" ] || exit 0

do_start() {
	# Replace every occurence of RANDOM_MAC with a unique locally
	# administered, unicast, randomly generated MAC address.
	while grep -q RANDOM_MAC /etc/network/interfaces; do
		sed -i "1,/RANDOM_MAC/s/RANDOM_MAC/$($RANPWD -m)/" /etc/network/interfaces
	done
}

case "$1" in
start)
	echo "$NAME: Setting random MAC addresses"
	do_start
	;;
stop)
	;;
*)
	echo "Usage: $SCRIPTNAME {start|stop}" >&2
	exit 3
	;;
esac

exit 0

