#!/bin/sh


# mpatrol
# A library for controlling and tracing dynamic memory allocations.
# Copyright (C) 1997-2002 Graeme S. Roy <graeme.roy@analog.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public
# License along with this library; if not, write to the Free
# Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307, USA.


# UNIX shell script to update the mpatrol library version and create a new
# release archive - this file should be used for administrative purposes only


# $Id: mupdate,v 1.19 2002/01/08 20:38:24 graeme Exp $


# Read the old release version and release date from the VERSION file.

if [ ! -f VERSION ]
then
    echo "cannot locate VERSION file" >&2
    exit 1
fi

read oldcnt olddate <VERSION

oldver=`expr "$oldcnt" : '\([^.]*\)'`
oldcnt=`expr "$oldcnt" : '[^.]*\.\(.*\)'`
oldrev=`expr "$oldcnt" : '\([^.]*\)'`
oldcnt=`expr "$oldcnt" : '[^.]*\.\(.*\)'`

newver="$oldver"
newrev="$oldrev"
newcnt=`expr "$oldcnt" + 1`
newdate=`date +"%d %B %Y %H:%M:%S %Z"`

release="$newver.$newrev.$newcnt"


# Change the mpatrol.h and version.h header files in order to update the
# MPATROL_VERSION, MP_VERNUM and MP_VERSION preprocessor macros with the latest
# version of the library.

oldvernum=`printf %d%02d%02d $oldver $oldrev $oldcnt`
newvernum=`printf %d%02d%02d $newver $newrev $newcnt`

oldversion="$oldver\\.$oldrev\\.$oldcnt"
newversion="$newver\\.$newrev\\.$newcnt"

sed "s/MPATROL_VERSION $oldvernum/MPATROL_VERSION $newvernum/" \
    src/mpatrol.h >src/mpatrol.h.new
mv src/mpatrol.h.new src/mpatrol.h
cvs commit -m "Updated to version $release" src/mpatrol.h >/dev/null 2>&1

sed -e "s/MP_VERNUM $oldvernum/MP_VERNUM $newvernum/" \
    -e "s/MP_VERSION \"mpatrol $oldversion\"/MP_VERSION \"mpatrol $newversion\"/" \
    src/version.h >src/version.h.new
mv src/version.h.new src/version.h
cvs commit -m "Updated to version $release" src/version.h >/dev/null 2>&1


# Change the version.c source file in order to update the release date strings.

olddate1=`date -d"$olddate" +"(%y\\/%m\\/%d)"`
olddate2=`date -d"$olddate" +"(%d\\.%m\\.%y)"`
olddate3=`date -d"$olddate" +"(%d %B %Y)"`

newdate1=`date -d"$newdate" +"(%y\\/%m\\/%d)"`
newdate2=`date -d"$newdate" +"(%d\\.%m\\.%y)"`
newdate3=`date -d"$newdate" +"(%d %B %Y)"`

sed -e "s/MP_VERSION \" $olddate1\"/MP_VERSION \" $newdate1\"/" \
    -e "s/MP_VERSION \" $olddate2\"/MP_VERSION \" $newdate2\"/" \
    -e "s/MP_VERSION \" $olddate3\"/MP_VERSION \" $newdate3\"/" \
    src/version.c >src/version.c.new
mv src/version.c.new src/version.c
cvs commit -m "Updated to version $release" src/version.c >/dev/null 2>&1


# Update the ChangeLog and NEWS files with all the changes since the last
# release.

rcs2log -R -u 'graeme:Graeme Roy:graeme.roy@analog.com' >ChangeLog.new 2>/dev/null
cp ChangeLog.new NEWS.new
cat ChangeLog >>ChangeLog.new
cat NEWS >>NEWS.new
vi ChangeLog.new
cmp ChangeLog ChangeLog.new >/dev/null 2>&1
if [ $? != 0 ]
then
    mv ChangeLog.new ChangeLog
    cvs commit -m "Updated to version $release" ChangeLog >/dev/null 2>&1
else
    rm -f ChangeLog.new
fi
vi NEWS.new
cmp NEWS NEWS.new >/dev/null 2>&1
if [ $? != 0 ]
then
    mv NEWS.new NEWS
    cvs commit -m "Updated to version $release" NEWS >/dev/null 2>&1
else
    rm -f NEWS.new
fi


# Write the new release version and release date to the VERSION file.

echo "$release $newdate" >VERSION
cvs commit -m "Updated to version $release" VERSION >/dev/null 2>&1


# Tag the entire distribution.

cvs tag REL_${newver}_${newrev}_${newcnt} >/dev/null 2>&1


# Remove the CVS subdirectories and .cvsignore files.

for dir in `find . -name CVS -print`
do
    rm -rf "$dir"
done
for file in `find . -name .cvsignore -print`
do
    rm -f "$file"
done


# Build the documentation.

cd doc
make all >/dev/null 2>&1
cd ../man
make all >/dev/null 2>&1
cd ..


# Create the CHECKSUMS file.

rm -f CHECKSUMS
cat <<EOF >/tmp/mupdate.md5
# Use the following command to check the integrity of this distribution:
# md5sum --check CHECKSUMS

EOF
md5sum `find . -type f -print` >>/tmp/mupdate.md5
cp /tmp/mupdate.md5 CHECKSUMS
rm -f /tmp/mupdate.md5


# Create a new release archive.

archive="mpatrol_$release.tar"

cd ..
tar cf "$archive" mpatrol
gzip "$archive"
echo "created $archive.gz" >&2
