#!/usr/bin/awk -f

# This file is part of the aMule project.
#
# Copyright (c) 2003-2011 aMule Team ( admin@amule.org / http://www.amule.org )
# Copyright (c) 2004-2011 xmb ( http://xmb.ath.cx )
# Copyright (c) 2004-2011 Jacobo Vilella (Jacobo221)
#
# 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 St, Fifth Floor, Boston, MA  02110-1301, USA
#

##########################################
#                                        #
#   Convert eD2k HighID numbers to IPs   #
#                                        #
##########################################
#                                        #
#          Original code: xmb            #
#  Further code improvements: Jacobo221  #
#                                        #
#  Contact: IRC @ irc.freenode.net/#awk  #
#           jacobo221 at @amule dot org  #
#                                        #
##########################################
#                                        #
#  This code is distributed under terms  #
#          of the GPL license            #
#  http://www.gnu.org/copyleft/gpl.html  #
#                                        #
##########################################
#                                        #
#       Usage: id2ip.awk ID <...>        #
#                                        #
##########################################

BEGIN {

	if (ARGC == 1) {
		printf "Usage: id2ip.awk ID <...>\n"
		exit
	}

	while (num = ARGV[++i]) {

		if (ARGV[i] < 16777216) {
			printf "%s -> LowID\n",ARGV[i]
		} else if (ARGV[i] > 256*256*256*256) {
			printf "%s -> Invalid IP\n",ARGV[i]
		} else {

			m = 256 * 256 * 256

			for (c = 0; m > 0; c++ ) {
				IP[c] = int(num / m)
				num -= IP[c] * m
				m /= 256
			}

			printf "%s -> %d.%d.%d.%d\n", ARGV[i], IP[3], IP[2], IP[1], IP[0]
		}
	}

}
