on:
  push:
    tags:
      - "v*"

name: Create Release

jobs:
  amd64_build:
    name: Setup and Build for amd64
    runs-on: ubuntu-latest
    container: robiot/xclicker
    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Build x64
        run: |
          mkdir -p outputs

          make deb TARGET_ARCH=amd64
          cp pkg/deb/*.deb outputs/

          make appimg TARGET_ARCH=amd64
          cp pkg/AppImage/*.AppImage outputs/

      - uses: actions/upload-artifact@v3
        with:
          name: output-artifact
          path: outputs

  build:
    name: Build for ARM and Create Release
    runs-on: ubuntu-latest
    needs: amd64_build
    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - uses: actions/download-artifact@v3
        with:
          name: output-artifact
          path: outputs

      - name: Build ARM
        uses: uraimo/run-on-arch-action@v2.1.1
        with:
          arch: armv7
          distro: ubuntu18.04

          githubToken: ${{ github.token }}

          dockerRunArgs: |
            --volume "${PWD}/outputs:${PWD}/outputs"

          install: |
            apt-get update -y
            apt-get install -y meson pkg-config gtk+-3.0 libx11-dev libxi-dev libxtst-dev \
              python3-pip python3-setuptools patchelf desktop-file-utils libgdk-pixbuf2.0-dev fakeroot strace wget \
              build-essential manpages-dev software-properties-common

            add-apt-repository ppa:ubuntu-toolchain-r/test -y
            apt-get install -y gcc-11
            update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 30

            wget https://github.com/AppImage/AppImageKit/releases/download/continuous/appimagetool-armhf.AppImage -O /opt/appimagetool
            cd /opt/; chmod +x appimagetool; sed -i 's|AI\x02|\x00\x00\x00|' appimagetool; ./appimagetool --appimage-extract
            mv /opt/squashfs-root /opt/appimagetool.AppDir
            ln -s /opt/appimagetool.AppDir/AppRun /usr/local/bin/appimagetool

          run: |
            make deb TARGET_ARCH=armhf
            cp pkg/deb/*.deb outputs/

            make appimg TARGET_ARCH=armhf
            cp pkg/AppImage/*.AppImage outputs/

      # Lots of repeated stuff, but well :\
      - name: Build aarch64
        uses: uraimo/run-on-arch-action@v2.1.1
        with:
          arch: aarch64
          distro: ubuntu18.04

          githubToken: ${{ github.token }}

          dockerRunArgs: |
            --volume "${PWD}/outputs:${PWD}/outputs"

          install: |
            apt-get update -y
            apt-get install -y meson pkg-config gtk+-3.0 libx11-dev libxi-dev libxtst-dev \
              python3-pip python3-setuptools patchelf desktop-file-utils libgdk-pixbuf2.0-dev fakeroot strace wget \
              build-essential manpages-dev software-properties-common

            add-apt-repository ppa:ubuntu-toolchain-r/test -y
            apt-get install -y gcc-11
            update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 30

            wget https://github.com/AppImage/AppImageKit/releases/download/continuous/appimagetool-aarch64.AppImage -O /opt/appimagetool
            cd /opt/; chmod +x appimagetool; sed -i 's|AI\x02|\x00\x00\x00|' appimagetool; ./appimagetool --appimage-extract
            mv /opt/squashfs-root /opt/appimagetool.AppDir
            ln -s /opt/appimagetool.AppDir/AppRun /usr/local/bin/appimagetool

          run: |
            rm -rf build/
            make deb TARGET_ARCH=arm64
            cp pkg/deb/*.deb outputs/

            make appimg TARGET_ARCH=arm64
            cp pkg/AppImage/*.AppImage outputs/

      - name: Create Release
        uses: softprops/action-gh-release@v0.1.14
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          generate_release_notes: true
          draft: false
          prerelease: false
          files: |
            outputs/*
