#!/bin/sh
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}"

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"

cwdata="ftp://ftp.cw.net/pub/rr/split/cw.db.in.gz"

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 40MB)."
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_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

echo ""
echo "  Downloading Cable and Wireless data..."
echo ""
wget -O cwdata.data.gz $cwdata

echo ""
echo "  Processing Cable and Wireless data..."
echo ""
zcat cwdata.data.gz | ./inetnum2countryalloc > 3_cwdata.data

echo ""
echo "  Checking data..."
echo ""
err="N"
for i in `ls *.data`; do
    if test ! -s ${i} ; then
        echo "ERROR: $i is empty..."
        err="Y"
    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 ""
