name: Release

on:
  push:
    tags:
      - "*"

  workflow_dispatch:

env:
  CARGO_TERM_COLOR: always

jobs:
  release:
    name: Cross build for ${{ matrix.target }}
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        include:
          - os: ubuntu-latest
            target: x86_64-unknown-linux-gnu
            exe: lazydns
          - os: ubuntu-latest
            target: x86_64-unknown-linux-musl
            exe: lazydns
          - os: ubuntu-latest
            target: aarch64-unknown-linux-musl
            exe: lazydns
          - os: ubuntu-latest
            target: i686-unknown-linux-musl
            exe: lazydns
          - os: ubuntu-latest
            target: arm-unknown-linux-musleabihf
            exe: lazydns
          - os: macos-latest
            target: x86_64-apple-darwin
            exe: lazydns
          - os: macos-latest
            target: aarch64-apple-darwin
            exe: lazydns
          - os: ubuntu-latest
            target: x86_64-unknown-freebsd
            exe: lazydns
          - os: windows-latest
            target: x86_64-pc-windows-msvc
            exe: lazydns.exe
          - os: windows-latest
            target: i686-pc-windows-msvc
            exe: lazydns.exe
          - os: windows-latest
            target: aarch64-pc-windows-msvc
            exe: lazydns.exe
    steps:
      - uses: actions/checkout@v6
      - uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          target: ${{ matrix.target }}

      - name: Install cross
        run: cargo install --version 0.2.5 cross

      - name: Build release
        run: |
          cross build --profile minimal --target ${{ matrix.target }}
          cross build --profile full --all-features --target ${{ matrix.target }}

      - name: Run UPX (non-Windows)
        # UPX may not support some platforms; ignore errors
        if: matrix.os != 'windows-latest'
        continue-on-error: true
        uses: crazy-max/ghaction-upx@v1
        with:
          version: v5.0.2
          files: |
            target/${{ matrix.target }}/minimal/lazydns
            target/${{ matrix.target }}/full/lazydns
          args: -q --best --lzma

      - name: Run UPX (Windows)
        # Use .exe paths on Windows runners
        if: matrix.os == 'windows-latest'
        continue-on-error: true
        uses: crazy-max/ghaction-upx@v1
        with:
          version: v5.0.2
          files: |
            target\${{ matrix.target }}\minimal\lazydns.exe
            target\${{ matrix.target }}\full\lazydns.exe
          args: -q --best --lzma
      - name: Package release (non-Windows)
        if: matrix.os != 'windows-latest'
        shell: bash
        run: |
          set -euo pipefail
          TARGET=${{ matrix.target }}
          EXE="${{ matrix.exe }}"
          DIR=target/${{ matrix.target }}/minimal
          # Ensure the executable exists
          ls -lah "$DIR" || true
          # Create an unversioned archive matching installer expectations:
          # lazydns-<target>.tar.gz
          cp config.yaml LICENSE "$DIR"/
          tar czf "$DIR/lazydns-${TARGET}.tar.gz" -C "$DIR" lazydns config.yaml LICENSE

      - name: Package release (Windows)
        if: matrix.os == 'windows-latest'
        shell: pwsh
        run: |
          $TARGET = '${{ matrix.target }}'
          $EXE = 'lazydns.exe'
          $DIR = "target/${{ matrix.target }}/minimal"
          # Use ${} to avoid accidental variable-token parsing when followed by punctuation
          Write-Output "Files in ${DIR}`n"
          Get-ChildItem -Path $DIR | Write-Output
          # Copy config and license files
          Copy-Item -Path "config.yaml" -Destination "$DIR/"
          Copy-Item -Path "LICENSE" -Destination "$DIR/"
          # Create an unversioned zip matching installer expectations: lazydns-<target>.zip
          Compress-Archive -Path "$DIR/$EXE", "$DIR/config.yaml", "$DIR/LICENSE" -DestinationPath "$DIR/lazydns-$TARGET.zip" -Force

      - name: Package release (non-Windows, full)
        if: matrix.os != 'windows-latest'
        shell: bash
        run: |
          set -euo pipefail
          TARGET=${{ matrix.target }}
          EXE="${{ matrix.exe }}"
          DIR=target/${{ matrix.target }}/full
          # Ensure the executable exists
          ls -lah "$DIR" || true
          # Create an unversioned archive matching installer expectations:
          # lazydns-full-<target>.tar.gz
          cp config.yaml LICENSE "$DIR"/
          tar czf "$DIR/lazydns-full-${TARGET}.tar.gz" -C "$DIR" lazydns config.yaml LICENSE

      - name: Package release (Windows, full)
        if: matrix.os == 'windows-latest'
        shell: pwsh
        run: |
          $TARGET = '${{ matrix.target }}'
          $EXE = 'lazydns.exe'
          $DIR = "target/${{ matrix.target }}/full"
          # Use ${} to avoid accidental variable-token parsing when followed by punctuation
          Write-Output "Files in ${DIR}`n"
          Get-ChildItem -Path $DIR | Write-Output
          # Copy config and license files
          Copy-Item -Path "config.yaml" -Destination "$DIR/"
          Copy-Item -Path "LICENSE" -Destination "$DIR/"
          # Create an unversioned zip matching installer expectations: lazydns-full-<target>.zip
          Compress-Archive -Path "$DIR/$EXE", "$DIR/config.yaml", "$DIR/LICENSE" -DestinationPath "$DIR/lazydns-full-$TARGET.zip" -Force

      - name: Upload artifact (non-Windows)
        if: matrix.os != 'windows-latest'
        uses: actions/upload-artifact@v5
        with:
          name: lazydns-${{ matrix.target }}.tar.gz
          path: target/${{ matrix.target }}/minimal/lazydns-${{ matrix.target }}.tar.gz

      - name: Upload artifact (Windows)
        if: matrix.os == 'windows-latest'
        uses: actions/upload-artifact@v5
        with:
          name: lazydns-${{ matrix.target }}.zip
          path: target/${{ matrix.target }}/minimal/lazydns-${{ matrix.target }}.zip

      - name: Upload artifact (non-Windows, full)
        if: matrix.os != 'windows-latest'
        uses: actions/upload-artifact@v5
        with:
          name: lazydns-full-${{ matrix.target }}.tar.gz
          path: target/${{ matrix.target }}/full/lazydns-full-${{ matrix.target }}.tar.gz

      - name: Upload artifact (Windows, full)
        if: matrix.os == 'windows-latest'
        uses: actions/upload-artifact@v5
        with:
          name: lazydns-full-${{ matrix.target }}.zip
          path: target/${{ matrix.target }}/full/lazydns-full-${{ matrix.target }}.zip

  publish:
    name: Publish release
    runs-on: ubuntu-latest
    if: startsWith(github.ref, 'refs/tags/')
    needs: [release]
    steps:
      - name: Download all artifacts
        uses: actions/download-artifact@v6
        with:
          path: artifacts
      - name: List artifacts
        run: |
          # List only release assets we intend to attach to the GitHub release
          find artifacts -type f \( -name "*.zip" -o -name "*.tar.gz" -o -name "*.deb" \) | sort
      - name: Create release
        uses: softprops/action-gh-release@v2
        with:
          # Support .zip, .tar.gz and .deb release artifacts
          files: |
            artifacts/**/*.{zip,tar.gz,deb}
          generate_release_notes: true
          draft: true

  publish-crate:
    name: Publish to crates.io
    if: startsWith(github.ref, 'refs/tags/')
    runs-on: ubuntu-latest
    needs: publish
    steps:
      - uses: actions/checkout@v6
      - uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
      - name: Publish
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CRATES_IO_API_TOKEN }}
        run: cargo publish
