cd $(dirname $0)
VERSION=`git describe|cut -d- -f 1`
NUMCOMMITS=`git describe|cut -d- -f 2`
if [ "$NUMCOMMITS" != "$VERSION"  ]; then
    LASTNUM=$(echo $VERSION|rev|cut -d . -f 1|rev)
    LASTNUM=$((LASTNUM+1))
    FIRSTPART=$(echo $VERSION|rev|cut -d . -f 2- |rev)
    VERSION=${FIRSTPART}.${LASTNUM}
    VERSION=$VERSION~dev$NUMCOMMITS+`git describe|cut -d- -f 3`
fi
mkdir -p dist/confluent_vtbufferd-$VERSION
cp ../LICENSE NOTICE *.c *.h Makefile dist/confluent_vtbufferd-$VERSION
cd dist
tar czf confluent_vtbufferd-$VERSION.tar.gz confluent_vtbufferd-$VERSION
cd -
mkdir -p ~/rpmbuild/SOURCES ~/rpmbuild/SPECS
cp dist/confluent_vtbufferd-$VERSION.tar.gz ~/rpmbuild/SOURCES
sed -e 's/#VERSION#/'$VERSION/  confluent_vtbufferd.spec.tmpl > ~/rpmbuild/SPECS/confluent_vtbufferd.spec
rpmbuild -ba ~/rpmbuild/SPECS/confluent_vtbufferd.spec  2> /dev/null |grep ^Wrote:
if [ $? -ne 0 ]; then
    echo "[ERROR] rpmbuild returned non-zero, run: rpmbuild -ba ~/rpmbuild/SPECS/confluent_vtbufferd.spec"
    exit 1
else
    # Clean up the generated files in this directory
    rm -rf dist 
fi
ARCH=$(uname -m)
if [ ! -z "$1" ]; then
    cp /root/rpmbuild/RPMS/$ARCH/confluent_vtbufferd-*.$ARCH.rpm $1
fi



