version: 0.2

phases:
  install:
    runtime-versions:
      java: "$JAVA_RUNTIME"

  pre_build:
    commands:
    - git config --global user.name "AWS"
    - git config --global user.email "<>"
    - 'PUBLIC_REPOSITORY_URL="https://$GITHUB_ACCESS_TOKEN@github.com/aws/aws-sdk-java-v2.git"'

  build:
    commands:
    - mkdir release
    - cd release
    -
    - git clone -o public -b release $PUBLIC_REPOSITORY_URL .
    - echo 'For debugging, running version command without -q'
    - mvn -Dexec.executable=echo -Dexec.args='${project.version}' --non-recursive exec:exec
    - RELEASE_VERSION=`mvn -q -Dexec.executable=echo -Dexec.args='${project.version}' --non-recursive exec:exec`
    - echo "Release version - $RELEASE_VERSION"
    -
    - MAJOR=$(echo $RELEASE_VERSION | cut -d'.' -f1)
    - MINOR=$(echo $RELEASE_VERSION | cut -d'.' -f2)
    - POINT=$(echo $RELEASE_VERSION | cut -d'.' -f3)
    - NEW_VERSION_SNAPSHOT="$MAJOR.$MINOR.$((POINT + 1))-SNAPSHOT"
    - echo "New snapshot version - $NEW_VERSION_SNAPSHOT"
    -
    - git checkout master
    - git merge public/release --no-edit
    -
    - MASTER_VERSION=`mvn -q -Dexec.executable=echo -Dexec.args='${project.version}' --non-recursive exec:exec`
    - echo "Master version - $MASTER_VERSION"
    -
    - |
      if [ "$MASTER_VERSION" != "$NEW_VERSION_SNAPSHOT" ]; then

        mvn versions:set -DnewVersion=$NEW_VERSION_SNAPSHOT -DgenerateBackupPoms=false -DprocessAllModules=true || { echo "Failed to update POMs to next snapshot version"; exit 1; }
        sed -i -E "s/(<version>).+(<\/version>)/\1$RELEASE_VERSION\2/" README.md
        sed -i -E "s/(<awsjavasdk.previous.version>).+(<\/awsjavasdk.previous.version>)/\1$RELEASE_VERSION\2/" pom.xml

        git commit -am "Update to next snapshot version: $NEW_VERSION_SNAPSHOT"
      fi
    -
    - git status
    - git push
