#!/bin/bash -ev
#
# do a smartmontools release
# (C) 2003-5 Bruce Allen <ballen4705@users.sourceforge.net>, 
#          Guido Guenther <agx@sigxcpu.org>
# $Id: do_release,v 1.34 2005/04/20 07:38:44 ballen4705 Exp $

# Notes on generating releases:
# (1) update NEWS
# (2) update CHANGELOG -- put in release number
# (3) update release number in configure.in and smartmontools.spec
# (4) update internal changelog in smartmontools.spec

USECVS=1

KEYID=0x841ABAE8

if [ -f /etc/redhat-release ]; then
  RPM_BASE=/usr/src/redhat/
else
  RPM_BASE=/usr/src/rpm/
fi
SOURCES=$RPM_BASE/SOURCES/

setup_cvs()
{
  CVS_SERVER=fakevalue
  unset CVS_SERVER || echo "can't unset CVS_SERVER=$CVS_SERVER"
  CVS_RSH=ssh
  CVSROOT=:ext:ballen4705@cvs.sourceforge.net:/cvsroot/smartmontools
}

get_release()
{
  VERSION=`grep 'AC_INIT' configure.in | awk '{ print $2 }' | sed s/,//g`
  RELEASE="RELEASE_${VERSION//\./_}"
  echo "Version: $VERSION"
  echo "Release: $RELEASE"
}

inc_release()
{
  MINOR=`echo $VERSION | cut -d. -f2`
  MAJOR=`echo $VERSION | cut -d. -f1`
  PERL_OLD=$MAJOR\\.$MINOR
  ((MINOR++))
  NEW_VERSION=$MAJOR.$MINOR
  PERL_NEW=$MAJOR\\.$MINOR	
  NEW_RELEASE="RELEASE_${NEW_VERSION//\./_}"
  echo "New Version: $NEW_VERSION"
  echo "New Release: $NEW_RELEASE"
}

# run automake/autoconf
if [ -f Makefile ] ; then
  make distcheck || exit 1
  make clean
  make distclean
  rm -f Makefile configure
fi

smartmontools_release_date=`date -u +"%Y/%m/%d"`
smartmontools_release_time=`date -u +"%T %Z"`
cat configure.in  | sed "s|smartmontools_release_date=.*|smartmontools_release_date=${smartmontools_release_date}|" > configure.tmp
cat configure.tmp | sed "s|smartmontools_release_time=.*|smartmontools_release_time=\"${smartmontools_release_time}\"|" > configure.in
rm -f configure.tmp

./autogen.sh

get_release

# tag CVS version
if [ $USECVS -ne 0 ] ; then
    setup_cvs
    cvs commit -m "Release $VERSION $RELEASE"
    cvs tag -d $RELEASE 
    cvs tag $RELEASE
fi

# build .tar.gz
rm -rf build
mkdir build
cd build
../configure
make distcheck || exit 1
cd ..
cp build/smartmontools-$VERSION.tar.gz $SOURCES

# build rpm
rpmbuild -ba --sign smartmontools.spec

# remove source tarball
rm -f $SOURCES/smartmontools-$VERSION.tar.gz

# increase release number:
inc_release
if [ $USECVS -ne 0 ] ; then
    perl -p -i.bak -e "s/$PERL_OLD/$PERL_NEW/" configure.in
    perl -p -i.bak -e "s/Version:\t$PERL_OLD/Version:\t$PERL_NEW/" smartmontools.spec
fi

mv -f $RPM_BASE/RPMS/i386/smartmontools-$VERSION-*.i386.rpm .
mv -f $RPM_BASE/SRPMS/smartmontools-$VERSION-*.src.rpm .
cp -f build/smartmontools-$VERSION.tar.gz .
if [ "$KEYID" ]; then
  gpg --default-key $KEYID --armor --detach-sign ./smartmontools-$VERSION.tar.gz
fi

# cleanup
rm -rf autom4te.cache build/ config.h.in Makefile.in examplescripts/Makefile.in \
       depcomp mkinstalldirs install-sh configure aclocal.m4 missing *.bak
