#!/bin/sh
# +--------------------------------------------------------------------------+
# | Endian Firewall                                                          |
# +--------------------------------------------------------------------------+
# | Copyright (c) 2004-2017 S.p.A. <info@endian.com>                         |
# |         Endian S.p.A.                                                    |
# |         via Pillhof 47                                                   |
# |         39057 Appiano (BZ)                                               |
# |         Italy                                                            |
# |                                                                          |
# | This program is free software; you can redistribute it and/or modify     |
# | it under the terms of the GNU General Public License as published by     |
# | the Free Software Foundation; either version 2 of the License, or        |
# | (at your option) any later version.                                      |
# |                                                                          |
# | 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.,  |
# | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.              |
# +--------------------------------------------------------------------------+

EMI=${EMI:-http://localhost:3131}

function usage() {
    echo "Usage: $0 <command> [--option[=argument]]"
    exit 1
}

if [ -z "$1" ]; then
    usage
fi

CMD=$1
shift

if echo $CMD | grep -qEe "^\-\-"; then
    usage
fi

if [ "$CMD" == "commands.migration.run" ]; then
   /sbin/jobcontrol call migration.migrate $*
   exit 0
fi

ARGS=""
for i in $*; do
    opt=$(echo $i | sed "s/^\-\-//")
    if [ "$opt" == "$i" ]; then
        echo "ERROR: Invalid option $i"
        exit 1
    fi
    if [ -z "$ARGS" ]; then
        ARGS=$opt
    else
        ARGS="$ARGS&$opt"
    fi
done

if ! /etc/init.d/emi status &>/dev/null; then
    # If the command is a reload and emi is not running, don't do anything
    if [ "$CMD" == "commands.emi.reload" ]; then
        exit 0
    fi
    logger -t emicommand -p local4.warning \
        "EMI is not running. Try to start it."
    /etc/init.d/emi start
    sleep 10
fi

curl --connect-timeout 30 --max-time 300 --user-agent emicommand -sS $EMI/manage/commands/$CMD/?$ARGS
EXIT_CODE=$?
if [ $EXIT_CODE -eq 52 ]; then
  # The server didn't reply anything, which here is not considered an error.
  EXIT_CODE=0
fi

if [ "$CMD" == "commands.emi.reload" ]; then
  EXIT_CODE=0
fi

# efw-dashboard restart fix
if [ $EXIT_CODE -eq 7 ]; then
  if [ "$ARGS" == "module=dashboard" ]; then
     EXIT_CODE=0
  fi
fi

exit $EXIT_CODE
