name: Build PageEdit AppImage

on:
  push:

  workflow_dispatch:
    inputs:
      vers_info:
        description: 'Version for file name'
        required: true
        default: ''

env:
  QT_VER: 6.8.2
  DOWNLOADQT: https://github.com/dougmassay/win-qtwebkit-5.212/releases/download/v5.212-1/AppImageWebEngine682.tar.xz

jobs:
  build:
    if: "!contains(github.event.head_commit.message, '[skip ci]')"
    runs-on: ubuntu-22.04

    steps:
    - name: Get Short SHA
      if: ${{ github.event_name != 'workflow_dispatch' }}
      id: get_short_sha
      run: echo "SHORT_SHA=$(echo $GITHUB_SHA | cut -c1-7)" >> $GITHUB_ENV

    - name: Get Dispatch Input
      if: ${{ github.event_name == 'workflow_dispatch' }}
      id: get_dispatch_input
      run: echo "DISP_INPUT=${{ github.event.inputs.vers_info }}" >> $GITHUB_ENV

    - name: Get Tag Name
      if: startswith( github.ref, 'refs/tags/')
      id: get_tag_name
      run: echo "TAG_NAME=${GITHUB_REF/refs\/tags\//}" >> $GITHUB_ENV

    - name: Checkout PageEdit
      uses: actions/checkout@v4
      with:
        ref: master
  
    - name: Install Qt
      uses: jurplel/install-qt-action@v3
      with:
        version: ${{env.QT_VER}}
        dir: ${{runner.workspace}}/Qt
        modules: 'qtpositioning qtpdf qtwebchannel qtserialport qtimageformats'
        setup-python: 'false'

    - name: Install patched QtWebEngine
      run: |
        curl -L -o qtwebengine.tar.xz "$DOWNLOADQT"
        tar -xJvf qtwebengine.tar.xz -C ${{runner.workspace}}/Qt

    - uses: lukka/get-cmake@latest

    - name: Install dependencies
      run: |
        sudo apt-get install -y mesa-common-dev libglu1-mesa-dev libxcb-cursor-dev libgtk-3-dev libtiff-dev libwebp-dev
        which cmake
        cmake --version
        which ninja
        ninja --version
        which qmake
        qmake -v
        which python3
        python3 --version
        gcc --version
        mkdir -p ${{runner.workspace}}/build/pageedit.AppDir/usr/lib
        cd ${{runner.workspace}}/build
        wget -nv https://github.com/linuxdeploy/linuxdeploy/releases/download/continuous/linuxdeploy-x86_64.AppImage
        chmod +x linuxdeploy-x86_64.AppImage
        wget -nv https://github.com/linuxdeploy/linuxdeploy-plugin-qt/releases/download/continuous/linuxdeploy-plugin-qt-x86_64.AppImage
        chmod +x linuxdeploy-plugin-qt-x86_64.AppImage
        wget -nv https://github.com/linuxdeploy/linuxdeploy-plugin-appimage/releases/download/continuous/linuxdeploy-plugin-appimage-x86_64.AppImage
        chmod +x linuxdeploy-plugin-appimage-x86_64.AppImage
        cp /lib/x86_64-linux-gnu/libtiff.so.5 ${{runner.workspace}}/build/pageedit.AppDir/usr/lib/
        cp /lib/x86_64-linux-gnu/libwebp.so.7 ${{runner.workspace}}/build/pageedit.AppDir/usr/lib/


    - name: Build PageEdit
      working-directory: ${{runner.workspace}}/build
      run: |
        cmake $GITHUB_WORKSPACE \
        -G "Ninja" \
        -DCMAKE_PREFIX_PATH=${QT_ROOT_DIR}/lib/cmake \
        -DCMAKE_BUILD_TYPE=Release \
        -DCMAKE_INSTALL_PREFIX=/usr \
        -DAPPIMAGE_BUILD=1 \
        -DCMAKE_SKIP_RPATH=ON
        ninja -j$(getconf _NPROCESSORS_ONLN)
        DESTDIR=pageedit.AppDir ninja install

    - name: Make AppImage
      working-directory: ${{runner.workspace}}/build
      run: |
        if [[ -v DISP_INPUT ]]; then
          export LINUXDEPLOY_OUTPUT_APP_NAME="PageEdit-$DISP_INPUT"
        elif [[ -v TAG_NAME ]]; then
          export LINUXDEPLOY_OUTPUT_APP_NAME="PageEdit-$TAG_NAME"
        else
          export LINUXDEPLOY_OUTPUT_APP_NAME="PageEdit-$SHORT_SHA"
        fi
        DEPLOY_PLATFORM_THEMES=1 \
        DISABLE_COPYRIGHT_FILES_DEPLOYMENT=1 \
        EXTRA_PLATFORM_PLUGINS=libqwayland-generic.so \
        EXTRA_QT_MODULES="waylandcompositor" \
        ./linuxdeploy-x86_64.AppImage --appdir pageedit.AppDir --plugin qt
        python3 $GITHUB_WORKSPACE/.github/workflows/appimg_cleanup.py ${{runner.workspace}}/build/pageedit.AppDir/usr/lib
        cp -f $GITHUB_WORKSPACE/.github/workflows/AppRun ${{runner.workspace}}/build/pageedit.AppDir/
        LDAI_UPDATE_INFORMATION="gh-releases-zsync|Sigil-Ebook|PageEdit|latest|PageEdit-*x86_64.AppImage.zsync" \
        ./linuxdeploy-plugin-appimage-x86_64.AppImage --appdir=pageedit.AppDir
     
    - name: Upload Artifact
      if: contains(github.event.head_commit.message, '[deploy]') || startswith( github.ref, 'refs/tags/') || github.event_name == 'workflow_dispatch'
      uses: actions/upload-artifact@v4
      with:
        name: PageEdit-AppImage-artifact
        path: ${{runner.workspace}}/build/PageEdit*.AppImage*
        retention-days: 5
