name: Release

on:
  push:
    tags:
      - "v*"
  workflow_dispatch:
    inputs:
      tag:
        description: "Release tag (e.g., v0.1.0)"
        required: true
        type: string

permissions:
  contents: write

env:
  CARGO_TERM_COLOR: always

jobs:
  build:
    name: Build ${{ matrix.target }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          # Linux x86_64
          - target: x86_64-unknown-linux-gnu
            os: ubuntu-22.04
          - target: x86_64-unknown-linux-musl
            os: ubuntu-22.04
          # Linux aarch64
          - target: aarch64-unknown-linux-gnu
            os: ubuntu-22.04
          - target: aarch64-unknown-linux-musl
            os: ubuntu-22.04
            use_cross: true
          # Linux armv7
          - target: armv7-unknown-linux-gnueabihf
            os: ubuntu-22.04
            use_cross: true
          - target: armv7-unknown-linux-musleabihf
            os: ubuntu-22.04
            use_cross: true
          # Linux i686
          - target: i686-unknown-linux-gnu
            os: ubuntu-22.04
          - target: i686-unknown-linux-musl
            os: ubuntu-22.04
            use_cross: true
          # macOS
          - target: x86_64-apple-darwin
            os: macos-latest
          - target: aarch64-apple-darwin
            os: macos-latest
          # Windows
          - target: x86_64-pc-windows-msvc
            os: windows-latest

    steps:
      - uses: actions/checkout@v6
        with:
          ref: ${{ inputs.tag || github.ref }}

      - uses: dtolnay/rust-toolchain@stable
        with:
          targets: ${{ matrix.target }}

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

      # Install cross-compilation tools for Linux targets
      - name: Install cross-compilation tools
        if: runner.os == 'Linux'
        run: |
          case "${{ matrix.target }}" in
            aarch64-unknown-linux-gnu)
              sudo apt-get update
              sudo apt-get install -y gcc-aarch64-linux-gnu
              echo "CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER=aarch64-linux-gnu-gcc" >> $GITHUB_ENV
              ;;
            x86_64-unknown-linux-musl)
              sudo apt-get update
              sudo apt-get install -y musl-tools
              ;;
            i686-unknown-linux-gnu)
              sudo apt-get update
              sudo apt-get install -y gcc-multilib
              ;;
          esac

      # Install cross for targets that need it
      - name: Install cross
        if: matrix.use_cross
        run: cargo install cross --git https://github.com/cross-rs/cross

      - name: Build trojan
        run: |
          if [ "${{ matrix.use_cross }}" = "true" ]; then
            cross build --release --target ${{ matrix.target }} -p trojan --features upgrade
          else
            cargo build --release --target ${{ matrix.target }} -p trojan --features upgrade
          fi
        shell: bash

      - name: Package (Unix)
        if: matrix.os != 'windows-latest'
        run: |
          cd target/${{ matrix.target }}/release
          tar czvf ../../../trojan-${{ matrix.target }}.tar.gz trojan
          cd ../../..

      - name: Package (Windows)
        if: matrix.os == 'windows-latest'
        run: |
          cd target/${{ matrix.target }}/release
          7z a ../../../trojan-${{ matrix.target }}.zip trojan.exe
          cd ../../..
        shell: bash

      - name: Upload artifact
        uses: actions/upload-artifact@v6
        with:
          name: trojan-${{ matrix.target }}
          path: trojan-${{ matrix.target }}.*

  release:
    name: Create Release
    needs: build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
        with:
          ref: ${{ inputs.tag || github.ref }}

      - name: Download artifacts
        uses: actions/download-artifact@v7
        with:
          path: artifacts

      - name: Collect release files
        run: |
          mkdir -p release
          find artifacts -type f \( -name "*.tar.gz" -o -name "*.zip" \) -exec mv {} release/ \;
          ls -la release/

      - name: Generate checksums
        run: |
          cd release
          sha256sum * > SHA256SUMS
          cat SHA256SUMS

      - name: Create Release
        uses: softprops/action-gh-release@v2
        with:
          tag_name: ${{ inputs.tag || github.ref_name }}
          files: |
            release/*
          draft: false
          prerelease: ${{ contains(inputs.tag || github.ref, 'alpha') || contains(inputs.tag || github.ref, 'beta') || contains(inputs.tag || github.ref, 'rc') }}
