#!/usr/bin/env bash
#*******************************************************************************
# Copyright (c) 2024 Eclipse Foundation and others.
# This program and the accompanying materials are made available
# under the terms of the Eclipse Public License 2.0
# which is available at http://www.eclipse.org/legal/epl-v20.html
# SPDX-License-Identifier: EPL-2.0
#*******************************************************************************

# Ensure dependencies are installed.
if ! command -v glab >/dev/null 2>&1; then
  echo "glab could not be found. Install it here: https://gitlab.com/gitlab-org/cli/#installation"
  exit 1
fi

## Check that we have a release version
if [ -z "$1" ]; then
  printf "Usage: \n\t$0 <release-version>\n\t$0 <release-version> <quarkus-version>"
  exit 1
fi
RELEASE="$1"
QUARKUS_VERSION="$2"
shift

## Get the personal remote name from env
REMOTE_NAME="$PERSONAL_REMOTE_NAME"
if [ -z "${REMOTE_NAME}" ]; then
  echo "A remote name should be set in environment via 'PERSONAL_REMOTE_NAME' variable"
  exit 1
fi

do_project_update() {
  ## Grab the local vars from args
  local PROJECT_NAME="$1"
  if [ -z "$PROJECT_NAME" ];then
    return
  fi
  local PROJECT_BRANCH="$2"
  if [ -z "$PROJECT_BRANCH" ];then
    PROJECT_BRANCH="main"
  fi

  ## Start processing, with first op being to check that the branch is clean and creating a release branch
  printf "\n\nChecking project $PROJECT_NAME\n"
  cd $PROJECT_NAME
  if [ -z "$(git status --porcelain)" ]; then 
    git fetch origin
    git checkout -b $REMOTE_NAME/auto/commons-$RELEASE-update origin/$PROJECT_BRANCH
  else
    echo "Not clean, cannot update build"
    FAILED_UPDATES+="$PROJECT_NAME "
    cd ..
    return
  fi

  ## Do the property updates
  update_maven_properties
  if [ -z "$(git status --porcelain)" ]; then 
    echo "Project already up to date, will not generate new MR."
    cd ..
    return
  fi

  ## Check the build and process
  check_update_success_and_process $PROJECT_NAME

  ## Reset script for next project
  cd ..
}

update_maven_properties () {
  echo "Updating Common release version"
  ## Set the eclipse API Commons version into the pom
  mvn versions:set-property -Dproperty=eclipse-api-version -DnewVersion=$RELEASE > /dev/null
  ## If there is a Quarkus version included, update the platform version
  if [ -n "$QUARKUS_VERSION" ]; then
    echo "Updating Quarkus platform version"
    mvn versions:set-property -Dproperty=quarkus.platform.version -DnewVersion=$QUARKUS_VERSION > /dev/null
  fi
}

check_update_success_and_process() {
  echo "Checking build for failure"
  make compile > /dev/null
  doesBuildSucceed="$?"
  if [ "$doesBuildSucceed" = "0" ]; then
    echo "Build successful, pushing to Gitlab"
    git add .
    git commit -m "Update commons to version $RELEASE"
    glab mr create -y --create-source-branch --push --remove-source-branch \
      --title "chore: Update Commons version to $RELEASE" \
      --description "/assign_reviewer @eclipsefdn/it/webdev/code-reviewers/java"
  else
    echo "Build failed, will not push to Gitlab"
    FAILED_UPDATES+="$1 "
    # Cleanup after self to revert to allow for chained calls
    git reset HEAD --hard
  fi
}

## Track which updates failed to log at the end
FAILED_UPDATES=()

## go to the base of the java projects dir
cd ~/localdev/eclipsefdn-webdev-toolkit/projects-java
PROJECT_NAMES=(eclipsefdn-cve-api eclipsefdn-hellosign-api eclipsefdn-profile-api eclipsefdn-working-groups-api git-eca-rest-api eclipsefdn-api-project-framework eclipsefdn-downloads-api eclipsefdn-mailing-lists-api eclipsefdn-project-adopters eclipse-openvsx-api eclipsefdn-committer-paperwork-api eclipsefdn-uss-api geoip-rest-api eclipsefdn-media-link-api eclipsefdn-foundationdb-api eclipsefdn-info-api eclipsefdn-eca-expiration-api)
for i in "${PROJECT_NAMES[@]}"; do do_project_update $i ""; done

echo "Builds requiring manual updates: $FAILED_UPDATES"

