#!/usr/bin/env bash

gem_name="rspectacular"
current_sha="$(git rev-parse HEAD)"

function chamber_env_var {
  local var_key="$1"

  bundle exec chamber show --as-env |
  grep $var_key |
  sed -r -n 's/.*=\"(.*)\"/\1/p'
}

export RUBYGEMS_API_KEY="$(chamber_env_var RUBYGEMS_API_KEY)"

if [ -n "$CIRCLECI" ]; then
  echo ""
  echo "Adding Rubygems Credentials to $HOME/.netrc"
  echo "--------------------------------------------------------------------------------"
  echo ""

  echo ":rubygems_api_key: ${RUBYGEMS_API_KEY}" > $HOME/.gem/credentials
  chmod 0600 $HOME/.gem/credentials

  echo "Done."
fi

echo ""
echo "Detecting the Version to Release"
echo "--------------------------------------------------------------------------------"
latest_release_tag="$(git tag | grep -E 'releases' | sort --version-sort | tail -n 1)"

if [ -n "$latest_release_tag" ]; then
  echo "The latest release detected was '${latest_release_tag}'."
  echo ""
else
  echo "No release tag found."
  echo "Skipping deploy."
  echo ""

  exit
fi

echo ""
echo "Current Version Information"
echo "--------------------------------------------------------------------------------"
echo ""
echo "Current SHA: $(git rev-parse HEAD)"
echo "Current Tag: $(git describe --exact-match HEAD 2> /dev/null)"
echo ""

if [ -n "$(git describe --exact-match HEAD 2> /dev/null | grep $latest_release_tag)" ]; then
  echo "The commit being built contains the release tag.  Releasing now."
else
  echo "The commit does not contain the latest release tag."
  echo "Skipping deploy."
  echo ""

  exit
fi

echo ""
echo "Pushing to Rubygems"
echo "--------------------------------------------------------------------------------"
deploy_successful=false

gem build *.gemspec | tee push.log

gem_file="$(cat push.log | grep -o '  File: .*' | sed -e 's-  File: \(.*\)-\1-')"

if gem push $gem_file | tee push.log; then
  deploy_successful=true
fi

if [[ "$deploy_successful" == "false" ]]; then
  echo "There was a problem pushing your gem to Rubygems."
  echo ""
  echo "Aborting deploy"
  echo ""
fi

echo ""
echo "Sending notification to Slack"
echo "--------------------------------------------------------------------------------"

release_message=""
color=""

if [ -n "$(cat push.log | grep 'Successfully registered gem')" ]; then
  release_message="${gem_name} ${latest_release_tag} has been successfully released to Rubygems"
  color="success"
elif [ -n "$(cat push.log | grep 'Repushing of gem versions is not allowed')" ]; then
  release_message="${gem_name} ${latest_release_tag} has already been pushed to Rubygems"
  color="warning"
else
  release_message="There was a problem pushing ${gem_name} ${latest_release_tag} to Rubygems"
  color="danger"
fi

curl -X POST --data-urlencode "payload={\"username\": \"rubygems\", \"text\": \"${release_message}\", \"icon_url\": \"https://pbs.twimg.com/profile_images/535452674729078784/5jL6-BA5_400x400.jpeg\"}" https://goodscout.slack.com/services/hooks/incoming-webhook?token=i7p6sAtGqMYG0owvtqmwVaDv

if ! $deploy_successful; then
  exit 1;
fi

echo ""
echo ""
echo "Deploy completed"
echo ""
