#!/bin/bash
#
# do a smartmontools release
# (C) 2003-9 Bruce Allen <ballen4705@users.sourceforge.net>,
#            Guido Guenther <agx@sigxcpu.org>
#            Christian Franke <smartmontools-support@lists.sourceforge.net>
# $Id: do_release 2970 2009-10-26 18:36:22Z chrfranke $

# Notes on generating releases:
# (1) update NEWS
# (2) update CHANGELOG -- put in release number
# (3) update release number in configure.in 
# (4) to test, run without '--commit'
# (5) when satisfied, add option '--commit'

set -e

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"
}

COMMIT=
RC=

case "$1" in
  --commit) COMMIT=yes; shift ;;
esac

case "$*" in
  RC[1-9]) RC="$1" ;;
  FINAL) ;;
  *) echo "Usage: $0 [--commit] RC[1-9]|FINAL"; exit 1 ;;
esac

# Check workdir
case "`/bin/pwd`" in
  */trunk/smartmontools) ;;
  *) echo "not run from trunk checkout"; exit 1 ;;
esac

if [ ! -d ../../tags ]; then
  echo "tags directory missing"; exit 1
fi

REV=`(cd ../.. && svnversion)` || exit 1
if [ -z "`echo "$REV" | sed -n '/^[0-9][0-9]*$/p'`" ]; then
  echo "Working directory not clean: $REV"; exit 1
fi

# Get release number
VERSION=`sed -n 's|^AC_INIT[^,]*, *\([0-9.]*\) *,.*$|\1|p' configure.in`
if [ -z "$VERSION" ]; then
  echo "AC_INIT not found in configure.in"; exit 1
fi
VERSIONRC="$VERSION"
RELEASE="RELEASE_${VERSION//\./_}"

if [ "$RC" ]; then
  VERSIONRC="${VERSION}-${RC/#RC/rc}"
  RELEASE="${RELEASE}_${RC}"
fi

if [ -e "../../tags/$RELEASE" ]; then
  echo "tags/$RELEASE exists"; exit 1
fi

echo "r$REV: Release $VERSIONRC $RELEASE"
set -v

# Update timestamp
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

# Create tag and commit
cd ../..
if [ "$COMMIT" = "yes" ]; then
  svn mkdir tags/$RELEASE
  svn copy trunk/smartmontools tags/$RELEASE/smartmontools
  svn commit -m "Release $VERSIONRC $RELEASE"
fi
cd trunk/smartmontools

# Build
./autogen.sh

mkdir build
cd build
../configure
make distcheck || exit 1
make maintainer-clean
cd ..

TARFILE=smartmontools-$VERSIONRC.tar.gz

mv -f build/smartmontools-$VERSION.tar.gz $TARFILE
rm -rvf build

md5sum $TARFILE > $TARFILE.md5
sha1sum $TARFILE > $TARFILE.sha1

# Increase release number
if [ -z "$RC" ]; then
  inc_release
  if [ "$COMMIT" = "yes" ]; then
    perl -p -i.bak -e "s/$PERL_OLD/$PERL_NEW/" configure.in
    # svn commit -m "Bump release number to $NEW_VERSION" configure.in
  fi
fi

