#!/bin/sh
# Nico Schottelius

files="doc/changelog lib/cdist/__init__.py"

# Stuff to take care of when doing a release
echo "Preparing next release"

# Ensure documentation builds cleanly
echo "Testing documentation..."
./build clean && ./build man || exit 1

# get version
changelog_version=$(grep '^[[:digit:]]' doc/changelog | head -n1 | sed 's/:.*//')
#git_version=$(git describe)
lib_version=$(grep ^VERSION lib/cdist/__init__.py | sed -e 's/.*= //' -e 's/"//g')

# get date
date_today="$(date +%Y-%m-%d)"
date_changelog=$(grep '^[[:digit:]]' doc/changelog | head -n1 | sed 's/.*: //')

echo "Ensure you fixed/prepared version files: $files"
echo "changelog: $changelog_version"
#echo "git: $git_version"
echo "lib: $lib_version"

if [ "$date_today" != "$date_changelog" ]; then
    echo "Messed up date, not releasing:"
    echo "Changelog: $date_changelog"
    exit 1
fi

if [ "$lib_version" != "$changelog_version" ]; then
    echo "Messed up versions, not releasing"
    exit 1
else
    echo "Versions are sane, continuing"
fi
echo "Press enter to continue"
read wait
version=$lib_version

# get target branch
branch=${version%.?}

# add tag
printf "Enter tag description for %s> " "$version"
read tagmessage
git tag "$version" -m "$tagmessage"

# Import into current version branch
printf "Press enter to git merge into $branch > "
read prompt
git checkout $branch
git merge master
git checkout master

# Publish manpages and sourcecode
printf "Press enter to publish doc/ and code/ > "
read prompt
./build web
./build pub

cat << notes
To be done manually...

    - freecode release
    - blog entry
    - linkedin entry
    - mailinglist update

notes