#!/bin/sh
# packetcircle-capture enable|disable|status [path-to-packetcircle]
# Gives the PacketCircle program the Linux file capabilities it needs for live capture
# (cap_net_raw, cap_net_admin) — or takes them away again. Needs root (sudo is used).
BIN="${2:-$(command -v packetcircle 2>/dev/null)}"
[ -x "$BIN" ] || { echo "packetcircle not found; give its path as the second argument" >&2; exit 1; }
BIN="$(readlink -f "$BIN")"
SU=""; [ "$(id -u)" -eq 0 ] || SU=sudo
case "$1" in
  enable)  command -v setcap >/dev/null 2>&1 || { echo "setcap missing: install libcap2-bin (Debian/Ubuntu) or libcap (Fedora/Arch/openSUSE: libcap-progs)" >&2; exit 1; }
           $SU setcap cap_net_raw,cap_net_admin=eip "$BIN" && echo "Capture enabled for $BIN" && getcap "$BIN" ;;
  disable) $SU setcap -r "$BIN" && echo "Capture permission removed from $BIN" ;;
  status)  getcap "$BIN" 2>/dev/null | grep -q cap_net_raw && echo "enabled: $(getcap "$BIN")" || echo "disabled for $BIN" ;;
  *) echo "usage: packetcircle-capture enable|disable|status [path]"; exit 2 ;;
esac
