name: build

on:
  # TODO: Enable once we have conditional build logic built
  # pull_request:
  #   paths:
  #     - 'srcpkgs/**'
  push:
    paths:
      - 'srcpkgs/**'
      - '.github/**' # for easier debugging

jobs:
  changedpkgs:
    name: Check package changes
    runs-on: ubuntu-latest
    if: "!contains(github.event.pull_request.title, '[ci skip]') && !contains(github.event.pull_request.body, '[ci skip]')"

    outputs:
      changed: ${{ steps.grabchanged.outputs.changed }}

    steps:
      - name: checkout local templates
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - id: grabchanged
        run: |
          # write changes to detected_templates
          .github/changed_templates.sh
          # move them to step output
          echo changed="$(cat detected_templates)" >> $GITHUB_OUTPUT
      - run: |
          echo "${{ steps.grabchanged.outputs.changed }}"

  build:
    name: Build packages
    runs-on: ubuntu-latest
    needs: [ changedpkgs ]
    if: "needs.changedpkgs.outputs.changed != '' && !contains(github.event.pull_request.title, '[ci skip]') && !contains(github.event.pull_request.body, '[ci skip]')"

    strategy:
      fail-fast: false
      matrix:
        config:
          - { arch: x86_64,       host: x86_64,      libc: glibc, platform: linux/amd64, test: 1 }
          # - { arch: i686,         host: i686,        libc: glibc, platform: linux/386,   test: 1 }
          # - { arch: aarch64,      host: x86_64,      libc: glibc, platform: linux/amd64, test: 0 }
          # - { arch: armv7l,       host: x86_64,      libc: glibc, platform: linux/amd64, test: 0 }
          # - { arch: x86_64-musl,  host: x86_64-musl, libc: musl,  platform: linux/amd64, test: 1 }
          # - { arch: armv6l-musl,  host: x86_64-musl, libc: musl,  platform: linux/amd64, test: 0 }
          # - { arch: aarch64-musl, host: x86_64-musl, libc: musl,  platform: linux/amd64, test: 0 }

    container:
      image: ghcr.io/void-linux/void-${{ matrix.config.libc }}-full:20250227R1
      options: --platform ${{ matrix.config.platform }} --privileged
      volumes:
        - /dev:/dev
      env:
        ARCH: '${{ matrix.config.arch }}'
        BOOTSTRAP: '${{ matrix.config.host }}'
        TEST: '${{ matrix.config.test }}'

    steps:
      - name: Prepare container
        run: |
          # switch to repo-ci mirror
          mkdir -p /etc/xbps.d && cp /usr/share/xbps.d/*-repository-*.conf /etc/xbps.d/
          sed -i 's|repo-default|repo-ci|g' /etc/xbps.d/*-repository-*.conf
          # install dependencies
          xbps-install -Syu xbps && xbps-install -yu && xbps-install -y sudo bash curl git
          # create non-root user
          useradd -G xbuilder -M builder

      - name: checkout upstream templates
        uses: actions/checkout@v4
        with:
          repository: void-linux/void-packages
          ref: master
          fetch-depth: 1

      - name: checkout local templates
        uses: actions/checkout@v4
        with:
          ref: ${{ github.head_ref }}
          path: added-packages

      - name: copy
        run: cp -rv added-packages/srcpkgs/* srcpkgs

        # TODO: Implement cache?
      - name: Prepare masterdir
        run: |
          chown -R builder:builder . &&
          sudo -Eu builder common/travis/set_mirror.sh &&
          sudo -Eu builder common/travis/prepare.sh &&
          common/travis/fetch-xtools.sh

      - name: Find changed templates
        env:
          CHANGED: ${{ needs.changedpkgs.outputs.changed }}
        run: sudo -Eu builder echo "$CHANGED" | sed 's/ /\n/g' > /tmp/templates

      - name: Build and check packages
        run: sudo -Eu builder common/travis/build.sh "$BOOTSTRAP" "$ARCH" "$TEST"

      - name: Show files
        run: sudo -Eu builder common/travis/show_files.sh "$BOOTSTRAP" "$ARCH"

      - name: Archive xbps artifacts
        uses: actions/upload-artifact@v4
        with:
          name: binaries-${{ matrix.config.arch }}-${{ matrix.config.libc }}
          path: |
            hostdir/binpkgs/*.xbps

      # - name: Compare to previous
      #   run: sudo -Eu builder common/travis/xpkgdiff.sh "$BOOTSTRAP" "$ARCH"

      # - name: Check file conflicts
      #   if: matrix.config.arch == 'x86_64' # the arch indexed in xlocate
      #   run: |
      #     if [ -s /tmp/templates ]; then
      #         export XDG_CACHE_HOME="$PWD/.cache"
      #         sudo -Eu builder xlocate -S &&
      #         sudo -Eu builder common/scripts/lint-conflicts
      #     fi

      # - name: Verify repository state
      #   run: |
      #     mkdir -p /check-install &&
      #     chown builder:builder /check-install &&
      #     sudo -Eu builder common/travis/check-install.sh "$BOOTSTRAP" "$ARCH"

  deploy:
    name: Deploy repository
    runs-on: ubuntu-latest
    needs: [ build ]
    concurrency:
      group: ${{ github.workflow }}-${{ github.ref }}-deployment
      cancel-in-progress: false
    if: "github.ref == 'refs/heads/main' && !contains(github.event.pull_request.title, '[ci skip]') && !contains(github.event.pull_request.body, '[ci skip]')"

    container:
      image: ghcr.io/void-linux/void-glibc-full:20250227R1
      options: --privileged

    steps:
      - name: Prepare container
        run: |
          # switch to repo-ci mirror
          mkdir -p /etc/xbps.d && cp /usr/share/xbps.d/*-repository-*.conf /etc/xbps.d/
          sed -i 's|repo-default|repo-ci|g' /etc/xbps.d/*-repository-*.conf
          # install dependencies
          xbps-install -Syu xbps && xbps-install -yu && xbps-install -y sudo bash curl git git-lfs

      - name: Checkout existing repo
        uses: actions/checkout@v4
        with:
          ref: gh-pages
          lfs: true

      - name: Remove any non-repo files
        run: |
          xbps-rindex --remove $PWD

      - uses: actions/download-artifact@v4
        with:
          pattern: binaries-*
          merge-multiple: true
      - name: Show downloaded files
        run: ls -R .

      - name: Sign package and repo
        run: |
          echo "${{ secrets.PRIV_KEY }}" > $HOME/privkey
          xbps-rindex -s --signedby "${{ secrets.SIGNER }}" --privkey $HOME/privkey $PWD
          xbps-rindex -S --signedby "${{ secrets.SIGNER }}" --privkey $HOME/privkey $PWD/*.xbps
          rm $HOME/privkey

      - name: Add package to repo
        run: |
          xbps-rindex --add $PWD/*.xbps
          xbps-rindex --clean $PWD

      # - name: checksum
      #   run: |
      #     for pkg in *.xbps; do sha256sum "$pkg" | tee "$pkg".sha256sum; done
      #     for pkg in *.xbps; do sha512sum "$pkg" | tee "$pkg".sha512sum; done

      - name: Create simple index page for repo
        run: |
          echo '<html><head><title>Custom void repository</title></head> <body><h1>Custom void repository. Index of /</h1><hr><pre>' > index.html
          for pkg in *.xbps *.xbps.sig2; do echo "<a href=\"${pkg}}\">${pkg}</a>" >> index.html; done
          echo '</body></html>' >> index.html

      - name: publish to pages
        uses: peaceiris/actions-gh-pages@v4
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          publish_dir: .