#!/bin/sh
#
# roll.sh : build a release tarball
#
# USAGE: roll.sh SOURCE [SIGNING-USER]
#
#   The "signing user" is the name of the key that you'll be signing the
#   release with.
#

# Run tests to ensure that our requirements are met

RELEASECHECK="`echo $0 | sed 's/roll.sh$/releasecheck.sh/'`"
${RELEASECHECK} || exit 1

dirname="$1"
user="$2"

if test "$#" != 1 && test "$#" != 2; then
  echo "USAGE: $0 SOURCE [SIGNING-USER]" >&2
  echo "  see the comments in this script for more info." >&2
  exit 1
fi

split="---------------------------------------------------------------------"

echo $split
echo ""

# make sure that the perms are good for the tarball
umask 022

echo "Eliminating unwanted files (e.g. .cvsignore) and generating initial"
echo "files via buildconf ..."
echo ""

find $dirname -name autom4te*.cache | xargs rm -rf
find $dirname -name STATUS | xargs rm -rf

(cd ${dirname} && ./buildconf) || exit 1

find $dirname -name autom4te*.cache | xargs rm -rf

echo ""
echo "Fixup the timestamps preventing remake of generated files."
touch $dirname/modules/ssl/ssl_expr_parse.c
touch $dirname/modules/ssl/ssl_expr_parse.h
touch $dirname/modules/ssl/ssl_expr_scan.c
echo ""

echo $split
echo ""
echo "Removing Manual Source Files."
echo ""
find $dirname/docs/manual -name \*.xml -o -name \*.xml.\* | xargs rm -rf 
find $dirname/docs/manual -name \*.xsl -o -name \*.xsl.\* | xargs rm -rf 

echo $split
echo ""
echo "Building the tarball, .gz, and .bz2 files ..."
echo ""

tar cf ${dirname}.tar ${dirname}
gzip -9 --to-stdout ${dirname}.tar > ${dirname}.tar.gz
bzip2 -9 ${dirname}.tar

echo $split
echo ""
echo "Cleaning up and signing the files ..."
echo ""

md5sum="`which md5sum md5 2> /dev/null | head -1`"
if test -x "${md5sum}"; then
  ${md5sum} ${dirname}.tar.gz > ${dirname}.tar.gz.md5
  ${md5sum} ${dirname}.tar.bz2 > ${dirname}.tar.bz2.md5
fi

if test -x "`which pgp 2> /dev/null`"; then
  if test -n "${user}"; then
    args="-u ${user}"
  fi

  pgp -sba ${dirname}.tar.gz ${args}
  pgp -sba ${dirname}.tar.bz2 ${args}
elif test -x "`which gpg 2> /dev/null`"; then
  if test -z "${user}"; then
    args="--default-key ${args}"
  else
    args="-u ${user} ${args}"
  fi

  gpg --armor ${args} --detach-sign ${dirname}.tar.gz
  gpg --armor ${args} --detach-sign ${dirname}.tar.bz2
else
  echo "PGP or GnuPG not found!  Not signing release!"
fi
