#!/bin/bash
YEAR=`date "+%Y"`
MONTH=`date "+%m"`
PWDIR=/var/pwhois
COMP='/bin/bzip2 -fq '
DECOMP=/bin/gunzip
DL='/usr/bin/wget -q --no-check-certificate'
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/arin_db.txt && -s $PWDIR/arin_db.txt ]]; then
   mv $PWDIR/arin_db.txt $PWDIR/former.arin_db.txt;
   /usr/bin/nohup $COMP $PWDIR/former.arin_db.txt &
fi

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

# Reload import latest data using pwhois-updatedb if user wants
# and if file size is nonzero 
if [[ $RELOAD -gt 0 && -s $PWDIR/arin_db.txt ]]; then
   /usr/local/sbin/pwhois-updatedb -i $PWDIR/arin_db.txt --whois-source=arin --import-whois-dump -l /var/log/pwhois-updatedb.log
fi
