#!/bin/bash
#
# JetBackup @ package
# Release authentication lockout by IP via Panel/ClearAuthThrottle.
#

JBAPI=/usr/bin/jetbackup5api

usage() {
	cat <<EOF
Usage: $(basename "$0") --ip <ip>

Release lockout for the given IP.

Example:
  $(basename "$0") --ip 1.2.3.4
EOF
}

if [[ "$1" == "-h" || "$1" == "--help" ]]; then
	usage
	exit 0
fi

if [[ "$1" != "--ip" || -z "$2" ]]; then
	usage >&2
	exit 1
fi

IP="$2"

RESPONSE=$( "$JBAPI" -F Panel/ClearAuthThrottle -D "ip=$IP" )
RESULT=$( echo "${RESPONSE}" | grep '^success:' | sed 's/^success:\s//' )
MESSAGE=$( echo "${RESPONSE}" | grep '^message:' | sed 's/^message:\s//' )

if [[ $RESULT -eq 1 ]]; then
	echo "[ip=$IP] ${MESSAGE}"
	exit 0
fi

echo "[ip=$IP] ${MESSAGE}" >&2
exit 1
