name: Continuous Deployment

on:
  push:
    tags:
      - "v*.*.*"
  workflow_dispatch:

jobs:
  publish:
    name: Publishing for ${{ matrix.os }}
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        build:
          - macos
          - macos-aarch64
          - linux
          - linux-musl
          - linux-aarch64
          - linux-arm
        rust: [stable]
        include:
          # See the list: https://github.com/cross-rs/cross

          - build: macos
            os: macos-latest
            target: x86_64-apple-darwin

          - build: macos-aarch64
            os: macos-latest
            target: aarch64-apple-darwin

          - build: linux
            os: ubuntu-latest
            target: x86_64-unknown-linux-gnu

          - build: linux-musl
            os: ubuntu-latest
            target: x86_64-unknown-linux-musl

          - build: linux-aarch64
            os: ubuntu-latest
            target: aarch64-unknown-linux-gnu

          - build: linux-arm
            os: ubuntu-latest
            target: arm-unknown-linux-gnueabihf

    steps:
      - uses: actions/checkout@v3

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

      - name: Installing needed macOS dependencies
        if: matrix.os == 'macos-latest'
        run: brew install openssl@1.1

      - name: Installing needed Ubuntu dependencies
        if: matrix.os == 'ubuntu-latest'
        run: |
          sudo apt-get update --fix-missing
          sudo apt-get install -y --no-install-recommends liblua5.1-0-dev libluajit-5.1-dev gcc pkg-config curl git make ca-certificates
          sudo apt-get install -y snapd
          # sudo snap install snapcraft --classic
          # sudo snap install multipass --classic --beta

      - if: matrix.build == 'linux-musl'
        run: sudo apt-get install -y musl-tools

      - if: matrix.build == 'linux-aarch64'
        run: sudo apt-get install -y gcc-aarch64-linux-gnu

      - if: matrix.build == 'linux-arm'
        run: |
          sudo apt-get install -y gcc-multilib
          sudo apt-get install -y gcc-arm-linux-gnueabihf
          sudo ln -s /usr/include/asm-generic/ /usr/include/asm

      - name: Running cargo build
        run: cargo build --locked --release --target ${{ matrix.target }}

      # - name: Running snapcraft build
      #   run: |
      #     snapcraft
      #     printf ' [ INFO ] generated <snapcraft> files include:\n'
      #     command ls -Al | grep "\.snap" | awk '{ print $9 }'
      #     mv ./*.snap ./xplr.snap

      - name: Install gpg secret key
        run: |
          cat <(echo -e "${{ secrets.GPG_SECRET }}") | gpg --batch --import
          gpg --list-secret-keys --keyid-format LONG

      - name: Packaging final binary
        shell: bash
        run: |
          cd target/${{ matrix.target }}/release
          BINARY_NAME=xplr
          RELEASE_NAME=$BINARY_NAME-${{ matrix.build }}
          tar czvf $RELEASE_NAME.tar.gz $BINARY_NAME
          shasum -a 256 $RELEASE_NAME.tar.gz > $RELEASE_NAME.sha256
          cat <(echo "${{ secrets.GPG_PASS }}") | gpg --pinentry-mode loopback --passphrase-fd 0 --detach-sign --armor $RELEASE_NAME.tar.gz

      - name: Releasing assets
        uses: softprops/action-gh-release@v1
        with:
          files: |
            target/${{ matrix.target }}/release/xplr-${{ matrix.build }}.tar.gz
            target/${{ matrix.target }}/release/xplr-${{ matrix.build }}.sha256
            target/${{ matrix.target }}/release/xplr-${{ matrix.build }}.tar.gz.asc
            xplr.snap
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      # - name: Cleaning snapcraft
      #   run: |
      #     command rm --verbose ./*.snap
      #     snapcraft clean

  publish-gpg-signature:
    name: Publishing GPG signature
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Install gpg secret key
        run: |
          cat <(echo -e "${{ secrets.GPG_SECRET }}") | gpg --batch --import
          gpg --list-secret-keys --keyid-format LONG

      - name: Signing archive with GPG
        run: |
          VERSION=${GITHUB_REF##*v}
          git -c tar.tar.gz.command='gzip -cn' archive -o xplr-${VERSION:?}.tar.gz --format tar.gz --prefix "xplr-${VERSION:?}/" "v${VERSION}"
          cat <(echo "${{ secrets.GPG_PASS }}") | gpg --pinentry-mode loopback --passphrase-fd 0 --detach-sign --armor "xplr-${VERSION:?}.tar.gz"
          mv "xplr-${VERSION:?}.tar.gz.asc" "source.tar.gz.asc"

      - name: Releasing GPG signature
        uses: softprops/action-gh-release@v1
        with:
          files: |
            source.tar.gz.asc
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

  publish-cargo:
    name: Publishing to Cargo
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3

      - uses: dtolnay/rust-toolchain@stable
        with:
          toolchain: stable

      - run: |
          sudo apt-get update --fix-missing
          sudo apt-get install -y -qq pkg-config libssl-dev libxcb1-dev libxcb-render0-dev libxcb-shape0-dev libxcb-xfixes0-dev

      - run: cargo publish --allow-dirty
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_API_KEY }}
