#!/bin/sh

PRODUCT=scylla
VERSION=4.3.1

if test -f version
then
	SCYLLA_VERSION=$(cat version | awk -F'-' '{print $1}')
	SCYLLA_RELEASE=$(cat version | awk -F'-' '{print $2}')
else
	DATE=$(date +%Y%m%d)
	GIT_COMMIT=$(git log --pretty=format:'%h' -n 1)
	SCYLLA_VERSION=$VERSION
	# For custom package builds, replace "0" with "counter.your_name",
	# where counter starts at 1 and increments for successive versions.
	# This ensures that the package manager will select your custom
	# package over the standard release.
	SCYLLA_BUILD=0
	SCYLLA_RELEASE=$SCYLLA_BUILD.$DATE.$GIT_COMMIT
fi

if [ -f build/SCYLLA-RELEASE-FILE ]; then
	RELEASE_FILE=$(cat build/SCYLLA-RELEASE-FILE)
	GIT_COMMIT_FILE=$(cat build/SCYLLA-RELEASE-FILE |cut -d . -f 3)
	if [ "$GIT_COMMIT" = "$GIT_COMMIT_FILE" ]; then
		exit 0
	fi
fi

echo "$SCYLLA_VERSION-$SCYLLA_RELEASE"
mkdir -p build
echo "$SCYLLA_VERSION" > build/SCYLLA-VERSION-FILE
echo "$SCYLLA_RELEASE" > build/SCYLLA-RELEASE-FILE
echo "$PRODUCT" > build/SCYLLA-PRODUCT-FILE
