#!/usr/bin/env bash
VERSION=$1
[ -z "$VERSION" ] && exit 1
OS=`uname`
SED_ARG='-r'

if [ $OS == "Darwin" ]
then
  SED_ARG='-E'
fi

cd docs/swagger && ./set-version $VERSION && cd ../..
if [ $? == 0 ]
then
    sed $SED_ARG "s/AC_INIT\(\[wforce\],(.*)/AC_INIT([wforce], [$VERSION])/" configure.ac >configure.tmp
    if [ $? == 0 ]
    then
        mv configure.tmp configure.ac
        read -r -p "Do you want to tag this release in git? [y/N] " response
        if [[ $response =~ ^([yY])$ ]]
        then
            SIGN=""
            read -r -p "Do you want to sign the tag? [y/N] " response
            if [[ $response =~ ^([yY])$ ]]
            then
                SIGN="--sign"
            fi
            git add -u 
            git commit -m "Set version $VERSION and tagged as v$VERSION"
            git tag -a v$VERSION -m "v$VERSION" $SIGN
            echo "Tagged this commit as v$VERSION"
        fi
    fi
fi
