# Author: K4YT3X <i@k4yt3x.com>

name: dvmhost-build
on:
  workflow_dispatch:
    inputs:
      build_type:
        description: "CMake Build Type"
        required: true
        default: "Release"
        type: choice
        options:
          - Release
          - Debug
          - RelWithDebInfo
          - MinSizeRel
      strip_symbols:
        description: "Strip Symbols"
        required: false
        type: boolean
      create_pre_release:
        description: "Create Pre-Release"
        required: false
        type: boolean
  push:
    branches:
      - master
  pull_request:
    branches:
      - master

permissions:
  contents: write

jobs:
  setup:
    name: Setup
    runs-on: ubuntu-20.04
    outputs:
      APPNAME: ${{ steps.get_appname.outputs.APPNAME }}
      DATE: ${{ steps.get_date.outputs.DATE }}
    steps:
      - name: Get app name
        id: get_appname
        run: echo APPNAME=${{ github.event.repository.name }} >> $GITHUB_OUTPUT
      - name: Get date
        id: get_date
        run: echo DATE=$(date +'%Y-%m-%d') >> $GITHUB_OUTPUT

  build:
    name: Build
    needs: [setup]
    strategy:
      matrix:
#        arch: ["amd64", "arm", "aarch64"]
        arch: ["amd64", "arm", "aarch64", "armhf"]
    runs-on: ubuntu-20.04
    env:
      PACKAGENAME: ${{ needs.setup.outputs.APPNAME }}-${{ needs.setup.outputs.DATE }}-${{ matrix.arch }}
      DEBIAN_FRONTEND: noninteractive
    steps:
      - name: Checkout repository
        uses: actions/checkout@v3
        with:
          submodules: recursive
      - 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 \
            libasio-dev libssl-dev
      - name: Build
        run: |
          build_args='-DENABLE_TUI_SUPPORT=0'
          if [[ "${{ github.event_name }}" == 'push' ]]; then
            build_args="$build_args -DCMAKE_BUILD_TYPE=${{ inputs.build_type }}"
            if [[ "${{ inputs.strip_symbols }}" == 'true' ]]; then
              build_args="$build_args -DCMAKE_C_FLAGS='-s' -DCMAKE_CXX_FLAGS='-s'"
            fi
          else
            build_args="$build_args -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_FLAGS='-s' -DCMAKE_CXX_FLAGS='-s'"
          fi

          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)
      - name: Package
        run: |
          mkdir -p ${{ env.PACKAGENAME }}
          cp dvmcmd dvmhost dvmfne ${{ env.PACKAGENAME }}
          zip -9 -r ${{ env.PACKAGENAME }}.zip ${{ env.PACKAGENAME }}
      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: ${{ needs.setup.outputs.APPNAME }}-${{ matrix.arch }}
          path: ${{ env.PACKAGENAME }}.zip

  create-release:
    if: ${{ github.event.inputs.create_pre_release == 'true' }}
    name: Create Release
    needs: [setup, build]
    runs-on: ubuntu-20.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.DATE }}
          name: Nightly Build ${{ needs.setup.outputs.DATE }}
          draft: false
          prerelease: true

  upload:
    if: ${{ github.event.inputs.create_pre_release == 'true' }}
    name: Upload
    needs: [setup, build, create-release]
    strategy:
      matrix:
        arch: ["amd64", "arm", "aarch64", "armhf"]
    runs-on: ubuntu-20.04
    env:
      PACKAGENAME: ${{ needs.setup.outputs.APPNAME }}-${{ needs.setup.outputs.DATE }}-${{ 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: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ needs.create-release.outputs.upload_url }}
          asset_path: ${{ env.PACKAGENAME }}.zip
          asset_name: ${{ env.PACKAGENAME }}.zip
          asset_content_type: application/zip
