#!/bin/bash

function buildArtifact () {
  /opt/chefdk/embedded/bin/rake
}

function getCurrentVersion () {
  local version=$(grep version metadata.rb | awk '{print $2}' | tr -d \'\")
  echo ${version}
}

function getIncrementedVersion () {
  version=$(getCurrentVersion)
  echo $version | awk -F'[.]' '{print $1 "." $2 "." $3+1}'
}

function incrementVersion () {
  export currentVersion=$(getCurrentVersion)
  export nextVersion=$(getIncrementedVersion)

  sed "s/$currentVersion/$nextVersion/" metadata.rb > metadata.rb.tmp
  rm -f metadata.rb
  mv metadata.rb.tmp metadata.rb
}

function deploy () {
  repo_name="internal-releases"
  if [[ $1 == *SNAPSHOT ]]; then
    repo_name="internal-snapshots"
  fi

  mvn deploy:deploy-file -Dfile=$(echo *.tar.gz) -DrepositoryId=alfresco-internal -Durl=https://artifacts.alfresco.com/nexus/content/repositories/$repo_name -DgroupId=org.alfresco.devops -DartifactId=img-trials -Dversion=$1 -Dpackaging=tar.gz
}

function deploySnapshot () {
  buildArtifact
  current_version=$(getCurrentVersion)
  deploy "$current_version-SNAPSHOT"
}

function release () {
  buildArtifact
  deploy $(getCurrentVersion)
  incrementVersion
}

export PATH=/opt/chefdk/embedded/bin/:$PATH
export PATH=/opt/apache-maven/bin:$PATH

MODE=$1

if [ "$MODE" == "snapshot" ]; then
  deploySnapshot
elif [ "$MODE" == "nextversion" ]; then
  getIncrementedVersion
elif [ "$MODE" == "thisversion" ]; then
  getCurrentVersion
else
  release
fi
