name: Build & Package Qt5

on:
  push:
    branches: [ main, 'develop' ]
    paths-ignore:
      - 'resource/lfs/locale/**'
      - '**.md'
  pull_request:
    branches: [ main, 'develop' ]
    paths-ignore:
      - 'resource/lfs/locale/**'
      - '**.md'

env:
  # Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.)
  BUILD_TYPE: Release

jobs:
  build:
    strategy:
      matrix:
        os: [ 'ubuntu-20.04', 'windows-2019' ]
    runs-on: ${{ matrix.os }}
    continue-on-error: true
    steps:
      - name: Set git to use LF(Windows) or CRLF(MacOS) line endings
        run: |
          git config --global core.autocrlf false
          git config --global core.eol lf
        if: matrix.os == 'windows-2019'

      - uses: actions/checkout@v4
        with:
          lfs: 'false'
          submodules: recursive

      - name: Get Short SHA of Commit
        id: vars
        run: echo "sha_short=$(git rev-parse --short HEAD)" >> $GITHUB_ENV

      - name: Set up MinGW (Windows)
        uses: msys2/setup-msys2@v2
        with:
          install: git msys2-devel base-devel binutils mingw-w64-x86_64-toolchain
          release: false
        if: matrix.os == 'windows-2019'

      - name: Set up Dependence (Windows)
        shell: msys2 {0}
        run: |
          pacman --noconfirm -S --needed mingw-w64-x86_64-gcc mingw-w64-x86_64-make mingw-w64-x86_64-cmake autoconf 
          pacman --noconfirm -S --needed make texinfo automake 
          pacman --noconfirm -S --needed mingw-w64-x86_64-qt5 mingw-w64-x86_64-angleproject libintl msys2-runtime-devel gettext-devel
          pacman --noconfirm -S --needed mingw-w64-x86_64-ninja mingw-w64-x86_64-gnupg mingw-w64-x86_64-libarchive
        if: matrix.os == 'windows-2019'

      - name: Install Dependence (Linux)
        run: |
          sudo apt-get update
          sudo apt-get -y install build-essential binutils git autoconf automake gettext texinfo
          sudo apt-get -y install gcc g++ ninja-build
          sudo apt-get -y install libarchive-dev libssl-dev
          sudo apt-get -y install gpgsm libxcb-xinerama0 libxcb-icccm4-dev libcups2-dev libdrm-dev libegl1-mesa-dev
          sudo apt-get -y install libgcrypt20-dev libnss3-dev libpci-dev libpulse-dev libudev-dev libxtst-dev gyp
          sudo apt-get -y install libglu1-mesa-dev libfontconfig1-dev libx11-xcb-dev libxcb-image0
          sudo apt-get -y install libglu1-mesa-dev libfontconfig1-dev libx11-xcb-dev libxcb-* libxkbcommon-x11-0
        if: matrix.os == 'ubuntu-20.04'
    
      - name: Install Qt5
        uses: jurplel/install-qt-action@v3
        with:
          version: '5.15.2'
          cache: 'true'
        if: matrix.os == 'ubuntu-20.04'

      - name: Build gpg-error (Windows)
        shell: msys2 {0}
        run: |
          git clone --depth 1 --branch libgpg-error-1.47 git://git.gnupg.org/libgpg-error.git ${{github.workspace}}/third_party/libgpg-error
          cd  ${{github.workspace}}/third_party/libgpg-error
          ./autogen.sh
          ./configure --enable-maintainer-mode && make -j4
          make install
          cd ${{github.workspace}}
        if: matrix.os == 'windows-2019'

      - name: Build gpg-error (Linux)
        run: |
          git clone --depth 1 --branch libgpg-error-1.47 git://git.gnupg.org/libgpg-error.git ${{github.workspace}}/third_party/libgpg-error
          cd  ${{github.workspace}}/third_party/libgpg-error
          ./autogen.sh
          ./configure --enable-maintainer-mode && make -j4
          sudo make install
          cd ${{github.workspace}}
        if: matrix.os == 'ubuntu-20.04'

      - name: Build assuan (Windows)
        shell: msys2 {0}
        run: |
          git clone --depth 1 --branch libassuan-2.5.6 git://git.gnupg.org/libassuan.git ${{github.workspace}}/third_party/libassuan
          cd ${{github.workspace}}/third_party/libassuan
          ./autogen.sh
          ./configure --enable-maintainer-mode && make -j4
          make install
          cd ${{github.workspace}}
        if: matrix.os == 'windows-2019'
      
      - name: Build assuan (Linux)
        run: |
          git clone --depth 1 --branch libassuan-2.5.6 git://git.gnupg.org/libassuan.git ${{github.workspace}}/third_party/libassuan
          cd ${{github.workspace}}/third_party/libassuan
          ./autogen.sh
          ./configure --enable-maintainer-mode && make -j4
          sudo make install
          cd ${{github.workspace}}
        if: matrix.os == 'ubuntu-20.04'

      - name: Build GpgME (Windows)
        shell: msys2 {0}
        run: |
          git clone --depth 1 --branch fix/1.18.0 https://git.bktus.com/GpgFrontend/gpgme.git ${{github.workspace}}/third_party/gpgme
          cd ${{github.workspace}}/third_party/gpgme
          ./autogen.sh
          ./configure --enable-maintainer-mode --enable-languages=cpp --disable-gpg-test && make -j4
          make install
          cd ${{github.workspace}}
        if: matrix.os == 'windows-2019'
      
      - name: Build GpgME (Linux)
        run: |
          git clone --depth 1 --branch gpgme-1.22.0 git://git.gnupg.org/gpgme.git ${{github.workspace}}/third_party/gpgme
          cd ${{github.workspace}}/third_party/gpgme
          ./autogen.sh
          ./configure --enable-maintainer-mode --enable-languages=cpp && make -j4
          sudo make install
          cd ${{github.workspace}}
        if: matrix.os == 'ubuntu-20.04'

      - name: Build mimalloc (Linux)
        run: |
          cd ${{github.workspace}}/third_party/mimalloc
          mkdir build && cd build
          cmake -G Ninja -DMI_SECURE=ON ..
          ninja
          sudo ninja install
        if: matrix.os == 'ubuntu-20.04'

      - name: Build mimalloc (Windows)
        shell: msys2 {0}
        run: |
          cd ${{github.workspace}}/third_party/mimalloc
          mkdir build && cd build
          cmake -G Ninja -DMI_SECURE=ON ..
          ninja
          sudo ninja install
        if: matrix.os == 'windows-2019'

      - name: Build googletest (Linux)
        run: |
          cd ${{github.workspace}}/third_party/googletest
          mkdir build && cd build
          cmake -G Ninja ..
          ninja
          sudo ninja install
        if: matrix.os == 'ubuntu-20.04'

      - name: Configure CMake & Build Binary(Windows)
        shell: msys2 {0}
        run: |
          cd $(echo "/${{github.workspace}}"  | sed 's/\\/\//g' | sed 's/://')
          mkdir build && cd build
          cmake -G Ninja -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DGPGFRONTEND_BUILD_TYPE_STABLE=ON -DGPGFRONTEND_QT5_BUILD=ON ..
          cmake --build . --config ${{env.BUILD_TYPE}}  -- -j 4
        if: matrix.os == 'windows-2019'

      - name: Build GpgFrontend (Linux)
        # Build your GpgFrontend with the given configuration
        run: |
          cmake -B ${{github.workspace}}/build -G Ninja -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DGPGFRONTEND_QT5_BUILD=ON
          cmake --build ${{github.workspace}}/build --config {{$env.BUILD_TYPE}} -- -v
        if: matrix.os == 'ubuntu-20.04'

      - name: Package App Image (Linux)
        run: |
          mkdir ${{github.workspace}}/build/final-artifact
          cd ${{github.workspace}}/build/final-artifact
          wget -c -nv https://github.com/probonopd/linuxdeployqt/releases/download/continuous/linuxdeployqt-continuous-x86_64.AppImage
          chmod u+x linuxdeployqt-continuous-x86_64.AppImage
          ./linuxdeployqt-continuous-x86_64.AppImage ${{github.workspace}}/build/artifacts/AppDir/usr/share/applications/*.desktop -no-translations -extra-plugins=iconengines -appimage
          echo "BUILD_TYPE_LOWER=${BUILD_TYPE,,}" >> ${GITHUB_ENV}
          echo "SHORT_SHA=`echo ${GITHUB_SHA} | cut -c1-8`" >> ${GITHUB_ENV}
        if: matrix.os == 'ubuntu-20.04'


      - name: Copy Modules & Package (Windows)
        shell: msys2 {0}
        run: |
          cd $(echo "/${{github.workspace}}"  | sed 's/\\/\//g' | sed 's/://')
          cd build
          windeployqt --no-translations --force ./artifacts/GpgFrontend.exe
          echo "BUILD_TYPE_LOWER=${BUILD_TYPE,,}" >> ${GITHUB_ENV}
          echo "SHORT_SHA=`echo ${GITHUB_SHA} | cut -c1-8`" >> ${GITHUB_ENV}
        if: matrix.os == 'windows-2019'
      
      - name: Upload Artifact(Linux)
        uses: actions/upload-artifact@v4
        with:
          name: gpgfrontend-${{matrix.os}}-${{env.BUILD_TYPE_LOWER}}-${{ env.SHORT_SHA }}
          path: ${{github.workspace}}/build/final-artifact/Gpg_Frontend*.AppImage*
        if: matrix.os == 'ubuntu-20.04'

      - name: Upload Artifact(Windows)
        uses: actions/upload-artifact@v4
        with:
          name: gpgfrontend-${{matrix.os}}--${{env.BUILD_TYPE_LOWER}}-${{ env.SHORT_SHA }}
          path: ${{github.workspace}}/build/artifacts/*
        if: matrix.os == 'windows-2019'
