name: Debug Build & Package

on:
  push:
    branches: [ 'develop-**', 'dev/**' ]
    paths-ignore:
      - 'resource/locale/template/**'
      - 'docs/**'
      - '**.md'
  pull_request:
    branches: [ 'develop-**', 'dev/**' ]
    paths-ignore:
      - 'resource/locale/template/**'
      - 'docs/**'
      - '**.md'

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

jobs:
  build:
    strategy:
      matrix:
        os: [ 'ubuntu-latest', 'macos-latest', 'windows-latest' ]
    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-latest' ||  matrix.os == 'macos-11' || matrix.os == 'macos-12'

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

      - 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 libconfig++-dev libboost-all-dev libssl-dev
          sudo apt-get -y install gpg
        if: matrix.os == 'ubuntu-latest'

      - name: Git Sumbodule Update
        run: |
          git submodule update --init --recursive

      - name: Install Dependence (macOS)
        run: |
          brew install cmake autoconf automake texinfo gettext openssl@3
          brew install boost ninja libarchive libconfig gpgme
          brew unlink gettext && brew link --force gettext
          brew link openssl@3 --force
        if:  matrix.os == 'macos-11' || matrix.os == 'macos-12'

      - name: Build gpg-error (Linux)
        run: |
          cd  ${{github.workspace}}/third_party/libgpg-error
          ./autogen.sh
          ./configure --enable-maintainer-mode && make -j2
          sudo make install
          cd ${{github.workspace}}
        if: matrix.os == 'ubuntu-latest'

      - name: Build assuan (Linux)
        run: |
          cd ${{github.workspace}}/third_party/libassuan
          ./autogen.sh
          ./configure --enable-maintainer-mode && make -j2
          sudo make install
          cd ${{github.workspace}}
        if: matrix.os == 'ubuntu-latest'

      - name: Build GpgME (Linux)
        run: |
          cd  ${{github.workspace}}/third_party/gpgme
          ./autogen.sh
          ./configure --enable-maintainer-mode --enable-languages=cpp && make -j2
          sudo make install
          cd ${{github.workspace}}
        if: matrix.os == 'ubuntu-latest'

      - name: Install Qt
        uses: jurplel/install-qt-action@v3
        with:
          version: '6.4.3'
          cache: 'true'
          modules: 'qt5compat'
        if: matrix.os == 'ubuntu-latest' || matrix.os == 'macos-11' || matrix.os == 'macos-12'

      - 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-latest'

      - 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 mingw-w64-x86_64-libconfig mingw-w64-x86_64-boost automake
          pacman --noconfirm -S --needed mingw-w64-x86_64-qt6 libintl msys2-runtime-devel gettext-devel
          pacman --noconfirm -S --needed mingw-w64-x86_64-ninja mingw-w64-x86_64-gnupg  mingw-w64-x86_64-gpgme
          pacman --noconfirm -S --needed mingw-w64-x86_64-libarchive mingw-w64-x86_64-icu mingw-w64-x86_64-icu-debug-libs
        if: matrix.os == 'windows-latest'

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

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

      - 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_TEST_UI=ON ..
          # Build your program with the given configuration
          cmake --build . --config ${{env.BUILD_TYPE}}  -- -j 2
        if: matrix.os == 'windows-latest'

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

      - name: Upload Artifact(Linux)
        uses: actions/upload-artifact@master
        with:
          name: gpgfrontend-${{matrix.os}}-${{env.BUILD_TYPE}}-${{env.sha_short}}
          path: ${{github.workspace}}/build/release/*
        if: matrix.os == 'ubuntu-latest'

      - name: Upload Artifact(macOS)
        uses: actions/upload-artifact@master
        with:
          name: gpgfrontend-${{matrix.os}}-${{env.BUILD_TYPE}}-${{env.sha_short}}
          path: ${{github.workspace}}/build/release/*
        if:  matrix.os == 'macos-11' || matrix.os == 'macos-12'

      - name: Upload Artifact(Windows)
        uses: actions/upload-artifact@master
        with:
          name: gpgfrontend-${{matrix.os}}-${{env.BUILD_TYPE}}-${{env.sha_short}}
          path: ${{github.workspace}}/build/release/*
        if: matrix.os == 'windows-latest'
