#!/bin/sh
# Root side of MoxiVue packet capture, run through pkexec: tcpdump on one interface with a time
# limit, packets written to standard output for the app of the user who asked. Arguments are checked.
#   moxivue-capture <seconds> <interface> <snaplen> [capture filter]
set -e
case "$1" in ''|*[!0-9]*) echo "bad time limit" >&2; exit 2 ;; esac
case "$3" in ''|*[!0-9]*) echo "bad snap length" >&2; exit 2 ;; esac
[ "$1" -ge 1 ] && [ "$1" -le 3600 ] || { echo "time limit out of range" >&2; exit 2; }
echo "$2" | grep -Eq '^[A-Za-z0-9_.@-]{1,32}$' || { echo "bad interface name" >&2; exit 2; }
TCPDUMP=$(command -v tcpdump || echo /usr/bin/tcpdump)
if [ -n "$4" ]; then
  exec timeout -s INT "$1" "$TCPDUMP" -i "$2" -U -n -s "$3" -w - "$4"
else
  exec timeout -s INT "$1" "$TCPDUMP" -i "$2" -U -n -s "$3" -w -
fi
