name: CI-Linux

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

  # Allows you to run this workflow manually from the Actions tab.
  workflow_dispatch:
    inputs:
      debug_enabled:
        type: boolean
        description: 'Run the build with tmate debugging enabled'
        required: false
        default: false

env:
    VNOTE_VER: 3.19.2
    CMAKE_VER: 3.24.3

jobs:
  build-linux:
    name: Build On Ubuntu
    runs-on: ubuntu-latest
    timeout-minutes: 120

    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE.
      - uses: actions/checkout@v2
        with:
          fetch-depth: 1

      - name: Init Submodules
        run: |
          auth_header="$(git config --local --get http.https://github.com/.extraheader)"
          git submodule sync --recursive
          git -c "http.extraheader=$auth_header" -c protocol.version=2 submodule update --init --force --recursive --depth=1

      - name: Install a Fresh CMake
        run: |
          wget --no-verbose https://github.com/Kitware/CMake/releases/download/v${CMAKE_VER}/cmake-${CMAKE_VER}-Linux-x86_64.sh
          chmod +x cmake-${CMAKE_VER}-Linux-x86_64.sh
          mkdir ${{runner.workspace}}/cmake
          sudo ./cmake-${CMAKE_VER}-Linux-x86_64.sh --skip-license --prefix=${{runner.workspace}}/cmake
          sudo rm -f /usr/local/bin/cmake /usr/local/bin/cpack
          sudo ln -s ${{runner.workspace}}/cmake/bin/cmake /usr/local/bin/cmake
          sudo ln -s ${{runner.workspace}}/cmake/bin/cpack /usr/local/bin/cpack

      - name: Install linuxdeploy
        uses: miurahr/install-linuxdeploy-action@v1
        with:
          plugins: qt appimage

      - name: Install Dependencies
        run: |
          sudo DEBIAN_FRONTEND=noninteractive apt-get update
          sudo DEBIAN_FRONTEND=noninteractive apt-get install -y libfcitx5-qt-dev fcitx-libs-dev extra-cmake-modules libxkbcommon-dev
          sudo DEBIAN_FRONTEND=noninteractive apt-get install -y tree
          sudo DEBIAN_FRONTEND=noninteractive apt-get install -y fuse libxcb-cursor-dev
          python3 -m pip config set global.break-system-packages true

      - name: Cache Qt
        id: cache-qt
        uses: actions/cache@v4
        with:
          path: ${{runner.workspace}}/Qt
          key: ${{ runner.os }}-QtCache-6.8

      - name: Install Qt
        uses: jurplel/install-qt-action@v3
        with:
          version: 6.8.3
          target: desktop
          modules: 'qtwebengine qtwebchannel qtpositioning qtpdf qtimageformats qt5compat qtserialport'
          tools: 'tools_opensslv3_src'
          cache: 'true'

      - name: Compile OpenSSLV3
        run: |
          cd ${Qt6_DIR}/../../Tools/OpenSSLv3/src
          ./Configure
          make -j2
          sudo make install

      - name: Create Build Dir
        run: mkdir build
        working-directory: ${{runner.workspace}}

      - name: Compile fcitxqt5
        run: |
          git clone https://github.com/fcitx/fcitx-qt5
          cd fcitx-qt5
          mkdir build && cd build
          cmake -DENABLE_QT5=OFF -DENABLE_QT6=ON ..
          make -j2
          sudo make install
        working-directory: ${{runner.workspace}}/build

      - name: Compile qt6ct
        run: |
          git clone https://github.com/trialuser02/qt6ct qt6ct.git
          cd qt6ct.git
          qmake
          make -j$(nproc) && sudo make install
        working-directory: ${{runner.workspace}}/build

      - name: Configure Project
        run: |
          qmake -v
          cmake --version
          cmake ${GITHUB_WORKSPACE}
        working-directory: ${{runner.workspace}}/build

      - name: Build Project
        run: |
          # Remove the libqsqlmimer.so as libmimerapi.so is not deployed with Qt6
          rm ${{runner.workspace}}/Qt/6.*/gcc_64/plugins/sqldrivers/libqsqlmimer.so
          cmake --build . --target pack
        working-directory: ${{runner.workspace}}/build

      - name: Fix Package
        run: |
          mkdir fixpackage
          mv VNote*.AppImage ./fixpackage
          pushd fixpackage
          # Extract the AppImage
          ./VNote*.AppImage --appimage-extract
          # Remove libnss3.so and libnssutil3.so
          rm ./squashfs-root/usr/lib/libnss*.so
          # Re-package
          rm VNote*.AppImage
          linuxdeploy-plugin-appimage-x86_64.AppImage --appdir=./squashfs-root
          mv VNote*.AppImage ../
          popd
          mv VNote*.AppImage VNote-${{env.VNOTE_VER}}-linux-x64.AppImage
        working-directory: ${{runner.workspace}}/build

      - name: Archive Artifacts
        uses: actions/upload-artifact@v4
        with:
          name: VNote-${{env.VNOTE_VER}}-linux-x64.AppImage
          path: ${{runner.workspace}}/build/VNote-${{env.VNOTE_VER}}-linux-x64.AppImage

      - name: Update Tag
        if: github.ref == 'refs/heads/master'
        run: |
          git tag --force continuous-build ${GITHUB_SHA}
          git push --force --tags

      - name: Update Continuous Build Release
        if: github.ref == 'refs/heads/master'
        uses: johnwbyrd/update-release@v1.0.0
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          files: ${{runner.workspace}}/build/VNote-${{env.VNOTE_VER}}-linux-x64.AppImage
          release: Continuous Build
          tag: continuous-build

      - name: Release
        if: github.ref == 'refs/heads/master' && startsWith(github.event.head_commit.message, '[Release]')
        uses: ncipollo/release-action@v1.11.0
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          artifacts: ${{runner.workspace}}/build/VNote-${{env.VNOTE_VER}}-linux-x64.AppImage
          commit: master
          tag: v${{env.VNOTE_VER}}
          allowUpdates: true
          draft: true
