#!/usr/bin/env bash
# shellcheck source=scripts/functions
source "$(dirname "${0}")"/../functions

# CLI
if [ "${1}" = "-h" ] || [ "${1}" = "help" ]
then
  echo "Build and deploy a release from the current project."
  echo
  echo "Arguments:"
  echo "-"
  echo
  echo "Options:"
  exit
elif [ -n "${1}" ]
then
  echo "Unknown argument '${1}'"
  exit 255
fi

if ! git diff-index --quiet HEAD || [ -n "$(git status -s)" ]
then
  doFail "Your local git repository has uncommitted changes. Please use 'git stash' and rerun on clean repo."
fi

if git remote -v | grep -q "github.com/${USER}"
then
  git remote -v
  doConfirmWarning "You seem to work on a fork. Releases should be done on the original repository!\nWe strongly recommend to abort and rerun on original repository."
fi

if [ -z "${force}" ] 
then
  if ! doDevonCommand build check-top-level-project
  then
    doFail "Release has to be performed from the top-level project or using force option."
  fi
fi

doEcho "Trying to determine current version of your project..."
current_version="$(doDevonCommand build -q get-version)" || doFail "Failed to determine version. You need to run 'mvn install' before.\n${current_version}"
release_version="${current_version/-SNAPSHOT/}"
next_version=$(doGetNextVersion "${release_version}")
if [ "${current_version}" = "${release_version}-SNAPSHOT" ]
then
  next_version="${next_version}-SNAPSHOT"
fi
if [ "${current_version}" = "${release_version}" ]
then
  doConfirmWarning "Current version is not a SNAPSHOT version!"
fi
while true
do
  echo "Current version: ${current_version}"
  echo "Release version: ${release_version}"
  echo "Next version: ${next_version}"
  if [ "${next_version/-SNAPSHOT/}" == "${next_version}" ]
  then
    doWarning "Next version is not a SNAPSHOT version!"
  fi
  echo
  if doAskToContinue "Is next version correct?" "return"
  then
    break
  else
    read -r -p "Please enter next version: " next_version
    echo
  fi
done
echo
echo "Setting version of your project to ${release_version} ..."
doDevonCommand build set-version "${release_version}" "${current_version}" cleanKeepBackup
git add -u
git commit -m "set release version to ${release_version}"
git tag -a "release/${release_version}" -m "tagged version ${release_version}"
while true
do
  echo
  echo "Building and deploying the release..."
  doDevonCommandAndReturn build release
  result=${?}
  if [ "${result}" == 0 ]
  then
    break
  else
    doEchoAttention "Release build failed!"
    if ! doAskToContinue "Do you want to retry the build (e.g. in case of a temporary network error)?" "return"
    then
      doFail "Release build failed and process aborted!\nYou should reset your local commits via 'git reset HEAD^'."
    fi
  fi
done
echo "Setting version of your project to ${next_version} ..."
doDevonCommand build set-version "${next_version}" "${current_version}" ""
git add -u
git commit -m "set next version to ${next_version}"
doEcho "Local commits and tag need to be pushed now.\nYou now have the chance to review these changes manually before they are pushed."
doAskToContinue
git push
git push --tags
echo "Done."