name: Build and release

on: [workflow_dispatch]

jobs:
  build-and-rel-pmt:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        id: checkout
        uses: actions/checkout@v4

      - name: Cleanup
        id: cleanup
        uses: rokibhasansagar/slimhub_actions@main

      - name: Update and setup packages
        id: general-packages-progress
        run: |
          type -p curl >/dev/null || (sudo apt update && sudo apt install curl -y)
          curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg \
          && sudo chmod go+r /usr/share/keyrings/githubcli-archive-keyring.gpg \
          && echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null \
          && sudo apt update \
          && sudo apt upgrade -y \
          && sudo apt install make xz-utils aria2 gawk gh -y

      - name: Save spefic version variables
        id: save-vars
        run: |
          . build/workflow/build.config
          echo "PMT_VERSION=${PMT_VERSION}" >> $GITHUB_ENV
          echo "PMT_VERSION_CODE=${PMT_VERSION_CODE}" >> $GITHUB_ENV
          echo "NDK_VERSION=${NDK_VERSION}" >> $GITHUB_ENV
          echo "NDK_LINK=${NDK_LINK}" >> $GITHUB_ENV
          echo "NDK_IS=${NDK_IS}" >> $GITHUB_ENV
          cd /home
          sudo git clone https://github.com/ShawkTeam/pmt -b ${PMT_VERSION}
          sudo chmod -R 777 pmt
          echo "PMT_SRCDIR=/home/pmt" >> $GITHUB_ENV
          echo "NDK_DIR=/home/ndk/android-ndk" >> $GITHUB_ENV
          echo "NDK_IN_DIR=/home/ndk" >> $GITHUB_ENV
          sudo mkdir -p "/home/ndk" && cd "/home/ndk" && sudo chmod 777 "/home/ndk"

      - name: Setup Android NDK
        id: setup-ndk
        run: |
          echo "Downloading NDK ${{ env.NDK_VERSION }}..."
          aria2c "${{ env.NDK_LINK }}"
          echo "Extracting..."
          unzip -q *.zip && rm *.zip
          mv * "${{ env.NDK_IS }}"
          sudo chmod -R 777 *
        working-directory: ${{ env.NDK_IN_DIR }}

      - name: Build pmt
        id: build-pmt
        run: |
          export NDK_PROJECT_PATH=${PWD}
          export NDK_ROOT_DIR=${{ env.NDK_DIR }}
          . build/bash/gen-header
          cd ${{ env.NDK_DIR }}
          ./ndk-build
        working-directory: ${{ env.PMT_SRCDIR }}

      - name: Last small transactions
        id: small-transactions
        run: |
          export CXX_64="${PWD}/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android21-clang++"
          export CXX_VERSION=$(${CXX_64} --version | head -n 1)
          echo "CXX_VERSION=${CXX_VERSION}" >> $GITHUB_ENV
          . ${{ env.PMT_SRCDIR }}/build/workflow/build.config
          . ${{ env.PMT_SRCDIR }}/build/workflow/relnotes > /home/pmt/release_body.txt
          cd ${{ env.PMT_SRCDIR }}
          zip -rq pmt-objs-local.zip obj/local/arm64-v8a obj/local/armeabi-v7a
          xz libs/arm64-v8a/pmt
          xz libs/armeabi-v7a/pmt
          mv libs/arm64-v8a/pmt.xz ${PWD}/pmt-arm64-v8a-$(date +%Y%m%d).xz
          mv libs/armeabi-v7a/pmt.xz ${PWD}/pmt-armeabi-v7a-$(date +%Y%m%d).xz
          cp build/mandoc/mandoc pmt.8
          gzip -c pmt.8 > mandoc.gz
        working-directory: ${{ env.NDK_DIR }}

      - name: Upload to release
        id: upload-to-rels
        uses: softprops/action-gh-release@v2
        with:
          files: |
            /home/pmt/pmt*.xz
            /home/pmt/pmt-objs-local.zip
            /home/pmt/mandoc.gz
          name: Partition Manager ${{ env.PMT_VERSION }} Release
          tag_name: ${{ env.PMT_VERSION }}
          body_path: /home/pmt/release_body.txt
        env:
          GITHUB_TOKEN: ${{ secrets.PMT_WORKFLOW_GTOKEN }}
