#!/bin/bash -eu

source /var/vcap/packages/ctl-utils/ctl_util.sh

export RUN_DIR=/var/vcap/sys/run/iptables-logger
export LOG_DIR=/var/vcap/sys/log/iptables-logger
export PIDFILE="${RUN_DIR}"/iptables-logger.pid
export CONF_DIR=/var/vcap/jobs/iptables-logger/config
export TIMEOUT=20

mkdir -p "${RUN_DIR}"
mkdir -p "${LOG_DIR}"

exec 1>> "${LOG_DIR}"/iptables-logger_ctl.out.log
exec 2>> "${LOG_DIR}"/iptables-logger_ctl.err.log

case $1 in

  start)

    exec 2> >(tee -a "${LOG_DIR}"/iptables-logger.stderr.log | logger -p user.error -t iptables-logger) \
          1> >(tee -a "${LOG_DIR}"/iptables-logger.stdout.log | logger -t iptables-logger)

    echo $$ > "${PIDFILE}"

    exec /var/vcap/packages/iptables-logger/bin/iptables-logger -config-file=/var/vcap/jobs/iptables-logger/config/iptables-logger.json

    ;;

  stop)

    stop_process

    ;;

  *)
    echo "Usage: $0 {start|stop}"

    ;;

esac
