#!/bin/bash

if [ "$1" = "-h" ]; then
   echo "Usage: firewall FW parameters"
   exit 17
elif [ "$1" = "list" ]; then
   ls -l /etc/firewall/firewall*
   ls -l /lib/firewall/firewall*
   exit 0
fi

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

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

if [ -z "$1" -a -f /etc/firewall/firewall ]; then
   source /etc/firewall/firewall
else
  if [ -f /etc/firewall/firewall.$1 ]; then
     N=$1
     shift
     source /etc/firewall/firewall.$N
  elif [ -f /lib/firewall/firewall.$1 ]; then
     N=$1
     shift
     source /lib/firewall/firewall.$N
  fi
fi

rm $FWLOCK 2> /dev/null

