#!/bin/sh
#
#	trafstart	Copyleft (c)2001 Grumbler
#
#	start of the network traffic collector
#

OS=`uname`

LOG_FILE=/var/log/trafd.log

if [ "x$OS" = 'xLinux' ] ; then
   PROGRAM='/usr/sbin/trafd'
else
   PROGRAM='/usr/local/sbin/trafd'
fi

if [ $# = 0 ]; then
  if [ "_$IFF_LISTEN"="_" ]; then
	echo trafstart - start tcp/udp network data traffic collector
	echo usage:
	echo "      trafstart interface1 [interface2 ...]"
	echo "      trafstart all"
	echo "or set the enviroment variable IFF_LISTEN to interface name"
	echo "and run without parameters"
	exit 1
  else
	ifaces="$IFF_LISTEN"	
  fi
else
  if [ "$1_" = "all_" ] ; then
     	if [ "x$OS" = 'xLinux' ] ; then
	   ifaces="`ifconfig | egrep '^[a-z]{2,3}[0-9]+ .*' | cut -d ' ' -f1 | sed s/lo0//`"
	else
	   # all ifaces except loopback (lo0)
	   ifaces="`ifconfig -lu | /usr/bin/sed s/lo0//`"
	fi
  else
	ifaces="$*"
  fi
fi

for iface in ${ifaces} ; do
  if [ ${iface}_opts ] ; then
    op=\$"${iface}_opts"
    OPTS=`eval echo $op`
  else
    OPTS='-pr'
  fi  

  $PROGRAM $OPTS -i ${iface} && echo "$PROGRAM $OPTS -i ${iface}"

  if [ $? = 0 ]; then
    echo `date +"%b %e %H:%M:%S"` `hostname -s` trafd.sh: '('$iface')' started >> $LOG_FILE
  fi
done
