name: Build Releases
on:
  push:
    tags:
      - "v*.*.*"
env:
  CARGO_TERM_COLOR: always

jobs:
  build:
    strategy:
      matrix:
        target:
          - x86_64-unknown-linux-gnu
          - x86_64-unknown-linux-musl
          - aarch64-unknown-linux-gnu
          - aarch64-unknown-linux-musl
          - armv7-unknown-linux-gnueabihf
          - armv7-unknown-linux-musleabihf
          - x86_64-apple-darwin
          - aarch64-apple-darwin
          - x86_64-pc-windows-msvc
          - i686-pc-windows-msvc
          - aarch64-pc-windows-msvc
          - x86_64-win7-windows-msvc
          - i686-win7-windows-msvc

        include:
          - target: x86_64-unknown-linux-gnu
            host_os: ubuntu-22.04
          - target: x86_64-unknown-linux-musl
            host_os: ubuntu-latest
          - target: aarch64-unknown-linux-gnu
            host_os: ubuntu-latest
          - target: aarch64-unknown-linux-musl
            host_os: ubuntu-latest
          - target: armv7-unknown-linux-gnueabihf
            host_os: ubuntu-latest
          - target: armv7-unknown-linux-musleabihf
            host_os: ubuntu-latest
          - target: x86_64-apple-darwin
            host_os: macos-latest
          - target: aarch64-apple-darwin
            host_os: macos-latest
          - target: x86_64-pc-windows-msvc
            host_os: windows-latest
          - target: i686-pc-windows-msvc
            host_os: windows-latest
          - target: aarch64-pc-windows-msvc
            host_os: windows-latest
          - target: x86_64-win7-windows-msvc
            host_os: windows-latest
          - target: i686-win7-windows-msvc
            host_os: windows-latest

    runs-on: ${{ matrix.host_os }}
    steps:
      - uses: actions/checkout@v6
      - uses: dtolnay/rust-toolchain@stable

      - name: Prepare
        shell: bash
        run: |
          cargo install cbindgen
          mkdir release

      - name: Install cross
        if: ${{ contains(matrix.host_os, 'ubuntu') && matrix.host_os != 'ubuntu-22.04' }}
        run: |
          cargo install cross --git https://github.com/cross-rs/cross --rev 36c0d78
          cross --version

      - name: Build
        shell: bash
        run: |
          if [[ "${{ contains(matrix.host_os, 'ubuntu') }}" == "true" && "${{ matrix.host_os }}" != "ubuntu-22.04" ]]; then
            rustup target add ${{ matrix.target }}
            cross build --all-features --release --target ${{ matrix.target }}
          else
            if [[ "${{ matrix.target }}" == "x86_64-win7-windows-msvc" || "${{ matrix.target }}" == "i686-win7-windows-msvc" ]]; then
              rustup toolchain install nightly
              rustup component add rust-src --toolchain nightly
              cargo +nightly build --release -Z build-std --target ${{ matrix.target }}
            else
              rustup target add ${{ matrix.target }}
              cargo build --all-features --release --target ${{ matrix.target }}
            fi
          fi

          cbindgen -c cbindgen.toml -o ./overtls.h
          if [[ "${{ matrix.host_os }}" == "windows-latest" ]]; then
            powershell Compress-Archive -Path target/${{ matrix.target }}/release/overtls-bin.exe, ./config.json, ./overtls.h, target/${{ matrix.target }}/release/overtls.dll -DestinationPath release/overtls-${{ matrix.target }}.zip
          elif [[ "${{ matrix.host_os }}" == "macos-latest" ]]; then
            zip -j release/overtls-${{ matrix.target }}.zip target/${{ matrix.target }}/release/overtls-bin ./config.json ./overtls.h target/${{ matrix.target }}/release/libovertls.dylib
            if [[ "${{ matrix.target }}" == "x86_64-apple-darwin" ]]; then
              ./build-aarch64-apple-ios.sh
              zip -r release/overtls-aarch64-apple-ios-xcframework.zip ./overtls.xcframework/
              ./build-apple.sh
              zip -r release/overtls-apple-xcframework.zip ./overtls.xcframework/
            fi
          elif [[ "${{ contains(matrix.host_os, 'ubuntu') }}" == "true" ]]; then
            zip -j release/overtls-${{ matrix.target }}.zip target/${{ matrix.target }}/release/overtls-bin ./config.json ./overtls.h target/${{ matrix.target }}/release/libovertls.so
          fi

          if [[ "${{ matrix.target }}" == "x86_64-unknown-linux-gnu" ]]; then
            ./build-android.sh
            cp ./overtls-android-libs.zip ./release/
          fi

      - name: Upload
        uses: softprops/action-gh-release@v3
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          files: release/*
