#!/bin/bash
#
# $Id: xvb-capt,v 1.8 2024/08/19 12:26:54 gosha Exp $
#
# vim:textwidth=80:tabstop=4:shiftwidth=4:smartindent:autoindent

# Check if is already running.
PROC=`ps ax | grep '[Tt]cpdump' | grep /opt/VirtualPBX/tmp/pcaps/sip.pca[pP]`
if [ "x$PROC" != "x" ]; then
	echo "tcpdump is already running." 
	exit 1
fi

TCPDUMP_SIZE=10
TCPDUMP_CHUNKS=10
TCPDUMP_DEV=any
TCPDUMP_PORT=5060
TMP_DIR=`/opt/VirtualPBX/contrib/utils/xvb_get_conf.pl PATH_TMP_DIR`

RC_CONFIG=${1-"/opt/VirtualPBX/etc/xvb-rc.cfg"}
. ${RC_CONFIG}

#
# Don't die if stdout/stderr can't be written to
#
trap '' PIPE

run_capt()
{
	cd /tmp
	mkdir -p "$TMP_DIR/pcaps"
	while :; do 
		/usr/sbin/tcpdump -nn -s0 -i "$TCPDUMP_DEV" -w "$TMP_DIR/pcaps/xvb-sip.pcap" -C "$TCPDUMP_SIZE" -W "$TCPDUMP_CHUNKS" port "$TCPDUMP_PORT" -Z root
		sleep 5
	done
}

if [ -f /usr/sbin/tcpdump ]; then
	run_capt &
fi
