#!/bin/sh
#
# junkfilter
# a junk email filter system for procmail
# Copyright 1997-2001 Gregory Sutter <gsutter@zer0.org>
#
# $Id: jf,v 1.16 2002/02/18 00:48:42 gsutter Exp $
#
# Please read the file "README" and the page
# http://junkfilter.zer0.org/ before using junkfilter.
#
# jf -- unified junkfilter management

SELF=$0

if [ ! $JFDIR ]; then
	JFDIR=/usr/local/etc/junkfilter
	if [ ! -r $JFDIR/junkfilter.config ]; then
		echo "Error: please set \$JFDIR to the location of junkfilter."
		exit 66
	fi
fi

#JFUSERDIR=`grep JFUSERDIR $JFDIR/junkfilter.config | awk -F "=" '{print $2}'` || \
#	( echo "Error: junkfilter not found in $JFDIR"; exit 66 )

if [ x$1 = xchk ]; then
	cat $JFDIR/domains/* | perl -pe 'y/[A-Z]/[a-z]/' | grep $2

elif [ x$1 = xadd -o x$1 = xcta ]; then
	cat $JFDIR/domains/* | perl -pe 'y/[A-Z]/[a-z]/' | egrep -q "^$2\$" && echo "$2 already in domains list" || echo $2 >> $JFDIR/domains/general

elif [ x$1 = xbuild ]; then
	if [ x$2 = xdomains ]; then
	  cat $JFDIR/domains/[a-z]* |\
	  egrep -v "^#" |\
	  perl -pe 's/[	 ]+//g; y/[A-Z]/[a-z]/; s/
//g' |\
	  egrep -v '^$' |\
	  sort | uniq |\
	  perl -p0e 's/\012(.)/\|$1/g; s/\./\\./g; s/\015//g; s/\|{2,}/\|/g' > $JFDIR/jf-domains
	  echo "Done. $2 built."

	elif [ x$2 = xaddresses ]; then 
	  cat $JFDIR/addresses |\
	  egrep -v "^#" | sort |\
	  perl -p0e 's/\012(.)/\|$1/g; s/\015//g; s/\|{2,}/\|/g' > $JFDIR/jf-addresses
	  echo "Done. $2 built."

	elif [ x$2 = xip ]; then 
	  cat $JFDIR/ip |\
	  egrep -v "^#" | sort |\
	  perl -p0e 's/\012(.)/\|$1/g; s/\./\\./g; s/\015//g; s/\|{2,}/\|/g' > $JFDIR/jf-ip
	  echo "Done. $2 built."

	elif [ x$2 = xbodychk ]; then 
	  cat $JFDIR/bodychk |\
	  egrep -v "^#" | sort |\
	  perl -p0e 's/\012(.)/\|$1/g; s/\015//g; s/\|{2,}/\|/g' > $JFDIR/jf-bodychk
	  echo "Done. $2 built."

	elif [ x$2 = xdialups ]; then 
	  cat $JFDIR/dialups |\
	  egrep -v "^#" | sort |\
	  perl -p0e 's/\012(.)/\|$1/g; s/\./\\./g; s/\015//g; s/\|{2,}/\|/g' > $JFDIR/jf-dialups
	  echo "Done. $2 built."

	elif [ x$2 = xheaders ]; then 
	  cat $JFDIR/headers |\
	  egrep -v "^#" | sort |\
	  perl -p0e 's/\012(.)/\|$1/g; s/\015//g; s/\|{2,}/\|/g' > $JFDIR/jf-headers
	  echo "Done. $2 built."

	elif [ x$2 = xwhite ]; then 
	  cat $JFDIR/white |\
	  egrep -v "^#" | sort |\
	  perl -p0e 's/\012(.)/\|$1/g; s/\015//g; s/\|{2,}/\|/g' > $JFDIR/jf-white
	  echo "Done. $2 built."

	elif [ x$2 = xall ]; then
	  $SELF build addresses
	  $SELF build bodychk
	  $SELF build dialups
	  $SELF build domains
	  $SELF build headers
	  $SELF build ip
	  $SELF build white
	  echo "Done. $2 built."

	elif [ x = x ]; then
	  echo "I can build 'addresses', 'bodychk', 'dialups', 'domains',"
	  echo "'headers', 'ip', 'white', 'all'"
	  exit 1

	fi

elif [ x$1 = xget ]; then
	echo "  You can get the latest list of domains from:"
	echo "  http://junkfilter.zer0.org/pkg/dev/domains/general"

elif [ x$1 = xrel ]; then
	if [ ! x$3 = x ]; then
	  JFELOC=$3
	  if [ ! -d $JFELOC ]; then
	    echo "Usage: $SELF rel <version> <package location>"
	    exit 1
	  fi
	else
	  echo "Usage: $SELF rel <version> <package location>"
	  exit 1
	fi
	if [ ! x$2 = x ]; then
	  JFEVER=$2
	  if [ ! -d $JFELOC/$JFEVER ]; then
	    mkdir $JFELOC/$JFEVER
	  else
	    echo "That version already exists!"
	    exit 1
	  fi
	else
	  echo "Usage: $SELF rel <version> <package location>"
	  exit 1
	fi
	  JFEDIR=$JFELOC/$JFEVER
	  TMPDIR=/tmp
	  rm $JFEDIR/*
#	  $SELF get
	  $SELF build all
	  mkdir $TMPDIR/junkfilter
	  cp -p $JFDIR/* $TMPDIR/junkfilter
	  mkdir $TMPDIR/junkfilter/domains
	  cp -p $JFDIR/domains/* $TMPDIR/junkfilter/domains
	  cd $TMPDIR
	  tar -cf - junkfilter | gzip -9 > $JFEDIR/junkfilter-$JFEVER.tar.gz
	  mv $TMPDIR/junkfilter/* $JFEDIR
	  rmdir $TMPDIR/junkfilter
	  rm $JFELOC/current
	  rm $JFELOC/junkfilter.tar.gz
	  cd $JFELOC
	  ln -s $JFEVER/junkfilter-$JFEVER.tar.gz junkfilter.tar.gz
	  ln -s $JFEVER current
	  echo "Done.  New junkfilter release in $JFEDIR."

elif [ x$1 = x ]; then
	echo "Command syntax: $SELF <command> <option>"
	echo "commands: add [domain], chk [domain], cta [domain], build [option],"
	echo "          get (new gulf-blocks domains file), rel (build release)"
	echo "options: addresses, domains, bodychk, dialups, ip, headers, white"
fi

#eof
