#!/bin/bash
port=$1
file=$2
MY_IP=62.212.108.6
if [ "$file" == "" ]; then
	echo Usage $0 portnum filename
	echo Where portnum is a number of port to make
	exit 1
fi

timestamp=`date +%s`
timenow=`date +%c`
tmpfile=/tmp/ccgraph_$timestamp

echo "<html><head><title>Country graph for port $port</title></head><body>" >$file
echo "<h1>The number of connection, sorted by country code on port $port</h1>" >>$file
echo "<b> Last update : </b> $timenow<br> " >>$file
echo "<table bgcolor=lightgrey>" >>$file
ips=`/usr/sbin/lsof -n -i :$port |/bin/grep "TCP $MY_IP"|/bin/cut -d\> -f2|/bin/cut -d\: -f1`
rm -f $tmpfile
flagdir="..\\/hublist.org\\/images\\/flags"
for ip in $ips; do 
	/usr/local/bin/geoiplookup $ip |/bin/cut -c24-25|/usr/bin/tr [:upper:] [:lower:]; 
done |/bin/sort|/usr/bin/uniq -c|/bin/sort -nr|/bin/sed -e "s/\W*\(\w*\)\W*\(.*\)/<tr><td><image src='$flagdir\/\2.gif' alt='\2'><\/td><td>\2<\/td><td>\1<\/td><\/tr>/g" >>$file
echo "</table></body></html>" >>$file
rm -f $tmpfile
