#! /bin/sh

# Script to create a .deb package from the current cvs sources.

usage ()
{
    cat <<EOF

usage: $0 [options]

options:
	-dir /path/to/mttroot
		Build package from local directory /path/to/mttroot instead of sourceforge CVS.

        -email email@address
		email address of packager
	-keep
		Keep temporary directory after package creation

	-log changelog text
		Write changelog text to changelog file
		Note: this must be the last option given.

	-name name
		name of packager
	-version #.#
		Use version #.# instead of the date to label the .deb

EOF
}

error ()
{
    echo $0: error: ${1:-"unknown error"} - aborting.
    exit 1
}

# use -email and -name to change
DEBFULLNAME=${DEBFULLNAME:-"Geraint Paul Bevan"}
DEBEMAIL=${DEBEMAIL:-"geraint@users.sourceforge.net"}

# default switches
default_sources="sourceforge"
default_logtext="snapshot from ${default_sources}."
default_version=`date --iso-8601 | sed 's/\-/./g'`

sources=${default_sources}
logtext=${default_logtext}
version="5.0."${default_version}
keeptmp="remove"

# look for command line arguments
while [ -n "`echo $1 | grep '^-'`" ]; do
    case $1 in
	-dir) # package directory instead of cvs sources
	    # should be the path to the mttroot directory
	    sources=$2
	    if [ ! "`basename ${sources}`" = "mttroot" ]; then
		error "directory ${sources} is not location of mttroot"
	    fi
	    if [ "${logtext}" = "${default_logtext}" ]; then
		logtext="snapshot from local sources (${sources})"
	    fi
	    echo "Using source directory: ${sources}"
	    shift ;;	
	-email) # email address of packager
	    DEBEMAIL=$2
	    echo "Using email address ${DEBEMAIL}"
	    shift ;;
	-h | -help | --help)
	    usage
	    exit 0 ;;	
	-keep) # keep temporary directory
	    echo "Keeping temporary files"
	    keeptmp="keep" ;;
	-log) # specify changelog text
	    shift
	    logtext="$*"
	    echo "Using changelog text: ${logtext}"
	    shift ;;
	-name) # name of packager
	    DEBFULLNAME=$2
	    echo "Using name ${DEBFULLNAME}"
	    shift ;;
	-version) # specify version
	    version=$2
	    echo "Using version number: ${version}"
	    shift ;;
	*)
	    usage
	    error "unknown option $1" ;;	    
    esac
    shift
done

topdir=`pwd`

## Main MTT
echo "Packaging mtt ..."

# create working directory
tmpdir=`mktemp -d mtt_cvs2deb.tmp.XXXXXXXXXX`
if [ ! -d ${tmpdir} ]; then
    error "cannot create temporary directory"
fi

# copy sources to working directory
cd ${tmpdir}
if [ "${sources}" = "${default_sources}" ]; then
    cvs -z3 -d:pserver:anonymous@mtt.cvs.sf.net:/cvsroot/mtt export -D now mttroot/mtt
else
    mkdir -p ./mttroot
    cp -a ${sources}/mtt ./mttroot/mtt
    for dir in $(find . -type d -name CVS); do
	rm -r $dir
    done
fi

# generate tarball of sources
cd mttroot
tar -cf mtt-${version}.tar mtt/
tar --file mtt-${version}.tar --delete mtt/lib/examples
gzip mtt-${version}.tar
cd mtt

# create .deb
echo "$0: information: a \"Could not find diffs\" error in the next few lines is probably harmless"
uupdate -u mtt-${version}.tar.gz
cd ../mtt-${version}/debian
dch -v ${version} ${logtext}
cd ..
dpkg-buildpackage -rfakeroot -k${DEBEMAIL} -m${DEBEMAIL}
cd ..

# clean up
cp mtt*.deb ${topdir}/
cd ${topdir}
if [ "${keeptmp}" = "remove" ]; then
    rm -r ${tmpdir}
else
    echo Keeping temporary directory: ${tmpdir}
fi


## Examples
echo "Packaging mtt-examples ..."

# create working directory
tmpdir=`mktemp -d mtt_cvs2deb.tmp.XXXXXXXXXX`
if [ ! -d ${tmpdir} ]; then
    error "cannot create temporary directory"
fi

# copy sources to working directory
cd ${tmpdir}
if [ "${sources}" = "${default_sources}" ]; then
    cvs -z3 -d:pserver:anonymous@mtt.cvs.sf.net:/cvsroot/mtt export -D now mttroot/mtt/lib/examples
else
    mkdir -p ./mttroot/mtt/lib/
    cp -a ${sources}/mtt/lib/examples ./mttroot/mtt/lib
    for dir in $(find . -type d -name CVS); do
	rm -r $dir
    done
fi

# generate tarball of sources
cd mttroot/mtt/lib
tar -cf mtt-examples-${version}.tar examples/
gzip mtt-examples-${version}.tar
cd examples

# create .deb
echo "$0: information: a \"Could not find diffs\" error in the next few lines is probably harmless"
uupdate -u mtt-examples-${version}.tar.gz
cd ../mtt-examples-${version}/debian
dch -v ${version} ${logtext}
cd ..
dpkg-buildpackage -rfakeroot -k${DEBEMAIL} -m${DEBEMAIL}
cd ..

# clean up
cp mtt-examples*.deb ${topdir}/
cd ${topdir}
if [ "${keeptmp}" = "remove" ]; then
    rm -r ${tmpdir}
else
    echo Keeping temporary directory: ${tmpdir}
fi

exit 0
