#! /bin/sh

# fetch a blacklist dcc.dcc-servers.net DCC servers

# This script should be run about twice an hour at an arbitrary time
#	instead of 0,30 to minimize pile-ups on the FTP server.

# -h homedir	set DCC home directory

# -o blklist	file of local blacklist entries

# -p pgm	fetch, wget, curl, or ftp

# -s srvr-host	host name of source of the common blacklist

# Rhyolite Software DCC 1.3.31-1.34 $Revision$
# Generated automatically from fetchblack.in by configure.


# other, probably local blacklist files
OBL=

exec </dev/null

DCC_HOMEDIR=/etc/dcc

DEBUG=
FNAME=dcc-servers-blacklist
SRVRS=
PGM=ftp
FTPEXPORT=
FORCE=

USAGE="`basename $0`: [-xef] [-h homdir] [-o blklist] [-p pgm] [-s srvr-host]"
while getopts "xefh:o:p:s:" c; do
    case $c in
	x) DEBUG=yes; set -x;;
	e) FTPEXPORT=yes;;
	f) FORCE=yes;;
	h) DCC_HOMEDIR="$OPTARG";;
	o) OBL="$OBL $OPTARG";;
	p) PGM=$OPTARG;;
	s) SRVRS="$SRVRS $OPTARG";;
	*) echo "$USAGE" 1>&2; exit 1;;
    esac
done
shift `expr $OPTIND - 1 || true`
if test "$#" -ne 0; then
    echo "$USAGE" 1>&2; exit 1
fi
if test -z "$SRVRS"; then
    SRVRS="http://www.dcc-servers.net/dcc http://www.rhyolite.com/anti-spam/dcc ftp://ftp.dcc-servers.net ftp://ftp.rhyolite.com"
fi
URLS=
for SRVR in $SRVRS; do
    if expr "$SRVR" : '.[hft]*tp://' >/dev/null; then
	if expr "$SRVR" : ".*/$FNAME"'$' >/dev/null; then
	    URLS="$URLS $SRVR"
	else
	    URLS="$URLS $SRVR/$FNAME"
	fi
    else
	URLS="$URLS ftp://$SRVR/$FNAME"
    fi
done

BDIR=$DCC_HOMEDIR/$FNAME
if test ! -d $BDIR; then
    mkdir $BDIR
fi
cd $BDIR

# use fetch, wget, curl, or ftp that understands URLs
rm -f $FNAME fetch-log
BASE_PGM=`basename "$PGM"`
if test "$BASE_PGM" = wget; then
    BASE_PGM=
    for URL in $URLS; do
	echo "$URL:" >>fetch-log
	$PGM -nd --retr-symlinks --recursive --no-host-directories \
	    --passive-ftp  $URL >>fetch-log 2>&1
	if test -s $FNAME; then
	    if test -z "`sed -n -e 2q -e 's/.*DOCTYPE.*/HTML/p' $FNAME`"; then
		break;
	    fi
	    rm $FNAME
	fi
	echo >>fetch-log
    done
fi

if test "$BASE_PGM" = fetch; then
    BASE_PGM=
    for URL in $URLS; do
	echo "$URL:" >>fetch-log
	HTTP_REFERER=DCC-script; export HTTP_REFERER
	$PGM -p -m $URL >>fetch-log 2>&1
	if test -s $FNAME; then
	    break;
	fi
	echo >>fetch-log
    done
fi

if test "$BASE_PGM" = curl; then
    BASE_PGM=
    for URL in $URLS; do
	echo "$URL:" >>fetch-log
	$PGM  --connect-timeout 30 --max-time 600\
	    $URL >>fetch-log 2>&1
	# --fail does not work on at least some versions of curl
	if test -s $FNAME; then
	    if test -z "`sed -n -e 2q -e 's/.*DOCTYPE.*/HTML/p' $FNAME`"; then
		break;
	    fi
	    rm $FNAME
	fi
	echo >>fetch-log
    done
fi

if test "$BASE_PGM" = ftp; then
    BASE_PGM=
    for URL in $URLS; do
	echo "$URL:" >>fetch-log
	$PGM -p $URL >>fetch-log 2>&1
	if test -s $FNAME; then
	    break;
	fi
	echo >>fetch-log
    done
    # if that did not work, try ancient FTP
    if test ! -s $FNAME; then
	for URL in $URLS; do
	    HOST=`expr "$URL" : "ftp://\([^/]*\)/"`
	    RFILE=`expr "$URL" : "ftp://[^/]*/\(.*\)"`
	    if test -z "$RFILE" -o -z "$HOST"; then
		continue
	    fi
	    echo "$URL:" >>fetch-log
	    (echo "user anonymous `hostname`"; echo "get $RFILE $FNAME")    \
		| ftp -n $HOST >>fetch-log 2>&1
	    if test -s $FNAME; then
		break;
	    fi
	    # some versions of ftp like to leave empty files
	    rm -f $FNAME
	    echo >>fetch-log
	done
    fi
fi

if test ! -s $FNAME; then
    echo "Unable to fetch blacklist"
    exit 1
fi

# put local list last so that it can override
cat  $FNAME $OBL > new-blacklist
if cmp new-blacklist ../blacklist 1>/dev/null 2>&1; then :
    rm new-blacklist
else
    FORCE=yes
fi
if test -n "$FORCE"; then
    mv new-blacklist ../blacklist
    if test -n "$FTPEXPORT"; then
	cp $FNAME ~ftp
    fi
fi
