#!/bin/bash
#===============================================================================
#
#          FILE: allwzunban
#
#         USAGE: allwzunban <IP>
#
#   DESCRIPTION: Unban specify IP by iptables on all WZ
#
#       OPTIONS: <IP>
#                
#  REQUIREMENTS: iptables, unban, sendscript
#
#         NOTES: If you didn't specify minutes to unban, will unban on next reboot.
#
#          BUGS:  ---
#        AUTHOR: rickz (Rick Zhang), xlrickz@gmail.com
#       COMPANY: X-LEGEND Entertainment Corp.
#       CREATED: Tue Jul 26 02:42:12 EDT 2011
#      REVISION: 1.0
#
#          TODO:
#
#===============================================================================

#set -o nounset                              # Treat unset variables as an error
set -m                                       # Enable job control 

[ "$1" == "" ] && exec echo "Usage: $0 <IP> [Minutes to unban]"

[ "$2" == "" ] || UNBAN_TIME="$(date -d "$2 minute" +%H:%M)"
[ "$2" == "0" ] && exec echo "Error: There is no 0 minute later in the future."

IP="$(egrep "\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b" <<< "$1")"

[ "$IP" == "" ] && exec echo "Error: Wrong IP format."

for IP in $*;do 
	sendscript allwz nosave <<< "unban $1 $2" 
	echo "$(date +%Y-%m-%dT%H:%M:%S) unban $1 $2" >> ~/.allwzunban.log
done
