#!/bin/bash
YEAR=`date "+%Y"`
MONTH=`date "+%m"`
PWDIR=/var/pwhois
COMP='/bin/bzip2 -fq '
DECOMP=/bin/gunzip
DL='/usr/bin/wget -q'
RELOAD=0

# Parse command line parameters
while [ $# -gt 0 ]
do
    case "$1" in
        -r)  RELOAD=1;;
	-*)
            echo >&2 "usage: $0 [-r]"
	    exit 1;;
	*)  break;;	# terminate while loop
    esac
    shift
done

# Always keep compressed last copy around in case the transfer fails 
# This also removes the existing file after compression
if [[ -r $PWDIR/twnic.db && -s $PWDIR/twnic.db ]]; then
   mv $PWDIR/twnic.db $PWDIR/former.twnic.db;
   $COMP $PWDIR/former.twnic.db
fi

# Remove older temporary files if they exist (not needed)
if [ -f $PWDIR/twnic.db* ]; then
   rm -f $PWDIR/twnic.db*;
fi
   
# Download latest file
cd $PWDIR
# REMOVED: $DL
$DECOMP $PWDIR/twnic.db.gz

# Reload import latest data using pwhois-updatedb if user wants
# and if file size is nonzero 
if [[ $RELOAD -gt 0 && -s $PWDIR/twnic.db ]]; then
   /usr/local/sbin/pwhois-updatedb -i $PWDIR/twnic.db -v --import-rpsl2-dump --registry-xml /var/pwhois/ipv4-address-space.xml -l /var/log/pwhois-updatedb.log
fi
