name: Rust

on:
  push:
    branches: [ master ]

env:
  CARGO_TERM_COLOR: always

jobs: 
  quickcheck:
    runs-on: ubuntu-latest
    outputs:
      version: ${{ steps.rustversion.outputs.version }}
    steps:
    - uses: actions/checkout@v4
    - run: cargo check
    - run: cargo pkgid
    - run: 'echo "$(cargo pkgid | cut -d# -f2)"'
    - id: rustversion
      run: 'echo "version=$(cargo pkgid | cut -d# -f2)" >> $GITHUB_OUTPUT'
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
    - name: Install Clippy
      run: rustup component add clippy rustfmt
    - id: cargoversion
      run: cargo --version
    - id: rustcversion
      run: rustc --version
    - name: Clippy
      run: cargo clippy -- -D warnings
    - name: Rustfmt
      run: cargo fmt --all -- --check
    - name: Run tests
      run: cargo test --workspace
    - name: Build
      run: cargo build --verbose --release
    - name: Upload Vopono Artifact
      uses: actions/upload-artifact@v4
      with:
        name: x86_64-gcc
        path: ./target/release/vopono
  fedorarpmbuild:
    needs: [build]
    runs-on: ubuntu-latest
    name: FedoraRpm
    steps:
    - uses: actions/checkout@v4
    - name: BuildRpm
      id: buildrpm
      uses: jamesmcm/cargo-rpm-amd64-fedora@master
    - name: Upload RPM Artifact
      uses: actions/upload-artifact@v4
      with:
        name: fedorarpm
        path: ./target/release/rpmbuild/RPMS/x86_64/*
  opensuseleaprpmbuild:
    needs: [build]
    runs-on: ubuntu-latest
    name: OpensuseLeapRpm
    steps:
    - uses: actions/checkout@v4
    - name: BuildRpm
      id: buildrpm
      uses: jamesmcm/cargo-rpm-amd64-opensuseleap@master
    - name: Upload RPM Artifact
      uses: actions/upload-artifact@v4
      with:
        name: opensuserpm
        path: ./target/release/rpmbuild/RPMS/x86_64/*
  debbuild:
    needs: [build]
    runs-on: ubuntu-latest
    name: Amd64Deb
    steps:
    - uses: actions/checkout@v4
    - name: BuildDeb
      id: debbuild
      uses: jamesmcm/cargo-deb-amd64-ubuntu@master
    - name: Upload Deb Artifact
      uses: actions/upload-artifact@v4
      with:
        name: x86_64-musl-deb
        path: ./target/x86_64-unknown-linux-musl/debian/*
  raspbianbuild:
    needs: [build]
    runs-on: ubuntu-latest
    name: Armv7Deb
    steps:
    - uses: actions/checkout@v4
    - name: BuildDeb
      id: debbuild
      uses: jamesmcm/cargo-deb-armv7-debian@master
    - name: Upload Deb Artifact
      uses: actions/upload-artifact@v4
      with:
        name: armv7-deb
        path: ./target/armv7-unknown-linux-musleabihf/debian/*
  armv5deb:
    needs: [build]
    runs-on: ubuntu-latest
    name: Armv5Deb
    steps:
    - uses: actions/checkout@v4
    - name: BuildDeb
      id: debbuild
      uses: jamesmcm/cargo-deb-armv5-debian@master
    - name: Upload Deb Artifact
      uses: actions/upload-artifact@v4
      with:
        name: armv5-deb
        path: ./target/armv5te-unknown-linux-musleabi/debian/*
  aarch64deb:
    needs: [build]
    runs-on: ubuntu-latest
    name: Aarch64Deb
    steps:
    - uses: actions/checkout@v4
    - name: BuildDeb
      id: debbuild
      uses: jamesmcm/cargo-deb-aarch64-debian@master
    - name: Upload Deb Artifact
      uses: actions/upload-artifact@v4
      with:
        name: aarch64-deb
        path: ./target/aarch64-unknown-linux-musl/debian/*
  amd64binaries:
    needs: [build, quickcheck]
    runs-on: ubuntu-latest
    name: Amd64StaticBinaries
    steps:
    - uses: actions/checkout@v4
    - name: StaticBinaryBuild
      id: amd64staticbuild
      uses: jamesmcm/cargo-deb-amd64-ubuntu@master
      with:
        cmd: cargo build --release --target=x86_64-unknown-linux-musl
    - name: Upload Vopono Artifact
      uses: actions/upload-artifact@v4
      with:
        name: x86_64-musl
        path: ./target/x86_64-unknown-linux-musl/release/vopono
  arm7binaries:
    needs: [build, quickcheck]
    runs-on: ubuntu-latest
    name: Armv7StaticBinaries
    steps:
    - uses: actions/checkout@v4
    - id: cargoversion
      run: cargo --version
    - id: rustcversion
      run: rustc --version
    - name: StaticBinaryBuild
      id: armv7staticbuild
      uses: jamesmcm/cargo-deb-armv7-debian@master
      with:
        cmd: cargo build --release --target=armv7-unknown-linux-musleabihf
    - name: Upload Vopono Artifact
      uses: actions/upload-artifact@v4
      with:
        name: armv7
        path: ./target/armv7-unknown-linux-musleabihf/release/vopono
  arm5binaries:
    needs: [build, quickcheck]
    runs-on: ubuntu-latest
    name: Armv5StaticBinaries
    steps:
    - uses: actions/checkout@v4
    - id: cargoversion
      run: cargo --version
    - id: rustcversion
      run: rustc --version
    - name: StaticBinaryBuild
      id: armv5staticbuild
      uses: jamesmcm/cargo-deb-armv5-debian@master
      with:
        cmd: cargo build --release --target=armv5te-unknown-linux-musleabi
    - name: Upload Vopono Artifact
      uses: actions/upload-artifact@v4
      with:
        name: armv5
        path: ./target/armv5te-unknown-linux-musleabi/release/vopono
  aarch64binaries:
    needs: [build, quickcheck]
    runs-on: ubuntu-latest
    name: Aarch64StaticBinaries
    steps:
    - uses: actions/checkout@v4
    - id: cargoversion
      run: cargo --version
    - id: rustcversion
      run: rustc --version
    - name: StaticBinaryBuild
      id: aarch64staticbuild
      uses: jamesmcm/cargo-deb-aarch64-debian@master
      with:
        cmd: cargo build --release --target=aarch64-unknown-linux-musl
    - name: Upload Vopono Artifact
      uses: actions/upload-artifact@v4
      with:
        name: aarch64
        path: ./target/aarch64-unknown-linux-musl/release/vopono
  update_release_draft:
    needs: [quickcheck, build, arm7binaries, arm5binaries, aarch64binaries, amd64binaries, raspbianbuild, armv5deb, aarch64deb, debbuild, opensuseleaprpmbuild, fedorarpmbuild]
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Create Release
        id: create_release
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # This token is provided by Actions, you do not need to create your own token
        with:
          tag_name: '${{ needs.quickcheck.outputs.version }}'
          release_name: Release ${{ needs.quickcheck.outputs.version }}
          body: 'Change Me'
          draft: true
          prerelease: false
      - uses: actions/download-artifact@v4
      - run: ls -lha
      - run: ls -lha x86_64-musl-deb
      - run: ls -lha x86_64-musl
      - run: ls -lha x86_64-gcc
      - run: ls -lha armv7-deb
      - run: ls -lha armv7
      - run: ls -lha armv5-deb
      - run: ls -lha armv5
      - run: ls -lha aarch64-deb
      - run: ls -lha aarch64
      - run: ls -lha fedorarpm
      - run: ls -lha opensuserpm
      - name: Upload amd64 deb Release Asset
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: ./x86_64-musl-deb/vopono_${{needs.quickcheck.outputs.version}}-1_amd64.deb
          asset_name: 'vopono_${{needs.quickcheck.outputs.version}}_amd64.deb'
          asset_content_type: application/vnd.debian.binary-package
      - name: Upload armv7 deb Release Asset
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: ./armv7-deb/vopono_${{needs.quickcheck.outputs.version}}-1_armhf.deb
          asset_name: 'vopono_${{needs.quickcheck.outputs.version}}_armhf.deb'
          asset_content_type: application/vnd.debian.binary-package
      - name: Upload armv5 deb Release Asset
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: ./armv5-deb/vopono_${{needs.quickcheck.outputs.version}}-1_armel.deb
          asset_name: 'vopono_${{needs.quickcheck.outputs.version}}_armel.deb'
          asset_content_type: application/vnd.debian.binary-package
      - name: Upload aarch64 deb Release Asset
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: ./aarch64-deb/vopono_${{needs.quickcheck.outputs.version}}-1_arm64.deb
          asset_name: 'vopono_${{needs.quickcheck.outputs.version}}_arm64.deb'
          asset_content_type: application/vnd.debian.binary-package
      - name: Upload amd64 rpm fedora Release Asset
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: ./fedorarpm/vopono-${{needs.quickcheck.outputs.version}}-1.fc42.x86_64.rpm
          asset_name: 'vopono_${{needs.quickcheck.outputs.version}}_amd64_fedora.rpm'
          asset_content_type: application/octet-stream
      - name: Upload amd64 rpm opensuse Release Asset
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: ./opensuserpm/vopono-${{needs.quickcheck.outputs.version}}-1.x86_64.rpm
          asset_name: 'vopono_${{needs.quickcheck.outputs.version}}_amd64_opensuseleap.rpm'
          asset_content_type: application/octet-stream
      - name: Upload Armv7 Static Binary
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: ./armv7/vopono
          asset_name: 'vopono_${{needs.quickcheck.outputs.version}}_linux_armv7'
          asset_content_type: application/octet-stream
      - name: Upload Armv5 Static Binary
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: ./armv5/vopono
          asset_name: 'vopono_${{needs.quickcheck.outputs.version}}_linux_armv5'
          asset_content_type: application/octet-stream
      - name: Upload Aarch64 Static Binary
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: ./aarch64/vopono
          asset_name: 'vopono_${{needs.quickcheck.outputs.version}}_linux_aarch64'
          asset_content_type: application/octet-stream
      - name: Upload Amd64 Static Binary
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: ./x86_64-musl/vopono
          asset_name: 'vopono_${{needs.quickcheck.outputs.version}}_linux_x86-64_musl'
          asset_content_type: application/octet-stream
      - name: Upload Amd64 Binary
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: ./x86_64-gcc/vopono
          asset_name: 'vopono_${{needs.quickcheck.outputs.version}}_linux_x86-64_gcc'
          asset_content_type: application/octet-stream
