name: Create Packages (Termux)

on:
  push:
    branches:
    - master
    - devel
  pull_request:
    types:
      - opened
      - reopened
      - synchronize
  workflow_dispatch:

permissions: {} # none

jobs:
  build:
    if: (github.repository_owner == 'dosemu2' && (github.event_name != 'pull_request' || github.event.pull_request.draft == false))
    permissions:
      contents: read # actions/upload-artifact doesn't need contents: write
    runs-on: ubuntu-24.04
    strategy:
      matrix:
        target_arch: [aarch64, arm, i686, x86_64]
      fail-fast: true
    steps:
    - name: Clone repository
      uses: actions/checkout@v6
      with:
        repository: 'termux-user-repository/tur'
        ref: 'master'
        fetch-depth: 100
    - name: Fetch dosemu2 termux recipes
      run: |
        git remote add stsp https://github.com/stsp/tur.git
        git fetch stsp dosemu2
        git checkout dosemu2
        git config --global user.email "ci@github"
        git config --global user.name "Github Action CI"
        git rebase master
        ./setup-environment.sh

    - name: Set process id limit for 32-bit builds depending on aosp-libs
      run: echo 65535 | sudo tee /proc/sys/kernel/pid_max

    - name: Gather build summary
      id: build-info
      run: |
        BASE_COMMIT=$(git merge-base dosemu2 master)
        HEAD_COMMIT="dosemu2"
        CHANGED_FILES=$(git diff-tree --no-commit-id --name-only -r "${BASE_COMMIT}" "HEAD" | grep "tur/" | cut -d "/" -f 2 | uniq)
        mkdir -p ./artifacts ./debs
        touch ./debs/.placeholder

        declare -a packages
        packages="$(echo $CHANGED_FILES)"
        echo "Building packages $packages"
        echo $packages >./built_dosemu2_packages.txt

    - name: Build packages
      env:
        DOCKER_BUILD: ${{ steps.build-info.outputs.docker-build }}
        DOSEMU2_REF: ${{ github.ref }}
      run: |
        declare -a packages
        if [ -f ./built_dosemu2_packages.txt ]; then
          packages="$packages $(cat ./built_dosemu2_packages.txt | tr '\n' ' ')"
        fi

        if [ -n "$packages" ]; then
          scripts/buildorder.py -d
          ./scripts/run-docker.sh ./build-package.sh -I -a ${{ matrix.target_arch }} $packages
        fi

    - name: Generate build artifacts
      if: always()
      run: |
        test -d termux-packages/output && mv termux-packages/output/* ./output/

        # Put package lists into directory with *.deb files so they will be transferred to
        # upload job.
        test -f ./built_dosemu2_packages.txt && mv ./built_dosemu2_packages.txt ./debs/

        # Move only debs from built_packages into debs/ folder before
        # creating an archive.
        while read -r pkg; do
          # Match both $pkg.deb and $pkg-static.deb.
          find output \( -name "$pkg_*.deb" -o -name "$pkg-static_*.deb" \) -type f -print0 | xargs -0r mv -t debs/
        done < <(cat ./debs/built_dosemu2_packages.txt)

        pip3 install termux-apt-repo
        termux-apt-repo debs repo

        # Files containing certain symbols (e.g. ":") will cause failure in actions/upload-artifact.
        # Archiving *.deb files in a tarball to avoid issues with uploading.
        tar cf artifacts/debs-${{ matrix.target_arch }}-${{ github.sha }}.tar debs
        tar cf artifacts/repo-${{ matrix.target_arch }}-${{ github.sha }}.tar repo

    - name: Checksums for built *.deb files
      if: always()
      run: |
        find debs -type f -name "*.deb" -exec sha256sum "{}" \; | sort -k2 | tee checksum-${{ matrix.target_arch }}-${{ github.sha }}.txt

    - name: Store checksums for built *.deb files
      if: always()
      uses: actions/upload-artifact@v6
      with:
        name: checksum-${{ matrix.target_arch }}-${{ github.sha }}
        path: checksum-${{ matrix.target_arch }}-${{ github.sha }}.txt

    - name: Store *.deb files
      if: always()
      uses: actions/upload-artifact@v6
      with:
        name: debs-${{ matrix.target_arch }}-${{ github.sha }}
        path: ./artifacts
