#!/bin/sh

defaultconfig=$(dirname $0)/hosts

if [ -z "$1" -a ! -f $defaultconfig ] ; then
	echo "Usage: $(basename $0) <config>"
fi

if [ -f "$1" ] ; then
	config=$1
else
	config=$defaultconfig
fi

hosts=$(grep -v -e prefix -e admin -e connection $config | perl -ne 's/=.*//; print')
startcmd="aodvd -D -d -l -r 3 -i eth1 &"
stopcmd="killall -9 aodvd ; lsmod | grep kaodv > /dev/null && rmmod kaodv"

for i in $hosts ; do
	echo -n "$i: "
	if [ $(basename $0) = "aodvstop" ] ; then
		ssh root@$i "$stopcmd" && echo "Killed."
	elif [ $(basename $0) = "aodvstart" ] ; then
		ssh root@$i "$startcmd" && echo "Started."
	elif [ $(basename $0) = "aodvrestart" ] ; then
		ssh root@$i "$stopcmd && $startcmd" && echo "Restarted."
	fi
done
