#!/bin/sh

# ------------------------------------------------------------------------------
#                         -= Arno's Iptables Firewall(AIF) =-
#              Single- & multi-homed firewall script with DSL/ADSL support
#
#                           ~ In memory of my dear parents ~
#
# (C) Copyright 2001-2021 by Arno van Amersfoort & Lonnie Abelbeck
# Web                   : https://github.com/arno-iptables-firewall/aif
# Email                 : a r n o DOT v a n DOT a m e r s f o o r t AT g m a i l DOT c o m
#                         (note: you must remove all spaces and substitute the @ and the .
#                         at the proper locations!)
# ------------------------------------------------------------------------------
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# version 2 as published by the Free Software Foundation.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
# ------------------------------------------------------------------------------

# Location of the main configuration file for the firewall
##########################################################
CONF_FILE=/etc/arno-iptables-firewall/firewall.conf

# Define some global variables
OPT_INDENT=""
JOB_HELPER_PATH=""

# Check sanity of environment
sanity_check()
{
  if [ -z "$JOB_HELPER_PATH" ]; then
    echo "** ERROR: Missing job-helper argument!" >&2
    return 1
  fi

  return 0
}


show_help()
{
  echo "Usage: $(basename $0) [options] {plugin_helper_path}" >&2
  echo "" >&2
  echo "Options:" >&2
  echo "--help|-h                   - Print this help" >&2
  echo "--indent=\'{indent}\'       - Use {indent} for line indention" >&2
  echo ""
}


process_commandline()
{
  # Check arguments
  while [ -n "$1" ]; do
    ARG="$1"
    ARGNAME="${ARG%%=*}"
    # Can't directly obtain value as = is optional!:
    ARGVAL="${ARG#$ARGNAME}"
    ARGVAL="${ARGVAL#=}"

    case "$ARGNAME" in
              --help|-h) show_help
                         exit 0
                         ;;
            --indent|-i) OPT_INDENT="$ARGVAL"
                         ;;
                     -*) echo "ERROR: Bad argument \"$ARG\"" >&2
                         echo "" >&2
                         show_help
                         exit 1
                         ;;
                      *) JOB_HELPER_PATH="$ARG"
                         ;;
    esac

    shift # Next argument
  done
}


############
# Mainline #
############

process_commandline "$@"

if [ ! -f "$CONF_FILE" ]; then
  echo "ERROR: Could not read configuration file ($CONF_FILE)!" >&2
  echo "" >&2
  exit 2
fi

# Source config file
. "$CONF_FILE"

# Check if the environment file exists and if so, load it
#########################################################
ENV_FILE="${0%/*}/environment"

if [ ! -f "$ENV_FILE" ]; then
  echo "** ERROR: Unable to locate environment file \"$ENV_FILE\"!" >&2
  exit 2
fi

# Source environment file
. "$ENV_FILE"

# Only proceed if environment ok
if ! sanity_check; then
  exit 2
fi

# Reset to 0, just in case
PLUGIN_RET_VAL=0

LOCK_NAME="$(basename "$JOB_HELPER_PATH")"

# Enter critical section (single lock)
if ! lock_enter_single $LOCK_NAME; then
  exit 2
fi

# Set indent
INDENT="$OPT_INDENT"

# Source helper
. "$JOB_HELPER_PATH"

# Leave critical section
if ! lock_leave $LOCK_NAME; then
  exit 2
fi

# Return helper's return code
exit $PLUGIN_RET_VAL
