#!/bin/bash

set -e

tmp=$(mktemp -d)

trap cleanup EXIT
cleanup() {
    set +e
    [ -z "$tmp" -o ! -d "$tmp" ] || rm -rf "$tmp"
}

unset CDPATH
pwd=$(pwd)
#SNAPDATE=$(date +%Y%m%d)
SNAPDATE_BZR=2011-01-28 && SNAPDATE=$(echo $SNAPDATE_BZR | sed s/-//g)

cd "$tmp"
#bzr branch bzr://bzr.savannah.gnu.org/emacs/trunk emacs
bzr branch bzr://bzr.savannah.gnu.org/emacs/trunk -r date:$SNAPDATE_BZR emacs
cd emacs && eval $(autoconf configure.in | egrep '^PACKAGE_VERSION=') && cd -
PKG_VER=$PACKAGE_VERSION
PKG=emacs-${PKG_VER}.bzr${SNAPDATE}
PKG_DIR=emacs-${PKG_VER}
[ -d emacs/.bzr ] && rm -rf emacs/.bzr
[ -f emacs/.bzrignore ] && rm -f emacs/.bzrignore
mv emacs ${PKG_DIR}
tar Jcfp ${pwd}/${PKG}.tar.xz ${PKG_DIR}
cd ${pwd}
exit
