#!/usr/bin/env bash
# This script assumes it's being run from the root of the SA code tree
set -x

umask 002

WEBDIR=$HOME/public_html

if [ ! -d $WEBDIR ] ; then
  echo "WARNING: $WEBDIR does not exist" 1>&2
  echo 1>&2
fi

RELDIR=$WEBDIR/devel
mkdir -p $RELDIR

DISTNAME=Mail-SpamAssassin

rm -rf $DISTNAME-*               # otherwise make will fail

test -f Makefile && make distclean
perl Makefile.PL < /dev/null || 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+$//'`

DISTDNAME=$DISTNAME-$DIST_VERSION
DISTVNAME=$DISTNAME-$VERSION

make || exit $?

make text_html_doc
make distcheck

echo $VERSION > $WEBDIR/latest_version

rm -f $DISTVNAME.*

make distdir || exit $?
tar -cvf   $DISTVNAME.tar $DISTDNAME         || exit $?

gzip  -f9vc $DISTVNAME.tar > $DISTVNAME.tar.gz  || exit $?
bzip2 -f9vc $DISTVNAME.tar > $DISTVNAME.tar.bz2 || exit $?
zip   -rv - $DISTDNAME     > $DISTVNAME.zip     || exit $?

#Adding information to run gpg-agent so a non-privileged user can sign - There should be a check to see if this is done
gpg-agent --homedir $HOME/sabuildtools/sasigningkey --daemon --write-env-file "${HOME}/sabuildtools/sasigningkey/.gpg-agent-info"

for ext in tar.bz2 tar.gz zip ; do
  cp $DISTVNAME.$ext $RELDIR

  #NO LONGER SIGNING WITH SHA1 OR MD5
  #perl build/md5sum.pl $DISTVNAME.$ext > $RELDIR/$DISTVNAME.$ext.md5  || exit $?
  #perl build/sha1sum.pl $DISTVNAME.$ext > $RELDIR/$DISTVNAME.$ext.sha1 || exit $?
  perl build/sha256sum.pl $DISTVNAME.$ext > $RELDIR/$DISTVNAME.$ext.sha256 || exit $?
  perl build/sha512sum.pl $DISTVNAME.$ext > $RELDIR/$DISTVNAME.$ext.sha512 || exit $?

  rm -f $DISTVNAME.$ext.asc*
  if [ -d $HOME/sabuildtools/sasigningkey ]; then
    gpg --homedir $HOME/sabuildtools/sasigningkey --detach-sign --sign --armor $DISTVNAME.$ext || exit $?
    mv $DISTVNAME.$ext.asc $RELDIR/$DISTVNAME.$ext.asc || exit $?
  fi

  rm -f $DISTVNAME.$ext
done

test -f Makefile && make distclean
rm -f $DISTVNAME.*

#chgrp -R spamassassin $RELDIR
ls -l $RELDIR
