#!/bin/bash
YEAR=`date "+%Y"`
MONTH=`date "+%m"`
PWDIR=/var/pwhois
COMP='/bin/bzip2 -fq'
DECOMP=/bin/gunzip
DL='/usr/bin/wget -q'
DIFF='/usr/bin/diff -iw'
SNDMAIL='/bin/mail -e'
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/ipv4-address-space.xml && -s $PWDIR/ipv4-address-space.xml ]]; then
   mv $PWDIR/ipv4-address-space.xml $PWDIR/former.ipv4-address-space.xml;
fi

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

# Reload import latest data using pwhois-updatedb if user wants
# and if file size is nonzero 
if [[ $RELOAD -gt 0 && -s $PWDIR/ipv4-address-space.xml ]]; then
#   /usr/local/sbin/pwhois-updatedb -i $PWDIR/ipv4-address-space.xml  --import-whois-dump -l /var/log/pwhois-updatedb.log
    $DIFF /var/pwhois/former.ipv4-address-space.xml /var/pwhois/ipv4-address-space.xml | $SNDMAIL -s 'IPv4 Allocation Change Detected' victor@pwhois.org
fi
