name: publish

on:
  release:
    types:
      - published

jobs:
  crate:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2

      - name: deps
        run: |
          sudo apt install -y libxcb-shape0-dev libxcb-xfixes0-dev

      - name: login
        run: echo ${{ secrets.CRATES_IO_TOKEN }} | cargo login

      - name: publish
        run: cargo publish

  package-bin-linux:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2

      - name: deps
        run: |
          sudo apt install -y libxcb-shape0-dev libxcb-xfixes0-dev

      - name: build
        run: cargo build --release

      - name: tarball
        run: |
          cd ./target/release
          tar czvf kbs2-linux-${{ github.event.release.tag_name }}.tar.gz ./kbs2 ../../LICENSE ../../README.md ../../contrib/
          shasum -a 256 kbs2-linux-${{ github.event.release.tag_name }}.tar.gz > kbs2-linux-${{ github.event.release.tag_name }}.sha256

      - name: attach tarball to release
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ github.event.release.upload_url }}
          asset_path: ./target/release/kbs2-linux-${{ github.event.release.tag_name }}.tar.gz
          asset_name: kbs2-linux-${{ github.event.release.tag_name }}.tar.gz
          asset_content_type: application/gzip

      - name: attach checksum to release
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ github.event.release.upload_url }}
          asset_path: ./target/release/kbs2-linux-${{ github.event.release.tag_name }}.sha256
          asset_name: kbs2-linux-${{ github.event.release.tag_name }}.sha256
          asset_content_type: text/plain

  package-deb:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2

      - name: deps
        run: |
          sudo apt install -y libxcb-shape0-dev libxcb-xfixes0-dev
          cargo install cargo-deb

      - name: package
        run: cargo deb

      - name: find deb
        id: find_deb
        run: |
          deb_file=$(find ./target/debian/ -maxdepth 1 -type f -name '*.deb')
          echo "::set-output name=deb_file::${deb_file}"

          deb_name=$(basename "${deb_file}")
          echo "::set-output name=deb_name::${deb_name}"

      - name: attach to release
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ github.event.release.upload_url }}
          asset_path: ${{ steps.find_deb.outputs.deb_file }}
          asset_name: ${{ steps.find_deb.outputs.deb_name }}
          asset_content_type: application/vnd.debian.binary-package
