#!/bin/sh
#
# Purpose: creates a Mahogany tarball
# Usage:   makeMtgz [output_dir]
# Author:  VZ
# Created: 03.11.01
# Version: $Id: makeMtgz,v 1.6 2003/07/29 11:30:04 vadz Exp $
###############################################################################

# change the configuration by editing these vars
#CVSROOT=:pserver:anonymous@cvs.sourceforge.net:/cvsroot/mahogany
CVSROOT=vadz@cvs.sourceforge.net:/cvsroot/mahogany
TMPDIR=/tmp/makeMtgz-$$

# no need to edit anything below
###############################################################################

# always clean up after us
cleanup() {
   rm -rf $TMPDIR
}

trap 'cleanup; exit 4' INT TERM

# where to place the output file?
OUTPUT_DIR=${1-`pwd`}

# create tar.gz or tar.bz2?
if test "x$USE_BZIP2" != "x"; then
  GZIP=bzip2
  GZ=bz2
else
  GZIP=gzip
  GZ=gz
fi

# create temp dir
if ! mkdir $TMPDIR; then
   echo "Failed to create temporary directory $TMPDIR"
   exit 1
fi

cd $TMPDIR

# and check out the sources in it
if ! cvs -z3 -Q -d $CVSROOT co M; then
   echo "Checking out Mahogany sources failed."
   cleanup
   exit 2
fi

# extract version info from the source tree
eval `grep "^#define M_VERSION_[MR]" M/include/Mversion.h | \
	    sed 's/^#define \+\(\w\+\) \+\([0-9]\+\)/\1=\2/'`

# and create the approproately named output file
M_FULLNAME="mahogany-$M_VERSION_MAJOR.$M_VERSION_MINOR"

# remove the stuff not needed in the distribution
rm M/*.ds[pw] M/M.DSP M/M.MAK M/TODO M/maketags.cmd M/cvsfix

mv M $M_FULLNAME
OUTPUT_FILE=$OUTPUT_DIR/$M_FULLNAME.tar.$GZ
if ! tar cf - $M_FULLNAME --exclude="CVS" | $GZIP -c9 > $OUTPUT_FILE; then
   echo "Creating $OUTPUT_FILE failed."
   RC=3
fi

# finally, clean up
cd -
cleanup

exit ${RC-0}
