# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

name: Publish Release Candidate in ASF SVN
run-name: ${{ github.actor }} is publishing the Release Candidate on ASF SVN🚀

on:
  workflow_dispatch:
    inputs:
      RC_TAG:
        description: Release Candidate Tag (release-x.xx-RCx)
        required: true
      APACHE_ID:
        description: Your Apache id
        required: true
      APACHE_PASSWORD:
        description: Your Apache password
        required: true
  
jobs:
  finalizeReleaseCandidateInSVN:
    runs-on: ubuntu-24.04
    steps:
      - name: Checkout
        uses: actions/checkout@v6.0.2
        with:
          ref: ${{ github.event.inputs.RC_TAG }}
          
      - name: Get Maven POM version
        id: standardMavenVersion
        run: echo "version=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout)" >> $GITHUB_OUTPUT
      
      - name: Extract branch name
        id: branchExtracted
        shell: bash
        run: echo "name=${{ github.event.inputs.RC_TAG }}" >> $GITHUB_OUTPUT
        
      - name: Extract Release Candidate revision
        id: rcRevision
        run: |
          rcRevision="${{ github.event.inputs.RC_TAG }}"
          set -- $rcRevision
          echo "version=${1##*-}" >> $GITHUB_OUTPUT
      
      - name: Extract branch without release prefix
        id: branchWithoutPrefixExtracted
        run: |
          branchWithoutPrefixExtracted="${{ github.event.inputs.RC_TAG }}"
          set -- $branchWithoutPrefixExtracted
          echo "version=${1#*-*}" >> $GITHUB_OUTPUT
      
      - name: Download artifacts from GitHub and Upload artifacts into ASF SVN
        run: |
          svn co https://dist.apache.org/repos/dist/dev/manifoldcf
          mkdir -p apache-manifoldcf-${{ steps.branchExtracted.outputs.name }}
          cd apache-manifoldcf-${{ steps.branchExtracted.outputs.name }}
          
          if (svn ls "apache-manifoldcf-${{steps.standardMavenVersion.outputs.version}}-dev-bin.zip"); then
            echo "Removing existing apache-manifoldcf-${{steps.standardMavenVersion.outputs.version}}-dev-bin.zip"
            svn delete "apache-manifoldcf-${{steps.standardMavenVersion.outputs.version}}-dev-bin.zip"
          fi
          
          if (svn ls "apache-manifoldcf-${{steps.standardMavenVersion.outputs.version}}-dev-bin.tar.gz"); then
            echo "Removing existing apache-manifoldcf-${{steps.standardMavenVersion.outputs.version}}-dev-bin.tar.gz"
            svn delete "apache-manifoldcf-${{steps.standardMavenVersion.outputs.version}}-dev-bin.tar.gz"
          fi
          
          if (svn ls "apache-manifoldcf-${{steps.standardMavenVersion.outputs.version}}-dev-lib.tar.gz"); then
            echo "Removing existing apache-manifoldcf-${{steps.standardMavenVersion.outputs.version}}-dev-lib.tar.gz"
            svn delete "apache-manifoldcf-${{steps.standardMavenVersion.outputs.version}}-dev-lib.tar.gz"
          fi
              
          if (svn ls "apache-manifoldcf-${{steps.standardMavenVersion.outputs.version}}-dev-lib.zip"); then
            echo "Removing existing apache-manifoldcf-${{steps.standardMavenVersion.outputs.version}}-dev-lib.zip"
            svn delete "apache-manifoldcf-${{steps.standardMavenVersion.outputs.version}}-dev-lib.zip"
          fi
          
          if (svn ls "apache-manifoldcf-${{steps.standardMavenVersion.outputs.version}}-dev-src.tar.gz"); then
            echo "Removing existing apache-manifoldcf-${{steps.standardMavenVersion.outputs.version}}-dev-src.tar.gz"
            svn delete "apache-manifoldcf-${{steps.standardMavenVersion.outputs.version}}-dev-src.tar.gz"
          fi
          
          if (svn ls "apache-manifoldcf-${{steps.standardMavenVersion.outputs.version}}-dev-src.zip"); then
            echo "Removing existing apache-manifoldcf-${{steps.standardMavenVersion.outputs.version}}-dev-src.zip"
            svn delete "apache-manifoldcf-${{steps.standardMavenVersion.outputs.version}}-dev-src.zip"
          fi
          
          echo "Downloading: https://github.com/apache/manifoldcf/releases/download/${{ steps.branchWithoutPrefixExtracted.outputs.version }}/apache-manifoldcf-${{steps.standardMavenVersion.outputs.version}}-dev-bin.zip"
          wget https://github.com/apache/manifoldcf/releases/download/${{ steps.branchWithoutPrefixExtracted.outputs.version }}/apache-manifoldcf-${{steps.standardMavenVersion.outputs.version}}-dev-bin.zip  -O "apache-manifoldcf-${{steps.standardMavenVersion.outputs.version}}-dev-bin.zip"
          echo "Downloading: https://github.com/apache/manifoldcf/releases/download/${{ steps.branchWithoutPrefixExtracted.outputs.version }}/apache-manifoldcf-${{steps.standardMavenVersion.outputs.version}}-dev-bin.tar.gz"
          wget https://github.com/apache/manifoldcf/releases/download/${{ steps.branchWithoutPrefixExtracted.outputs.version }}/apache-manifoldcf-${{steps.standardMavenVersion.outputs.version}}-dev-bin.tar.gz  -O "apache-manifoldcf-${{steps.standardMavenVersion.outputs.version}}-dev-bin.tar.gz"
          echo "Downloading: https://github.com/apache/manifoldcf/releases/download/${{ steps.branchWithoutPrefixExtracted.outputs.version }}/apache-manifoldcf-${{steps.standardMavenVersion.outputs.version}}-dev-lib.tar.gz"
          wget https://github.com/apache/manifoldcf/releases/download/${{ steps.branchWithoutPrefixExtracted.outputs.version }}/apache-manifoldcf-${{steps.standardMavenVersion.outputs.version}}-dev-lib.tar.gz  -O "apache-manifoldcf-${{steps.standardMavenVersion.outputs.version}}-dev-lib.tar.gz"
          echo "Downloading: https://github.com/apache/manifoldcf/releases/download/${{ steps.branchWithoutPrefixExtracted.outputs.version }}/apache-manifoldcf-${{steps.standardMavenVersion.outputs.version}}-dev-lib.zip"
          wget https://github.com/apache/manifoldcf/releases/download/${{ steps.branchWithoutPrefixExtracted.outputs.version }}/apache-manifoldcf-${{steps.standardMavenVersion.outputs.version}}-dev-lib.zip  -O "apache-manifoldcf-${{steps.standardMavenVersion.outputs.version}}-dev-lib.zip"
          echo "Downloading: https://github.com/apache/manifoldcf/releases/download/${{ steps.branchWithoutPrefixExtracted.outputs.version }}/apache-manifoldcf-${{steps.standardMavenVersion.outputs.version}}-dev-src.tar.gz"
          wget https://github.com/apache/manifoldcf/releases/download/${{ steps.branchWithoutPrefixExtracted.outputs.version }}/apache-manifoldcf-${{steps.standardMavenVersion.outputs.version}}-dev-src.tar.gz -O "apache-manifoldcf-${{steps.standardMavenVersion.outputs.version}}-dev-src.tar.gz"
          echo "Downloading: https://github.com/apache/manifoldcf/releases/download/${{ steps.branchWithoutPrefixExtracted.outputs.version }}/apache-manifoldcf-${{steps.standardMavenVersion.outputs.version}}-dev-src.zip"
          wget https://github.com/apache/manifoldcf/releases/download/${{ steps.branchWithoutPrefixExtracted.outputs.version }}/apache-manifoldcf-${{steps.standardMavenVersion.outputs.version}}-dev-src.zip -O "apache-manifoldcf-${{steps.standardMavenVersion.outputs.version}}-dev-src.zip"
          
      - name: Import GPG key
        id: import_gpg
        uses: crazy-max/ghaction-import-gpg@111c56156bcc6918c056dbef52164cfa583dc549
        with:
          gpg_private_key: ${{ secrets.GPG_PRIVATE_KEY }}
          passphrase: ${{ secrets.GPG_PASSPHRASE }}
          
      - name: Mask Apache Password
        run: |
          # Workaround for Actions bug - https://github.com/actions/runner/issues/643
          APACHE_PASS=$(jq -r '.inputs.APACHE_PASSWORD' $GITHUB_EVENT_PATH)
          echo "::add-mask::$APACHE_PASS"
      
      - name: Generate file hashes
        run: | 
          echo "$GPG_PRIVATE_KEY" | gpg --batch --import --passphrase "$GPG_PASSPHRASE"
          gpg --local-user "${{steps.import_gpg.outputs.name}}" --armor --output apache-manifoldcf-${{steps.standardMavenVersion.outputs.version}}-src.zip.asc --detach-sig apache-manifoldcf-${{steps.standardMavenVersion.outputs.version}}-src.zip
          gpg --local-user "${{steps.import_gpg.outputs.name}}" --armor --output apache-manifoldcf-${{steps.standardMavenVersion.outputs.version}}-src.tar.gz.asc --detach-sig apache-manifoldcf-${{steps.standardMavenVersion.outputs.version}}-src.tar.gz
          gpg --local-user "${{steps.import_gpg.outputs.name}}" --armor --output apache-manifoldcf-${{steps.standardMavenVersion.outputs.version}}-lib.zip.asc --detach-sig apache-manifoldcf-${{steps.standardMavenVersion.outputs.version}}-lib.zip
          gpg --local-user "${{steps.import_gpg.outputs.name}}" --armor --output apache-manifoldcf-${{steps.standardMavenVersion.outputs.version}}-lib.tar.gz.asc --detach-sig apache-manifoldcf-${{steps.standardMavenVersion.outputs.version}}-lib.tar.gz
          gpg --local-user "${{steps.import_gpg.outputs.name}}" --armor --output apache-manifoldcf-${{steps.standardMavenVersion.outputs.version}}-bin.zip.asc --detach-sig apache-manifoldcf-${{steps.standardMavenVersion.outputs.version}}-bin.zip
          gpg --local-user "${{steps.import_gpg.outputs.name}}" --armor --output apache-manifoldcf-${{steps.standardMavenVersion.outputs.version}}-bin.tar.gz.asc --detach-sig apache-manifoldcf-${{steps.standardMavenVersion.outputs.version}}-bin.tar.gz

          gpg --print-md SHA512 apache-manifoldcf-${{steps.standardMavenVersion.outputs.version}}-src.zip > apache-manifoldcf-${{steps.standardMavenVersion.outputs.version}}-src.zip.sha512
          gpg --print-md SHA512 apache-manifoldcf-${{steps.standardMavenVersion.outputs.version}}-src.tar.gz > apache-manifoldcf-${{steps.standardMavenVersion.outputs.version}}-src.tar.gz.sha512
          gpg --print-md SHA512 apache-manifoldcf-${{steps.standardMavenVersion.outputs.version}}-lib.zip > apache-manifoldcf-${{steps.standardMavenVersion.outputs.version}}-lib.zip.sha512
          gpg --print-md SHA512 apache-manifoldcf-${{steps.standardMavenVersion.outputs.version}}-lib.tar.gz > apache-manifoldcf-${{steps.standardMavenVersion.outputs.version}}-lib.tar.gz.sha512
          gpg --print-md SHA512 apache-manifoldcf-${{steps.standardMavenVersion.outputs.version}}-bin.zip > apache-manifoldcf-${{steps.standardMavenVersion.outputs.version}}-bin.zip.sha512
          gpg --print-md SHA512 apache-manifoldcf-${{steps.standardMavenVersion.outputs.version}}-bin.tar.gz > apache-manifoldcf-${{steps.standardMavenVersion.outputs.version}}-bin.tar.gz.sha512
          
          svn add --force .
          svn status
          svn commit -m "Create ${{ steps.rcRevision.outputs.version }} for MCF ${{ steps.artifactVersion.outputs.version }}"
          svn commit -m "Staging Apache ManifoldCF ${{ steps.artifactVersion.outputs.version }}" --non-interactive --username "${{ github.event.inputs.APACHE_ID }}" --password "${{ github.event.inputs.APACHE_PASSWORD }}"
          