name: Check JDK for non-major updates

on:
  schedule:
    - cron: '0 0 1 * *' # run once a month at the first day of month
  workflow_dispatch:

env:
  JDK_VERSION: '23.0.1+11'
  JDK_VENDOR: temurin
  RUNTIME_VERSION_HELPER: >
    public class Test {
      public static void main(String[]  args) {
          System.out.println(Runtime.version());
      }
    }

jobs:
  check-version:
    name: Checkout latest jdk version
    runs-on: ubuntu-latest
    env:
        JDK_MAJOR_VERSION: 'toBeFilled'
    steps:
      - name: Determine current major version
        run: echo 'JDK_MAJOR_VERSION=${{ env.JDK_VERSION }}'.substring(0,20) >> "$env:GITHUB_ENV"
        shell: pwsh
      - name: Checkout latest JDK ${{ env.JDK_MAJOR_VERSION }}
        uses: actions/setup-java@v4
        with:
          java-version: ${{ env.JDK_MAJOR_VERSION}}
          distribution: ${{ env.JDK_VENDOR }}
          check-latest: true
      - name: Determine if update is available
        id: determine
        shell: pwsh
        run: |
          $latestVersion = 0,0,0,0 #INTERIM, UPDATE, PATCH and BUILD
          $currentVersion = 0,0,0,0

          # Get the latest JDK runtime version
          "${env:RUNTIME_VERSION_HELPER}" | Set-Content -Path "GetRuntimeVersion.java"
          $latestVersionString = & java GetRuntimeVersion.java
          $runtimeVersionAndBuild = $latestVersionString.Split('+')
          if($runtimeVersionAndBuild.Length -eq 2) {
            $latestVersion[3]=$runtimeVersionAndBuild[1];
          }
          $tmp=$runtimeVersionAndBuild[0].Split('.')
          for($i=0;$i -lt $latestVersion.Length; $i++) {
            $latestVersion[$i]=$tmp[$i+1];
          }

          # Get the current JDK version
          $runtimeVersionAndBuild = '${{ env.JDK_VERSION}}'.Split('+')
          if($runtimeVersionAndBuild.Length -eq 2) {
            $currentVersion[3]=$runtimeVersionAndBuild[1];
          }
          $tmp=$runtimeVersionAndBuild[0].Split('.')
          for($i=0;$i -lt $currentVersion.Length; $i++) {
            $currentVersion[$i]=$tmp[$i+1];
          }

          # compare
          for($i=0; $i -lt $currentVersion.Length ; $i++) {
            if($latestVersion[$i] -gt  $currentVersion[$i]){
              echo 'UPDATE_AVAILABLE=true' >> "$env:GITHUB_OUTPUT"
              echo "LATEST_JDK_VERSION='${latestVersionString}'" >> "$env:GITHUB_OUTPUT"
              return 0;
            }
          }
      - name: Notify
        if: steps.determine.outputs.UPDATE_AVAILABLE == 'true'
        uses: rtCamp/action-slack-notify@v2
        env:
          SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK_URL }}
          SLACK_USERNAME: 'Cryptobot'
          SLACK_ICON: false
          SLACK_ICON_EMOJI: ':bot:'
          SLACK_CHANNEL: 'cryptomator-desktop'
          SLACK_TITLE: "JDK update available"
          SLACK_MESSAGE: "Cryptomator-CI JDK can be upgraded to ${{ steps.determine.outputs.LATEST_JDK_VERSION }}. Check the Nextcloud collective for instructions."
          SLACK_FOOTER: false
          MSG_MINIMAL: true