---
name: PackageCloud

on:
  workflow_call:
    secrets:
      PACKAGECLOUD_TOKEN:
        required: true

jobs:
  build:
    runs-on: ubuntu-latest
    continue-on-error: true
    env:
      PACKAGECLOUD_TOKEN: ${{ secrets.PACKAGECLOUD_TOKEN }}

    steps:
      - name: Checkout
        uses: actions/checkout@v6

      - name: Install dependencies
        run: sudo apt -y install musl-dev musl-tools

      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          targets: x86_64-unknown-linux-musl

      - name: Build
        run: cargo build --release --locked --target x86_64-unknown-linux-musl

      - name: Setup Ruby
        uses: ruby/setup-ruby@v1
        with:
          ruby-version: 3
          bundler-cache: true

      - name: Install fpm
        run: gem install --quiet --no-document fpm

      - name: Install PackageCloud
        run: gem install --quiet --no-document package_cloud

      - name: Get Version
        run: echo "VERSION=$(grep -oP -m 1 'version = "\K[^"]+' Cargo.toml)" >> $GITHUB_ENV

      - name: Get description
        run: echo "DESCRIPTION=$(grep -oP -m 1 'description = "\K[^"]+' Cargo.toml)"
          >> $GITHUB_ENV

      - name: Create dist dir
        run: mkdir dist

      - name: create deb
        run: fpm -s dir -t deb -n s3m -v ${{ env.VERSION }} --description "${{ env.DESCRIPTION }}"  -a
          amd64 -p dist -C target/x86_64-unknown-linux-musl/release/ ./s3m=/usr/local/bin/

      - name: create rpm
        run: fpm -s dir -t rpm -n s3m -v ${{ env.VERSION }} --description "${{ env.DESCRIPTION }}"
          -a x86_64 -p dist -C target/x86_64-unknown-linux-musl/release/ ./s3m=/usr/local/bin/

      - name: run ls
        run: ls -al dist

      - name: push Debian
        run: |-
          for distro in squeeze wheezy jessie stretch buster bullseye bookworm; do
            package_cloud push nbari/s3m/debian/$distro dist/*.deb --skip-errors
          done

      - name: push Ubuntu
        run: |-
          for distro in bionic focal jammy; do
            package_cloud push nbari/s3m/ubuntu/$distro dist/*.deb --skip-errors
          done

      - name: push Raspbian
        run: |-
          for distro in stretch buster bullseye; do
            package_cloud push nbari/s3m/raspbian/$distro dist/*.deb --skip-errors
          done

      - name: push Elementaryos
        run: |-
          for distro in odin jolnir horus; do
            package_cloud push nbari/s3m/elementaryos/$distro dist/*.deb --skip-errors
          done

      - name: push Linuxmint
        run: |-
          for distro in vanessa vera victoria; do
            package_cloud push nbari/s3m/linuxmint/$distro dist/*.deb --skip-errors
          done

      - name: push Enterprise Linux
        run: |-
          for distro in 7 8 9; do
            package_cloud push nbari/s3m/el/$distro dist/*.rpm --skip-errors
          done

      - name: push Fedora
        run: |-
          for distro in 38 39 40; do
            package_cloud push nbari/s3m/fedora/$distro dist/*.rpm --skip-errors
          done

      - name: push Opensuse
        run: |-
          for distro in 15.4 15.5 15.6; do
            package_cloud push nbari/s3m/opensuse/$distro dist/*.rpm --skip-errors
          done

      - name: push Poky
        run: |-
          for distro in mickledore nanbield; do
            package_cloud push nbari/s3m/poky/$distro dist/*.rpm --skip-errors
          done
