#!/bin/sh
#  Copyright (c) 2004-2005, Burton M. Strauss III - Burton@ntopsupport.com

# Download the registry data and build an updated p2c table

#  Released under GPL...

#    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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#

echo ""
echo "Making p2c table..."
echo ""

work="/tmp/p2c"
dest=`pwd`
year=`date +%Y`
month=`date +%m`
day=`date +%d`

if [ $day -eq 1 ]; then
    # Fixup for the 1st since some data isn't available until
    # late in the day
    echo "Attempting to retrieve data for ${month} ${year}"
    echo ""
    echo "**********************************************"
    echo "*WARNING:                                    *"
    echo "*                                            *"
    echo "* Some registry data isn't available until   *"
    echo "* late on the 1st of the month.              *"
    echo "*                                            *"
    echo "* We'll grab last month's data instead       *"
    echo "*                                            *"
    echo "**********************************************"
    month=$(($month - 1))
    if [ $month -eq 0 ]; then
        month=12
        year=$(($year - 1))
    elif [ $month -lt 10 ]; then
        month="0${month}"
    fi
fi

echo ""
echo " Retrieving data for ${month} ${year}"

# Temporary - use the official launch day file for April 2005
if [ "x${year}${month}" = "x200504" ]; then
  afrinic="ftp://ftp.afrinic.net/pub/stats/afrinic/delegated-afrinic-20050415"
else
  afrinic="ftp://ftp.afrinic.net/pub/stats/afrinic/delegated-afrinic-${year}${month}01"
  afrinic02="ftp://ftp.afrinic.net/pub/stats/afrinic/delegated-afrinic-${year}${month}02"
  afrinic03="ftp://ftp.afrinic.net/pub/stats/afrinic/delegated-afrinic-${year}${month}03"
fi
apnic="ftp://ftp.apnic.net/pub/stats/apnic/delegated-apnic-${year}${month}01"
arin="ftp://ftp.arin.net/pub/stats/arin/delegated-arin-${year}${month}01"
lacnic="ftp://ftp.lacnic.net/pub/stats/lacnic/delegated-lacnic-${year}${month}01"
ripe="ftp://ftp.ripe.net/ripe/stats/${year}/delegated-ripencc-${year}${month}01.bz2"

ripealt="ftp://ftp.ripe.net/ripe/dbase/split/ripe.db.inetnum.gz"
apnicalt="ftp://ftp.apnic.net/pub/apnic/whois-data/APNIC/split/apnic.db.inetnum.gz"

# We used to get updated data from Cable & Wireless.
#  They spun this off to SAAVIS, who doesn't appear to be doing it any more...
# We leave the hook in place, but code is protective...
extradata=""

echo ""
echo "  Destination: ${dest}, work directory: ${work}"
echo ""
if test -d $work; then
  echo "  Clean up old work directory..."
  echo ""
  rm -rf $work
fi

echo ""
echo "  Preparing work directory..."
echo ""
mkdir $work
cp utils/p2clib.* $work
cp utils/prefixtablegen.c $work
cp utils/inetnum2countryalloc.c $work

echo ""
echo "  Checking for a manual override file (manual.something)..."
echo ""
manual=`ls -1 -t *manual.* 2>/dev/null | head -n1`

if test "x${manual}" != "x"; then
    if test -r ${manual}; then
        echo ""
        echo "    Copying manual additions to p2c data from ${manual}"
        echo ""
        cp ${manual} ${work}/0_p2cmanual.data
    else
        echo "    Not found ... continuing ..."
    fi
else
    echo "    Not found ... continuing ..."
fi

cd $work

echo ""
echo "  Downloading data..."
echo ""
echo "         This will take a while, especially on a slow link"
echo "                  (the ripe file is over 70MB)."
echo ""
echo "                   Time for a cup of coffee..."
echo ""
gcc -c -o p2clib.o p2clib.c
gcc -lm -o prefixtablegen prefixtablegen.c p2clib.o
gcc -o inetnum2countryalloc inetnum2countryalloc.c p2clib.o

wget -O 1_afrinic.data $afrinic
rc=$?
if [ $rc -ne 0 ]; then
  wget -O 1_afrinic.data $afrinic02
  rc=$?
  if [ $rc -ne 0 ]; then
    wget -O 1_afrinic.data $afrinic03
  fi
fi
wget -O 1_apnic.data $apnic
wget -O 1_arin.data $arin
wget -O 1_lacnic.data $lacnic
wget -O 1_ripe.data.bz2 $ripe
bunzip2 1_ripe.data.bz2

echo ""
echo "  Downloading alternate RIPE data..."
echo ""
wget -O ripealt.data.gz $ripealt

echo ""
echo "  Processing alternate RIPE data..."
echo ""
zcat ripealt.data.gz | ./inetnum2countryalloc > 2_ripealt.data

echo ""
echo "  Downloading alternate APNIC data..."
echo ""
wget -O apnicalt.data.gz $apnicalt

echo ""
echo "  Processing alternate APNIC data..."
echo ""
zcat apnicalt.data.gz | ./inetnum2countryalloc > 2_apnicalt.data

if [ "x${extradata}" != "x" ]; then
  echo ""
  echo "  Downloading Extra data..."
  echo ""
  wget -O extradata.data.gz $extradata

  if [ -r extradata.data.gz ]; then
    echo ""
    echo "  Processing Extra data..."
    echo ""
    zcat extradata.data.gz | ./inetnum2countryalloc > 3_extradata.data
  else
    touch 3_extradata.data
  fi
else
  touch 3_extradata.data
fi

echo ""
echo "  Checking data..."
echo ""
err="N"
for i in `ls *.data`; do
    if test ! -r ${i} ; then
        echo "ERROR: $i is missing..."
        err="Y"
    else
      if test ! -s ${i} ; then
          echo "WARNING: $i is empty...continuing anyway..."
      fi
    fi
done
if test "${err}" = "Y"; then
    echo ""
    echo "ABORTING - files remain in $work"
    exit 1
fi

echo ""
echo "  Processing downloaded data..."
echo ""
./prefixtablegen -v *.data

echo ""
echo "  Copying file(s) to ntop directory..."
echo ""
for i in `ls p2c.*.table`; do
    echo "Copying ${i}..."
    cp $i $dest
done

echo ""
echo "  Cleanup work directory..."
echo ""
cd $pwd
rm -rf $work

echo ""
echo "Done!"
echo ""
