name: Create Packages
on:
  release:
    types: [published]

jobs:
  CPack-Package-Build:
    strategy:
      fail-fast: false
      matrix:
        include:
          - release: ubuntu-2204
            extension: ".deb"
            OS-name: "ubuntu/jammy"
          - release: ubuntu-2404
            extension: ".deb"
            OS-name: "ubuntu/noble"
          - release: oraclelinux-9
            extension: ".rpm"
            OS-name: "ol/9"
          - release: fedora-39
            extension: ".rpm"
            OS-name: "fedora/39"
          - release: fedora-40
            extension: ".rpm"
            OS-name: "fedora/40"
          - release: fedora-41
            extension: ".rpm"
            OS-name: "fedora/41"

    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Build Container
        run: docker build . -f ./.github/images/${{ matrix.release }}/Dockerfile  -t package_builder

      - name: Packaging
        run: docker run -e DOMAIN_SIZE="64bit" --name container -t package_builder

      - name: Extract
        id: extract_pkg
        run: |-
          docker cp container:/souffle/build/ . &&
          cd build &&
          echo "pkg_name=$(ls *${{ matrix.extension }} | head -n1)" >> $GITHUB_OUTPUT
          echo "artifact_name=x86_64-${{ matrix.release }}-$(ls *${{ matrix.extension }} | head -n1)" >> $GITHUB_OUTPUT

      - name: Naming Artifact
        run: cp build/${{ steps.extract_pkg.outputs.pkg_name }} build/${{ steps.extract_pkg.outputs.artifact_name }}

      - name: Upload Artifact
        uses: actions/upload-artifact@v4
        with:
          name: ${{ steps.extract_pkg.outputs.artifact_name }}
          path: build/${{ steps.extract_pkg.outputs.artifact_name }}

  Arch-Package-Build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Prepare
        id: prepare
        run: |-
          echo "release_tag=$(git describe --tags --always | sed 's/-.*$//')" >> $GITHUB_OUTPUT

      - name: Build Container
        run: docker build ./.github/images/arch-linux/ -t package_builder

      - name: Packaging
        run: >
          docker run
          -e RELEASE_TAG=${{ steps.prepare.outputs.release_tag }}
          -e REPO_OWNER=${{ github.repository_owner }}
          --name container
          -t package_builder

      - name: Setup Credentials
        uses: shimataro/ssh-key-action@v2
        with:
           key: ${{ secrets.AUR_PRIVATE_KEY }}
           name: aur
           known_hosts: ${{ secrets.AUR_KNOWN_HOSTS }}
           config: |
              Host aur.archlinux.org
              IdentityFile ~/.ssh/aur
              User aur


      - name: Upload
        run: |-
          git clone ssh://aur@aur.archlinux.org/souffle.git && 
          cd souffle && 
          docker cp container:/home/builduser/PKGBUILD . && 
          docker cp container:/home/builduser/.SRCINFO . && 
          git config user.email "xihu5895@uni.sydney.edu.au" && 
          git config user.name "Xiaowen Hu" && 
          git add PKGBUILD .SRCINFO &&
          git commit -m "release version ${{ steps.prepare.outputs.release_tag }}" &&
          git push


  Upload-Release-Assets:
    needs: CPack-Package-Build
    if: ${{ always() }}

    runs-on: ubuntu-latest
    steps:
      - name: Download All Artifacts
        uses: actions/download-artifact@v4
        with:
          path: ./downloads

      - name: Create Checksum
        run: |-
          mkdir result && 
          mv downloads/*/* result/ &&
          cd result && 
          sha512sum * > sha512sum.txt

      - name: Upload Release Assets
        uses: softprops/action-gh-release@v2
        with:
          files: |
            result/*

  Upload-Repo-Files:
    needs: CPack-Package-Build
    if: ${{ always() }}

    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
          clean: false

      - name: Download All Artifacts
        uses: actions/download-artifact@v4
        with:
          path: ./downloads

      - name: Configure GPG Key
        run: |-
          .github/scripts/config_gpg.sh
        env:
          GPG_SIGNING_KEY: ${{ secrets.GPG_SIGNING_KEY }}

      - name: Upload Repo Files
        env:
          API_TOKEN_GITHUB: ${{ secrets.API_TOKEN_GITHUB }}
        run : |-
          .github/scripts/updatePPA.sh
