#!/bin/sh

# fetch a list of "empty" mail messages for whitelisting
# This script should be run at most once a day

# Rhyolite Software DCC 1.2.16-1.4 $Revision$


DCC_HOMEDIR=/var/lib/dcc
DEBUG=
# check the args once to get the home directory
while getopts "xh:t:" c; do
    case $c in
	x) DEBUG=yes; set -x;;
	h) DCC_HOMEDIR="$OPTARG";;
	*) ;;
    esac
done
. $DCC_HOMEDIR/dcc_conf

SRC=http://www.iecc.com/dcc-testmsg-whitelist.txt
TGT=testmsg-whitelist
FETCH=wget

USAGE="`basename $0`: [-x] [-h homedir] [-f fetch-pgm] [-s src-URL] [-t tgt]"
OPTIND=1
while getopts "xnvh:l:o:s:" c; do
    case $c in
	x) ;;
	h) ;;
	f) FETCH="$OPTARG";;
	s) SRC="$OPTARG";;
	t) TGT="$OPTARG";;
	*) echo 1>&2 "$USAGE"; exit 1;;
    esac
done
shift `expr $OPTIND - 1 || true`
if test "$#" -ne 0; then
    echo 1>&2 "$USAGE"
    exit 1
fi


SFILE=`expr "$SRC" : '.*/\([^/][^/]*\)'`
if test -z "$SFILE"; then
    SFILE="$SRC"
    if test -z "$SFILE"; then
	echo 1>&2 "source file not specified"
	exit 1
    fi
fi
LOG="$TGT.log"

cd $DCC_HOMEDIR

# don't bother if the file exists and is not at least 2 weeks old
if test -f "$TGT"; then
    if test "`find $TGT -mtime -14 -type f`"; then
	date "+%n%n####################%n%x %X: $TGT is too recent to fetch again" >> $LOG
	exit 0
    fi
fi

# use fetch, wget, curl, or ftp that understands URLs
if test "$FETCH" = wget; then
    wget --output-file $LOG --recursive --no-host-directories \
	--passive-ftp $SRC >$LOG 2>&1
    FETCH=
fi
if test "$FETCH" = fetch; then
    fetch -p -q $SRC >$LOG 2>&1
    FETCH=
fi
if test "$FETCH" = curl; then
    curl -s -S --connect-timeout 30 --max-time 600 $SRC >$LOG 2>&1
    FETCH=
fi
if test "$FETCH" = ftp; then
    ftp -p $SRC  >$LOG 2>&1
    FETCH=
fi
# if some other program was specified, use it and hope it is simple enough
if test ! -z "$FETCH"; then
    $FETCH $SRC
fi

if test ! -s "$SFILE"; then
    echo 1>&2 "failed to fetch $SFILE"
    exit 1
fi
touch $SFILE		# work around wget timestamping

if test "$SFILE" != "$TGT"; then
    mv -f "$SFILE" "$TGT"
fi
