#!/bin/bash

if [ $# -eq 0 ]; then
    echo "usage: get_internet2_netblocks <output_filename>" >&2
    exit 1
fi

outfile="$1"
tmpdir=$(mktemp -d /tmp/get_i2_netblocks.XXXXXXXX) || exit 1
trap "rm -rf ${tmpdir}" EXIT QUIT HUP KILL TERM
listfile=$tmpdir/list

function last_rib()
{
	tail ${tmpdir}/index.html | grep rib\. | tail -n 1 | \
	awk -F 'href="' '{print $2}' | \
	sed -e 's/".*//'
}

function get_i2_netblocks()
{
    local YEAR=$(date +%Y)
    local MONTH=$(date +%m)
    local DAY=$(date +%d)
    local ROUTERS="ATLA CHIC HOUS KANS LOSA NEWY SALT SEAT WASH"
    local URL
    local last

    for ROUTER in ${ROUTERS}; do
	URL="http://zebra.net.internet2.edu/bgp/RIBS/${ROUTER}/${YEAR}/${MONTH}/${DAY}"
	wget -q -O ${tmpdir}/index.html "${URL}/"
	last=$(last_rib)
	wget -O ${tmpdir}/rib.gz -q "${URL}/${last}"
	zcat ${tmpdir}/rib.gz | perl zebra-dump-parser/zebra-dump-parser.pl >> ${listfile}
    done
    sort ${listfile} | uniq > ${tmpdir}/list-sorted
    mv ${tmpdir}/list-sorted ${listfile}
}

get_i2_netblocks
if [ -s ${listfile} ]; then
    cp -f ${listfile} "${outfile}"
else
    echo "unable to retrieve netblock list." >&2
    exit 1
fi
exit 0
