#!/bin/sh

umask 002

srcdir=`pwd`
tmpdir=$srcdir/tmp/rulesdir.$$
rm -rf $tmpdir; mkdir -p $tmpdir         || exit $?


PERL=${PERL:-perl}

RELDIR=$HOME/public_html/devel
mkdir -p $RELDIR

DISTNAME=Mail-SpamAssassin-rules
make || perl Makefile.PL < /dev/null || exit $?
make || exit $?
make version.env || exit $?
. ./version.env

# "3.1.0-pre2-r202405" should appear as "3.1.0-pre2" after this
VERSION=`echo $FULL_VERSION | perl -pe 's/-(r\d+)$//'`
SVNVER=`svn info | perl -ne '/Revision: (\d+)/ and print $1'`
DISTVNAME=$DISTNAME-$VERSION.r$SVNVER
DISTVBASE=$DISTNAME-$VERSION


# remove the rules files for rules we won't be shipping
rm -f rules/70_*.cf

# remove new features, unsupported in existing code in the field
# (TODO: need a better way to exclude files that require new features
# like this; judicious use of "ifplugin" helps)
# rm rules/60_somerandomfeature.cf

# and ensure the ruleset lints!
./spamassassin --lint                    || exit $?

rulesdir=rules

(
  cd $rulesdir

  # Use this to include plugin .pm files:
  # tar cvf - *.cf *.pm                  || exit $?

  # or this, to ban code from the rules:
  tar cvf - [0-9]*.cf                    || exit $?

) | gzip -9 > $tmpdir/rules.tgz         || exit $?

# ensure non-empty
[ -s $tmpdir/rules.tgz ] || exit 3

linttmp=$tmpdir/lintdir
rm -rf $linttmp
mkdir $linttmp
(
  cd $linttmp
  # check validity of tarball; also extract
  gunzip -cd < $tmpdir/rules.tgz | tar xf - || exit $?
)

sitetmp=$tmpdir/sitetmp
rm -rf $sitetmp
mkdir $sitetmp
cp rules/*.pre $sitetmp

# now, ensure the ruleset (entirely as distributed) lints, also.
# use "-p /dev/null" so any user_prefs data is ignored.
./spamassassin -x --configpath=$linttmp --siteconfigpath=$sitetmp \
               -p /dev/null --lint \
                || exit $?

# sign and get sums
rm -f $tmpdir/rules.*.asc
ssh updatesd@spamassassin.zones.apache.org \
        . /etc/profile \; \
        gpg --homedir /home/updatesd/key -bsa - \
        < $tmpdir/rules.tgz > $tmpdir/rules.tgz.asc || exit $?

(
  cd $tmpdir
  ln rules.tgz $DISTVNAME.tgz
  $PERL $srcdir/build/md5sum.pl $DISTVNAME.tgz > rules.tgz.md5    || exit $?
  $PERL $srcdir/build/sha1sum.pl $DISTVNAME.tgz > rules.tgz.sha1  || exit $?
)

for ext in tgz tgz.md5 tgz.sha1 tgz.asc ; do
  rm -f $RELDIR/$DISTVBASE.*.$ext
  if [ -f $tmpdir/rules.$ext ] ; then
    mv $tmpdir/rules.$ext $RELDIR/$DISTVNAME.$ext
    ls -l $RELDIR/$DISTVNAME.$ext
  fi
done

chgrp -R spamassassin $RELDIR
rm -rf $tmpdir

