#!/bin/sh
#
# We need this wrapper because tethereal does not work with 
# fastpath. The packets never make it to the packet tap. 
if [ ! -n "`ps -ea | grep tethereal_bin`" ]; then
    echo "Disabling fastpath..."
    /opt/meru/bin/fastpath disable
fi

#ethereal has a memory leak, so we are setting the virtual memory to 64Mb
ulimit -v 64000 # set the virtual memory to 64000 kbytes
exec 2>/tmp/tethereal_error # redirecting error to tmp file

sleep 1
/opt/meru/bin/tethereal_bin "$@"
if [[ $(grep -c 'GLib-ERROR' /tmp/tethereal_error) != 0 ]]; then
    # if tethereal has exited becasue of memory then display message to user
    echo "Exiting capture-packets because it exceeded the allowed memory"
else
    # if tethereal has exited because of syntax or some other reason, display same to user
    cat /tmp/tethereal_error
fi 

exec 2>&1
ulimit -v unlimited

if [ ! -n "`ps -ea | grep tethereal_bin`" ]; then 
    echo "Restoring fastpath..."
    /opt/meru/bin/fastpath restore
fi

