#!/bin/bash

# Creates development source tarball.
# * generates development version string from git
# * cretes git archive with particular name-version prefix
# * updates setup.py to contain the version string
# * writes out the name of the tarball

# NOTE:
# Production (released) tarballs should be taken from github:
# https://github.com/upgrades-migrations/%{name}/%{version}/%{name}-%{version}.tar.gz
# It is our responsibility to update setup.py before we create version tag.

set -e

# ensure that current working directory is OK
BASE_DIR="$(dirname "$(realpath "$0")")"
pushd "$BASE_DIR" > /dev/null

# get version from git
VERSION="$(git describe --tags | sed -r -e 's/-([0-9]+)-[0-9a-z]{8}/.post\1/')"
PREFIX="preupgrade-assistant-$VERSION"

# create base archive from git
mkdir -p dist
rm -rf dist/${PREFIX}
git archive --format=tar --prefix=${PREFIX}/ HEAD | { cd dist; tar -x; }

# enter dist directory
cd dist

# update setup.py
sed -i -r "s/^(package_version\s*=\s*).*/\\1'$VERSION'/" ${PREFIX}/setup.py

# compress archive
tar -czf ${PREFIX}.tar.gz ${PREFIX}

# clean
rm -rf ${PREFIX}

# provide useful output
echo "$BASE_DIR/dist/${PREFIX}.tar.gz"

popd > /dev/null

