#! /bin/sh
#
# Prepare a release of libpqxx.  This is tailored to my machine and my current
# postgres installation.  It's not supposed to work on anything else.
#
# Usage: preprelease [sourcedir]
#
# where sourcedir is the main directory of the libpqxx source tree; defaults
# to .
# 
# The script builds libpqxx and runs a regression test from the tarball
# generated by 'make dist'.  This ensures that no important files are left out
# of the distribution archive, and that there are no regression failures in the
# released version.
#
# If successful, the script leaves a distribution tarball in /tmp.
#
# Set CONFIG_ARGS to any argument list you wish to pass to configure

LOGFILE=/tmp/libpqxx-preprelease.log
ERRLOG=/tmp/libpqxx-preprelease.err

SOURCEDIR=$1

function print() {
	echo "$1" >>$LOGFILE
	echo "$1" >>$ERRLOG
}

function log() {
	print
	print "`date`: $1"
	print
}

function command() {
	print "$ $1"
	$2 $1 >>$LOGFILE 2>>$ERRLOG
}

rm -f $LOGFILE $ERRLOG

PQXXVERSION=`grep '\<PQXXVERSION\>' VERSION | sed -e 's/^[[:space:]A-Z_]*//' | sed -e 's/[[:space:]]*#.*$//'`

# See if debian/changelog needs updating
log "Seeing if debian/changelog is up to date..."
if ! head -n 1 debian/changelog | grep "($PQXXVERSION-" >/dev/null
then
	echo "Please update debian/changelog to mention version $PQXXVERSION!"
	exit 1
fi

# See if NEWS describes the new release
log "Seeing if NEWS is up to date..."
if ! head -n 1 NEWS | grep "^$PQXXVERSION[[:space:]]*$"
then
	echo "Please update NEWS to describe $PQXXVERSION release!"
	exit 1
fi


log "Preparing libpqxx release $PQXXVERSION."
log "Log file is $LOGFILE, errors go to $ERRLOG"

tail -f --pid=$$ $ERRLOG &


log "Ensuring PostgreSQL is running..."
command startpostgres nice

if test -z "$PGHOST"; then export PGHOST=/tmp; fi

log "Accessing PostgreSQL in $PGHOST"

set -e

if test -z "$SOURCEDIR"; then SOURCEDIR="."; fi
command "cd $SOURCEDIR"
SOURCEDIR=`pwd`

log "Building in \"$SOURCEDIR\" (`pwd`)"

DISTNAME=libpqxx-$PQXXVERSION
TARBALL=$DISTNAME.tar.gz

log "Generating autoconf/automake files"
command ./autogen.sh nice

#log "Generating reference documentation"
#mkdir -p doc/html/Reference
#rm -rf doc/html/Reference/*
#command doxygen nice
#
#log "Generating tutorial"
#mkdir -p doc/html/Tutorial
#rm -rf doc/html/Tutorial/*
#command "xmlto -o doc/html/Tutorial xhtml doc/libpqxx.xml"

log "Creating $TARBALL"
command "make dist" nice

command "rm -f /tmp/$TARBALL"
command "mv $TARBALL /tmp"

command "cd /tmp"
command "rm -rf $DISTNAME"

log "Extracting $TARBALL in /tmp"
command "tar xzf $TARBALL"
command "rm $TARBALL"
command "cd $DISTNAME"

log "Configuring"
command "./configure $CONFIG_ARGS" nice

log "Building"
command "make" nice

log "Running regression test"
command "make check" nice
grep '^PASS:' $LOGFILE
grep '^FAIL:' $LOGFILE 2>>$ERRLOG || true
grep '^All [0-9]* tests passed' $LOGFILE

log "Generating symbol table"
nm -C -D src/.libs/libpqxx.a | cut -d' ' -f 3  | grep -v '^_' | grep -v '^$' | sort -u -o $SOURCEDIR/lib/SYMBOLS.$PQXXVERSION

log "Creating release version of $TARBALL"
command "make dist" nice
command "mv $TARBALL .."
command "cd .."
command "rm -r $DISTNAME"

log "Finished.  Release tarball is /tmp/$TARBALL"

# Give output time to catch up with command prompt
sleep 2

