#!/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/ripe.db && -s $PWDIR/ripe.db ]]; then
   mv $PWDIR/ripe.db $PWDIR/former.ripe.db;
   /usr/bin/nohup $COMP $PWDIR/former.ripe.db &
fi

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

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