name: Build Check

on:
  push:
    branches: [master]
  pull_request:
    branches: [master]

concurrency:
  group: build-check-${{ github.ref }}
  cancel-in-progress: true

jobs:
  build:
    name: ${{ matrix.name }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - name: Linux x86_64 (tar.gz)
            target: x86_64-unknown-linux-gnu
            format: tar-gz
          - name: Linux x86_64 (deb)
            target: x86_64-unknown-linux-gnu
            format: deb
            deps: rpm
          - name: Linux x86_64 (rpm)
            target: x86_64-unknown-linux-gnu
            format: rpm
            deps: rpm
          - name: Arch Linux x86_64
            target: x86_64-unknown-linux-gnu
            format: arch
          - name: Linux x86_64 (musl)
            target: x86_64-unknown-linux-musl
            format: musl-tar-gz
            deps: musl-tools
          - name: Linux ARM64 (tar.gz)
            target: aarch64-unknown-linux-gnu
            format: tar-gz
            deps: gcc-aarch64-linux-gnu binutils-aarch64-linux-gnu
          - name: Linux ARM64 (deb)
            target: aarch64-unknown-linux-gnu
            format: deb
            deps: gcc-aarch64-linux-gnu binutils-aarch64-linux-gnu rpm
          - name: Linux ARM64 (rpm)
            target: aarch64-unknown-linux-gnu
            format: rpm
            deps: gcc-aarch64-linux-gnu binutils-aarch64-linux-gnu rpm
          - name: Arch Linux ARM64
            target: aarch64-unknown-linux-gnu
            format: arch-arm64
            deps: gcc-aarch64-linux-gnu binutils-aarch64-linux-gnu
          - name: Linux ARM64 (musl)
            target: aarch64-unknown-linux-musl
            format: musl-tar-gz
            deps: musl-tools gcc-aarch64-linux-gnu
          - name: Windows x86_64 (zip)
            target: x86_64-pc-windows-gnu
            format: zip
            deps: gcc-mingw-w64-x86-64

    runs-on: ubuntu-latest
    env:
      CARGO_TERM_COLOR: always
    steps:
      - uses: actions/checkout@v7

      - uses: dtolnay/rust-toolchain@stable

      - name: Install target
        run: rustup target add ${{ matrix.target }}

      - uses: Swatinem/rust-cache@v2
        with:
          key: ${{ matrix.target }}
          cleanup: true

      - name: Install system dependencies
        if: matrix.deps != ''
        run: |
          sudo apt-get update -qq
          sudo apt-get install -y -qq ${{ matrix.deps }}

      - name: Install packaging tools
        if: matrix.format == 'deb' || matrix.format == 'rpm'
        run: |
          cargo install cargo-deb --locked
          cargo install cargo-rpm --locked

      - name: Build and package
        shell: bash
        run: |
          set -euxo pipefail
          mkdir -p dist
          VERSION=$(git describe --tags --always --dirty 2>/dev/null || echo "dev")

          case "${{ matrix.format }}" in
            tar-gz)
              cargo build --release --target ${{ matrix.target }}
              cp target/${{ matrix.target }}/release/leenfetch dist/leenfetch
              tar -czf "dist/leenfetch-${VERSION}-linux-${{ matrix.target == 'x86_64-unknown-linux-gnu' && 'x86_64' || 'aarch64' }}.tar.gz" -C dist leenfetch
              rm dist/leenfetch
              ;;

            deb)
              cargo build --release --target ${{ matrix.target }}
              cargo deb ${{ matrix.target == 'aarch64-unknown-linux-gnu' && '--no-strip ' || '' }}--target ${{ matrix.target }}
              cp target/${{ matrix.target }}/debian/*.deb "dist/leenfetch-${VERSION}-debian-${{ matrix.target == 'x86_64-unknown-linux-gnu' && 'x86_64' || 'aarch64' }}.deb"
              ;;

            rpm)
              cargo build --release --target ${{ matrix.target }}
              case "${{ matrix.target }}" in
                x86_64-unknown-linux-gnu)
                  cargo rpm init 2>/dev/null || true
                  cargo rpm build 2>/dev/null || true
                  find target/release/rpmbuild -name "*.rpm" -exec cp {} "dist/leenfetch-${VERSION}-rhel-x86_64.rpm" \; 2>/dev/null || true
                  ;;
                aarch64-unknown-linux-gnu)
                  {
                    echo "Name: leenfetch"
                    echo "Version: ${VERSION#v}"
                    echo "Release: 1%{?dist}"
                    echo "Summary: Fast, minimal system info tool"
                    echo "License: MIT"
                    echo "BuildArch: aarch64"
                    echo ""
                    echo "%description"
                    echo "Fast and customizable system info tool for the terminal."
                    echo ""
                    echo "%install"
                    echo "install -Dm0755 %{_sourcedir}/leenfetch %{buildroot}%{_bindir}/leenfetch"
                    echo ""
                    echo "%files"
                    echo "%{_bindir}/leenfetch"
                  } > dist/leenfetch.spec
                  cp target/${{ matrix.target }}/release/leenfetch dist/leenfetch
                  rpmbuild -bb dist/leenfetch.spec \
                    --define "_sourcedir $PWD/dist" \
                    --define "_rpmdir $PWD/dist" \
                    --define "_target_cpu aarch64" \
                    --nodeps 2>/dev/null || true
                  for f in $(find dist -name "*.rpm" -type f 2>/dev/null); do
                    mv "$f" "dist/leenfetch-${VERSION}-rhel-aarch64.rpm" 2>/dev/null || true
                  done
                  rm -f dist/leenfetch dist/leenfetch.spec
                  ;;
              esac
              ;;

            arch)
              cargo build --release --target ${{ matrix.target }}
              docker run --rm -v $PWD:/work archlinux:latest bash -c "
                pacman -Sy --noconfirm base-devel
                cp /work/target/${{ matrix.target }}/release/leenfetch /work/
                cd /work
                useradd build
                chown build:build leenfetch PKGBUILD
                su build -c 'makepkg -f' || true
                cp *.pkg.tar.zst /work/dist/ 2>/dev/null || true
              " || true
              if [ -f dist/*.pkg.tar.zst ]; then
                mv dist/*.pkg.tar.zst "dist/leenfetch-${VERSION}-arch-x86_64.pkg.tar.zst"
              else
                BIN=target/${{ matrix.target }}/release/leenfetch
                mkdir -p pkg/usr/bin
                cp "$BIN" pkg/usr/bin/leenfetch
                printf 'pkgname = leenfetch\npkgver = %s\npkgdesc = Fast, minimal system info tool\nurl = https://github.com/drunkleen/leenfetch\nbuilddate = %d\npackager = DrunkLeen\nsize = %d\narch = x86_64\nlicense = MIT\n' \
                  "${VERSION#v}" "$(date +%s)" "$(stat -c%s "$BIN")" > .PKGINFO
                tar -c --zstd -f "dist/leenfetch-${VERSION}-arch-x86_64.pkg.tar.zst" .PKGINFO -C pkg . 2>/dev/null || true
                rm -rf pkg .PKGINFO
              fi
              ;;

            arch-arm64)
              cargo build --release --target ${{ matrix.target }}
              BIN=target/${{ matrix.target }}/release/leenfetch
              mkdir -p pkg/usr/bin
              cp "$BIN" pkg/usr/bin/leenfetch
              printf 'pkgname = leenfetch\npkgver = %s\npkgdesc = Fast, minimal system info tool\nurl = https://github.com/drunkleen/leenfetch\nbuilddate = %d\npackager = DrunkLeen\nsize = %d\narch = aarch64\nlicense = MIT\n' \
                "${VERSION#v}" "$(date +%s)" "$(stat -c%s "$BIN")" > .PKGINFO
              tar -c --zstd -f "dist/leenfetch-${VERSION}-arch-aarch64.pkg.tar.zst" .PKGINFO -C pkg . 2>/dev/null || true
              rm -rf pkg .PKGINFO
              ;;

            musl-tar-gz)
              CARGO_TARGET_AARCH64_UNKNOWN_LINUX_MUSL_LINKER=aarch64-linux-gnu-gcc \
                cargo build --release --target ${{ matrix.target }}
              cp target/${{ matrix.target }}/release/leenfetch dist/leenfetch
              tar -czf "dist/leenfetch-${VERSION}-linux-${{ matrix.target == 'x86_64-unknown-linux-musl' && 'x86_64' || 'aarch64' }}-musl.tar.gz" -C dist leenfetch
              rm dist/leenfetch
              ;;

            zip)
              cargo build --release --target ${{ matrix.target }}
              cp target/${{ matrix.target }}/release/leenfetch.exe dist/leenfetch.exe
              zip -j "dist/leenfetch-${VERSION}-windows-x86_64.zip" dist/leenfetch.exe
              rm dist/leenfetch.exe
              ;;
          esac

      - name: Clean up build artifacts
        run: rm -rf target/ dist/
