#!/bin/bash
# monitor iptables, if labeled correctly with scripts from /usr/share/firewall/lib/firewall.utils

LOOP=5 # Seconds to loop

if [ -d /run ]; then 
  FWLOCK=/run/firewall.lock
  SYNCD=/run
else
  FWLOCK=/tmp/firewall.lock
  SYNCD=/tmp
fi

_usage () {
    echo "usage: fwmon -c | --categories"
    echo "       fwmon -l label | --label label"
    echo "       fwmon -i | --interactive"
    echo "       fwmon -a | --all"
    echo "       fwmon -h | --help"
}


_fwwait () {
sync -f $SYNCD
while [ -f $FWLOCK ]; do
   echo "firewall locked with $FWLOCK; sleeping..."
   sleep 1
   sync -f $SYNCD
done
touch $FWLOCK
}

_fwunlock () {
rm $FWLOCK 2> /dev/null
}


# Categories

_allcats() {
    # _fwwait
    echo "NYI"
    # _fwunlock
}

#__listrules () {
#}

_labellist () {
    # _fwwait
    echo "NYI"
    # _fwunlock
}

_interactive () {
    # watch nft list ruleset
    watch nft list table ipv4firewall 
}

case "$1" in
    -c|--categories)
      _allcats
      exit 0
      ;;
    -i|--interactive)
      # if [ "$2" != "" ]; then LINS=`expr $2 - 1` ; else LINS=1000 ; fi
      _interactive
      ;;
    -a|--all)
      echo not yet implemented
      exit 17
      ;;
    -l|--label)
      _labellist $2
      exit 0
      ;;
    -h|--help|*)
      _usage
      exit 0
      ;;
esac



