name: Create GitHub Release after GitLab tag mirror

on:
  push:
    tags:
      - '[0-9]+.[0-9]+.[0-9]+'

jobs:
  preparations:
    name: "Preparations"
    runs-on: ubuntu-latest
    # OUTPUTS
    outputs:
      version: ${{ steps.tag.outputs.tag }}

    steps:
      # GET THE LATEST CODE
      - name: Checkout latest code
        uses: actions/checkout@v4.1.1
        with:
          fetch-depth: 0

      # EXTRACT TAG FROM PUSH
      - name: Get tag
        id: tag
        uses: tj-actions/branch-names@v8.0.1
      - name: Use tag
        run: echo ${{steps.tag.outputs.tag}}

  jar-and-media:
    name: "JAR and media"
    needs: preparations
    runs-on: ubuntu-latest

    steps:
      # GET THE LATEST CODE
      - name: Checkout latest code
        uses: actions/checkout@v4.1.1
        with:
          fetch-depth: 0

      # BUILD PROJECT AND PUBLISH
      - name: Set up JDK 21
        uses: actions/setup-java@v4
        with:
          distribution: 'zulu'
          java-version: '21'
          check-latest: true

      - name: Set Permissions
        run: |
          chmod +x gradlew

      - uses: gradle/actions/setup-gradle@v4
        with:
          gradle-version: wrapper

      - uses: luangong/setup-install4j@v1
        with:
          version: '11.0'
          license: ${{ secrets.INSTALL4J_LICENSE }}

      - name: Remove install4j script
        run: |
          rm install4j_linux-x64_*.sh

      - name: Build release
        env:
          GITHUB_ACTOR: ${{ secrets.GITHUB_ACTOR }}
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          ./gradlew -Pversion="${{ needs.preparations.outputs.version }}" build --info --full-stacktrace -x :serverpackcreator-api:test -x :serverpackcreator-app:test
          ./gradlew -Pversion="${{ needs.preparations.outputs.version }}" media --info --full-stacktrace -x :serverpackcreator-api:test -x :serverpackcreator-app:test

      - name: Rename JAR and Plugin
        run: |
          mv serverpackcreator-app/build/libs/serverpackcreator-app-${{ needs.preparations.outputs.version }}.jar serverpackcreator-app/build/libs/ServerPackCreator-${{ needs.preparations.outputs.version }}.jar

      - name: Collect files
        run: |
          mkdir ${{ needs.preparations.outputs.version }}
          cp serverpackcreator-api/build/libs/*.jar ${{ needs.preparations.outputs.version }}/
          cp serverpackcreator-app/build/libs/*.jar ${{ needs.preparations.outputs.version }}/
          cp serverpackcreator-plugin-example/build/libs/*.jar ${{ needs.preparations.outputs.version }}/
          cp media/* ${{ needs.preparations.outputs.version }}/
          rm -f ${{ needs.preparations.outputs.version }}/output.txt ${{ needs.preparations.outputs.version }}/*plain.jar 

      - name: Generate checksum
        uses: jmgilman/actions-generate-checksum@v1
        with:
          patterns: |
            ${{ needs.preparations.outputs.version }}/*

      - name: Collect checksum
        run: |
          cp checksum.txt ${{ needs.preparations.outputs.version }}/

      # UPLOAD ARTIFACTS
      - uses: actions/upload-artifact@v4
        if: ${{ always() }}
        with:
          name: jar-media-build-artifacts
          path: |
            media
            serverpackcreator-api/build
            serverpackcreator-app/build
            serverpackcreator-web-frontend/dist
            serverpackcreator-plugin-example/build
          if-no-files-found: warn # 'warn' or 'ignore' are also available, defaults to `warn`

      # UPLOAD ARTIFACTS
      - uses: actions/upload-artifact@v4
        if: ${{ always() }}
        with:
          name: jar-media-release
          path: |
            ${{ needs.preparations.outputs.version }}
          if-no-files-found: warn # 'warn' or 'ignore' are also available, defaults to `warn`

  release:
    name: "Release"
    if: ${{ always() }}
    runs-on: ubuntu-latest
    needs: [preparations, jar-and-media]

    steps:
      # GET THE LATEST CODE
      - name: Checkout latest code
        uses: actions/checkout@v4.1.1
        with:
          fetch-depth: 0

      # GET RELEASE INFO
      - name: Get Release Info
        run: |
          curl --header \
          'PRIVATE-TOKEN: ${{ secrets.GITLAB_TOKEN }}' \
          'https://git.griefed.de/api/v4/projects/63/releases/${{ needs.preparations.outputs.version }}' >> version.json

      # GET DESCRIPTION
      - name: Extract version changelog
        uses: sergeysova/jq-action@v2.3.0
        id: description
        with:
          cmd: "jq .description version.json -r | sed -e 's/\\n/\\n/g' > description.txt"
          multiline: true

      - name: General Info
        id: info
        uses: jaywcjlove/github-action-read-file@v2.0.0
        with:
          path: misc/INFO.md

      - name: Dafuq # Otherwise we wouldn't be able to work with the file. Because reasons...
        run: sudo chown $USER description.txt

      - name: Check length and truncate if needed
        run: |
          if [[ $(wc -c description.txt | awk '{print $1}') -ge 9500 ]]; then
            truncate -s 9500 description.txt
            printf "\n\n............\n\nRelease notes truncated. For more details, see the [CHANGELOG](https://github.com/Griefed/ServerPackCreator/blob/${{ needs.preparations.outputs.version }}/CHANGELOG.md).\n\n" >> description.txt
          fi

      - uses: actions/download-artifact@v4
        with:
          name: jar-media-release
          path: ${{ needs.preparations.outputs.version }}

      - name: Append Info
        uses: DamianReeves/write-file-action@v1.3
        with:
          path: description.txt
          write-mode: append
          contents: |
            ${{ steps.preinfo.outputs.content }}
            ${{ steps.info.outputs.content }}

      # GENERATE RELEASE
      - name: Create Release
        id: create_release
        uses: softprops/action-gh-release@v2.1.0
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          tag_name: ${{ needs.preparations.outputs.version }}
          name: Release ${{ needs.preparations.outputs.version }}
          body_path: description.txt
          draft: false
          prerelease: false
          fail_on_unmatched_files: false
          files: |
            ${{ needs.preparations.outputs.version }}/*

      - name: Get source archives
        run: |
          wget -O ${{ needs.preparations.outputs.version }}/source.zip https://github.com/Griefed/ServerPackCreator/archive/refs/tags/${{ needs.preparations.outputs.version }}.zip
          wget -O ${{ needs.preparations.outputs.version }}/source.tar.gz https://github.com/Griefed/ServerPackCreator/archive/refs/tags/${{ needs.preparations.outputs.version }}.tar.gz

      - name: Upload release to version-dir
        uses: nogsantos/scp-deploy@master
        with:
          src: ./${{ needs.preparations.outputs.version }}
          host: ${{ secrets.SPCUPLOAD_HOST }}
          remote: "${{ secrets.SPCUPLOAD_TARGET }}"
          user: ${{ secrets.SPCUPLOAD_USERNAME }}
          key: ${{ secrets.SPCUPLOAD_KEY }}

      - name: Cleanup latest
        id: action-ssh
        uses: tiyee/action-ssh@v1.0.1
        with:
          host: ${{ secrets.SPCUPLOAD_HOST }}
          username: ${{ secrets.SPCUPLOAD_USERNAME }}
          privateKey: ${{ secrets.SPCUPLOAD_KEY }}
          command: 'rm -f ${{ secrets.SPCUPLOAD_TARGET }}/latest/*'

      - name: Upload release to latest-dir
        uses: nogsantos/scp-deploy@master
        with:
          src: ./${{ needs.preparations.outputs.version }}/*
          host: ${{ secrets.SPCUPLOAD_HOST }}
          remote: "${{ secrets.SPCUPLOAD_TARGET }}/latest"
          user: ${{ secrets.SPCUPLOAD_USERNAME }}
          key: ${{ secrets.SPCUPLOAD_KEY }}

  news:
    name: "News on Discord"
    runs-on: ubuntu-latest
    needs: [preparations, release]

    steps:
      - name: Get current date
        id: date
        run: echo "::set-output name=today::$(date +'%Y-%m-%dT%H:%M:%S')"

      - name: Post WebHook Message
        run: |
          curl -L --output ./discord.sh https://raw.githubusercontent.com/ChaoticWeg/discord.sh/master/discord.sh
          chmod a+x ./discord.sh
          ./discord.sh \
            --webhook-url="${{ secrets.WEBHOOK_URL }}" \
            --username "$GITHUB_REPOSITORY" \
            --avatar "https://i.griefed.de/images/2020/11/18/Prosper_Docker_300x300.png" \
            --text "There's been a new release for $GITHUB_REPOSITORY. The new version is ${{ needs.preparations.outputs.version }} and is available at <$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/releases/tag/${{ needs.preparations.outputs.version }}>" \
            --title "New $GITHUB_REPOSITORY Release" \
            --description "There's been a new release for $GITHUB_REPOSITORY. The new version is ${{ needs.preparations.outputs.version }} and is available at $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/releases/tag/${{ needs.preparations.outputs.version }}" \
            --color "0xC0FFEE" \
            --url "$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/releases/tag/${{ needs.preparations.outputs.version }}" \
            --author "Griefed" \
            --author-url "$GITHUB_SERVER_URL/Griefed" \
            --author-icon "https://i.griefed.de/images/2022/01/21/sam_1500x1500.th.jpg" \
            --thumbnail "https://i.griefed.de/images/2021/05/08/app.png" \
            --field "Author;[Griefed]($GITHUB_SERVER_URL/Griefed)" \
            --field "Platform;[GitHub]($GITHUB_SERVER_URL)" \
            --footer "Released at ${{ steps.date.outputs.today }}" \
            --footer-icon "https://i.griefed.de/images/2022/01/21/start_generation.png"

  pages:
    name: "GitHub Pages"
    runs-on: ubuntu-latest
    steps:
      - name: Checkout latest code
        uses: actions/checkout@master
        with:
          fetch-depth: 0

      - name: Set up JDK 21
        uses: actions/setup-java@v4
        with:
          distribution: 'zulu'
          java-version: '21'

      - name: Grant execute permission for gradlew
        run: chmod +x gradlew

      - name: Build documentation
        run: ./gradlew :serverpackcreator-api:dokkaHtml -Pversion="${{ needs.preparations.outputs.version }}" --full-stacktrace --info -x test

      - uses: actions/upload-artifact@v4
        with:
          name: build-artifacts
          path: serverpackcreator-api/build/dokka/
          if-no-files-found: warn # 'warn' or 'ignore' are also available, defaults to `warn`

      # DEPLOY TO GH-PAGES
      - name: Deploy to GitHub-Pages
        uses: JamesIves/github-pages-deploy-action@v4.7.3
        with:
          branch: gh-pages
          folder: serverpackcreator-api/build/dokka