#!/bin/bash
export DEBFULLNAME="Lenovo HPC"
export DEBEMAIL="hpchelp@lenovo.com"
cd `dirname $0`
PKGNAME=$(basename $(pwd))
DPKGNAME=$(basename $(pwd) | sed -e s/_/-/)
OPKGNAME=$(basename $(pwd) | sed -e s/_/-/)
DSCARGS="--with-python3=True --with-python2=False"
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
cd ..
rm -rf /tmp/confluent
mkdir -p /tmp/confluent # $DPKGNAME
cp -a * .git /tmp/confluent # $DPKGNAME
cd /tmp/confluent
mv $PKGNAME $DPKGNAME-${VERSION}
sed -i s/%%VERSION%%/$VERSION/ $DPKGNAME-${VERSION}/debian/changelog
tar cvzf ${DPKGNAME}_${VERSION}.orig.tar.gz $DPKGNAME-${VERSION}
cd $DPKGNAME-${VERSION}
debuild --no-lintian -us -uc
if [ ! -z "$1" ]; then
    mv /tmp/confluent/${DPKGNAME}_${VERSION}*.deb $1/
fi
exit 0
