#!/bin/sh

# Copyright (c) 2016 Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
# This file is released under the GPLv2 license.
#
# Can obtain a complete copy of the license at: http://www.gnu.org/licenses/gpl-2.0.html
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

#
# vars
#

THIS_SCRIPT_NAME=$(basename -- $0)
OINKMASTER_BIN=$(which oinkmaster)
SURICATASC_BIN=$(which suricatasc)
OUTPUT_DIR="/etc/suricata/rules"
CONFIG_FILE="/etc/suricata/suricata-oinkmaster.conf"

#
# functions
#

msg_err()
{
	echo "ERROR: ${THIS_SCRIPT_NAME}: $1" >&2
	exit 1
}

msg_err_soft()
{
	echo "WARNING: ${THIS_SCRIPT_NAME}: Refusing to run. $1" >&2
	exit 0
}

#
# main execution
#

# checks
if [ $(id -u) -ne 0 ] ; then
	msg_err "this script requires root permissions"
fi

if [ ! -x "$OINKMASTER_BIN" ] ; then
	msg_err_soft "no oinkmaster binary found"
fi

if [ ! -x "$SURICATASC_BIN" ] ; then
	msg_err_soft "no suricatasc binary found"
fi

if [ ! -r "$CONFIG_FILE" ] ; then
	msg_err_soft "unable to read config file"
fi

if [ ! -d "$OUTPUT_DIR" ] ; then
	msg_err_soft "missing output dir $OUTPUT_DIR"
fi

set -e
$OINKMASTER_BIN -C $CONFIG_FILE -o $OUTPUT_DIR
$SURICATASC_BIN -c reload-rules
exit 0
