# Author: K4YT3X <i@k4yt3x.com>
# Cross-compiles dvmhost and dvmcmd for all architectures upon pushing a new tag.

name: dvmhost-build-release
on:
  push:
    tags:
      - "*"

permissions:
  contents: write

jobs:
  setup:
    name: Setup
    runs-on: ubuntu-22.04
    outputs:
      APPNAME: ${{ steps.get_appname.outputs.APPNAME }}
      VERSION: ${{ steps.get_version.outputs.VERSION }}
    steps:
      - name: Get app name
        id: get_appname
        run: echo APPNAME=${{ github.event.repository.name }} >> $GITHUB_OUTPUT
      - name: Get version
        id: get_version
        run: echo VERSION=${GITHUB_REF/refs\/tags\//} >> $GITHUB_OUTPUT

  build:
    name: Build
    needs: [setup]
    strategy:
      matrix:
        arch: ["amd64", "arm", "aarch64", "armhf"]
    runs-on: ubuntu-22.04
    env:
      PACKAGENAME: ${{ needs.setup.outputs.APPNAME }}-${{ needs.setup.outputs.VERSION }}-${{ matrix.arch }}
      DEBIAN_FRONTEND: noninteractive
    steps:
      - name: Checkout repository
        uses: actions/checkout@v3
        with:
          ref: ${{ needs.setup.outputs.VERSION }}
          submodules: recursive
      - name: Setup architecture support
        run: |
          sudo dpkg --add-architecture armhf
          sudo dpkg --add-architecture arm64
          echo "deb [arch=amd64] http://archive.ubuntu.com/ubuntu jammy main restricted universe multiverse" | sudo tee /etc/apt/sources.list
          echo "deb [arch=amd64] http://archive.ubuntu.com/ubuntu jammy-updates main restricted universe multiverse" | sudo tee -a /etc/apt/sources.list
          echo "deb [arch=armhf,arm64] http://ports.ubuntu.com/ubuntu-ports jammy main restricted universe multiverse" | sudo tee -a /etc/apt/sources.list
          echo "deb [arch=armhf,arm64] http://ports.ubuntu.com/ubuntu-ports jammy-updates main restricted universe multiverse" | sudo tee -a /etc/apt/sources.list
          sudo apt-get update
      - name: Install dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y git build-essential cmake \
            g++-arm-linux-gnueabihf \
            gcc-arm-linux-gnueabihf \
            g++-aarch64-linux-gnu

          if [[ "${{ matrix.arch }}" == 'aarch64' ]]; then
            sudo apt-get install -y libasio-dev:arm64 libssl-dev:arm64 libncurses-dev:arm64
          elif [[ "${{ matrix.arch }}" == 'arm' ]]; then
            sudo apt-get install -y libasio-dev:armhf libssl-dev:armhf libncurses-dev:armhf
          elif [[ "${{ matrix.arch }}" == 'armhf' ]]; then
            sudo apt-get install -y libasio-dev:armhf libssl-dev:armhf
          else
            sudo apt-get install -y libasio-dev libssl-dev libncurses-dev
          fi
      - name: Build
        run: |
          if [[ "${{ matrix.arch }}" == 'amd64' || "${{ matrix.arch }}" == 'arm' || "${{ matrix.arch }}" == 'aarch64' ]]; then
            build_args='-DENABLE_TUI_SUPPORT=1'
          else
            build_args='-DENABLE_TUI_SUPPORT=0'
          fi

          build_args="$build_args -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_FLAGS='-s' -DCMAKE_CXX_FLAGS='-s'"

          if [[ "${{ matrix.arch }}" == 'armhf' ]]; then
            cmake $(echo $build_args) -DCROSS_COMPILE_RPI_ARM=1 .
          else
            cmake $(echo $build_args) \
              -D "CROSS_COMPILE_$(echo '${{ matrix.arch }}' | tr '[:lower:]' '[:upper:]')=1" .
          fi

          make -j $(nproc)
          make strip
          make tarball
      - name: Package
        run: |
          mv dvmhost_*.tar.gz ${{ env.PACKAGENAME }}.tar.gz
          mkdir -p ${{ env.PACKAGENAME }}
      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: ${{ needs.setup.outputs.APPNAME }}-${{ matrix.arch }}
          path: ${{ env.PACKAGENAME }}.tar.gz

  create-release:
    name: Create Release
    needs: [setup, build]
    runs-on: ubuntu-22.04
    outputs:
      upload_url: ${{ steps.create_release.outputs.upload_url }}
    steps:
      - name: Create release
        id: create_release
        uses: softprops/action-gh-release@v1
        with:
          tag_name: ${{ needs.setup.outputs.VERSION }}
          name: Release ${{ needs.setup.outputs.VERSION }}
          draft: false
          prerelease: false

  upload:
    name: Upload
    needs: [setup, build, create-release]
    strategy:
      matrix:
        arch: ["amd64", "arm", "aarch64", "armhf"]
    runs-on: ubuntu-22.04
    env:
      PACKAGENAME: ${{ needs.setup.outputs.APPNAME }}-${{ needs.setup.outputs.VERSION }}-${{ matrix.arch }}
      DEBIAN_FRONTEND: noninteractive
    steps:
      - name: Download artifact
        uses: actions/download-artifact@v4
        with:
          name: ${{ needs.setup.outputs.APPNAME }}-${{ matrix.arch }}
      - name: Upload release asset
        uses: softprops/action-gh-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ needs.setup.outputs.VERSION }}
          files: ${{ env.PACKAGENAME }}.tar.gz
          append_body: true
