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

WEBDIR=$SA_WEBDIR
test -n "$WEBDIR"  || WEBDIR=$HOME/site

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

GPGSIGN=$SA_GPGSIGN
test -x "$GPGSIGN" || GPGSIGN=$HOME/sabuildtools/bin/gpgsign

DEVDIR=$WEBDIR/devel
RELDIR=$WEBDIR/released
mkdir -p $DEVDIR $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

# $VERSION will be x.y.z for released not marked $IS_DEVEL_BUILD, else
# x-y-latest while $DIST_VERSION is always the x.y.z variant.
VERSION=`echo $FULL_VERSION | perl -pe 's/\.\d+(-.*)-r\d+$/-dev/'`

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

# delete old CVS files
find . -name '.#*' -print | xargs rm -f

make || exit $?

make text_html_doc
make distcheck

echo $VERSION > $WEBDIR/latest_version

rm -f $DISTVNAME.*

make distdir || exit $?
tar -cWvf   $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 $?

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

  build/md5sum.pl $DISTVNAME.$ext > $DEVDIR/$DISTVNAME.$ext.md5  || exit $?
  build/sha1sum.pl $DISTVNAME.$ext > $DEVDIR/$DISTVNAME.$ext.sha1 || exit $?

  rm -f $DISTVNAME.$ext.asc*
  if [ -x "$GPGSIGN" ]; then
    $GPGSIGN $DISTVNAME.$ext                           || exit $?
    mv $DISTVNAME.$ext.asc $DEVDIR/$DISTVNAME.$ext.asc || exit $?
  fi

  rm -f $DISTVNAME.$ext
done

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

# ( cd $DEVDIR && svn add *SpamAssassin* )

ls -l $DEVDIR

