name: 'Install Qt'
description: 'Install Qt'
inputs:
  qt_version:
    description: 'Version of Qt to install'
    required: true
  qt_archives:
    description: 'List of Qt archives to intall'
  qt_modules:
    description: 'List of Qt modules to install'
  platform:
    description: 'Operating system (linux|windows|macos)'
    required: true
runs:
  using: composite
  steps:
    - name: Install dependencies
      if: ${{ inputs.platform == 'linux' }}
      shell: bash
      run: |
        sudo apt-get update
        sudo apt-get install -y --no-install-recommends \
          python3 python3-pip \
          build-essential \
          dbus dbus-x11 \
          libgl-dev libegl-dev
    - name: Install Qt
      shell: bash
      run: |
        pip3 install aqtinstall~=2.1
        case "${{ inputs.platform }}" in
          "windows")
            QT_ARCH="win64_msvc2019_64"
            QT_INSTALL_PATH="C:\\Qt"
            QT_BASE_DIR="${QT_INSTALL_PATH}\\${{ inputs.qt_version }}\\msvc2019_64"
            QT_OS="windows"
            ;;
          "linux")
            QT_ARCH="gcc_64"
            QT_INSTALL_PATH="/opt/qt"
            QT_BASE_DIR="${QT_INSTALL_PATH}/${{ inputs.qt_version }}/${QT_ARCH}"
            QT_OS="linux"
            ;;
          "macos")
            QT_ARCH=""
            QT_INSTALL_PATH="/Users/runner/qt"
            if [[ "$(echo ${{ inputs.qt_version }} | cut -d'.' -f1)" == "6" ]]; then
              QT_BASE_DIR="${QT_INSTALL_PATH}/${{ inputs.qt_version }}/macos"
            else
              QT_BASE_DIR="${QT_INSTALL_PATH}/${{ inputs.qt_version }}/clang_64"
            fi
            QT_OS="mac"
            ;;
        esac

        if [[ -n "${{ inputs.qt_archives }}" ]]; then
          qt_archives="--archives ${{ inputs.qt_archives }}"
        fi
        if [[ -n "${{ inputs.qt_modules }}" ]]; then
          qt_modules="--modules ${{ inputs.qt_modules }}"
        fi
        python3 -m aqt install-qt -O ${QT_INSTALL_PATH} ${QT_OS} desktop ${{ inputs.qt_version }} ${QT_ARCH} ${qt_archives} ${qt_modules}

        echo "${QT_BASE_DIR}/bin" >> $GITHUB_PATH
        echo "CMAKE_PREFIX_PATH=${QT_BASE_DIR}/lib/cmake" >> $GITHUB_ENV
        echo "LD_LIBRARY_PATH=${QT_BASE_DIR}/lib:${LD_LIBRARY_PATH}" >> $GITHUB_ENV
        echo "XDG_DATA_DIRS=${QT_BASE_DIR}/share:${XDG_DATA_DIRS}" >> $GITHUB_ENV

        if [[ "${{ inputs.platform }}" == "windows" ]]; then
          powershell "./.github/actions/install-qt/install-dbus.ps1" "$QT_BASE_DIR"
        fi
