name: Build
on: [push, pull_request]

jobs:
  linux-configure-gcc:
    name: GCC (Linux)
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
    - name: Install Dependencies
      run: |
        sudo apt update
        sudo apt install autoconf automake make gcc g++ qtbase5-dev qtbase5-dev-tools qttools5-dev-tools libgl1-mesa-dev
    - name: autogen.sh
      run: ./autogen.sh
    - name: configure
      run: CC=gcc CXX=g++ ./configure
    - name: make
      run: make
    - name: make install
      run: sudo make install

  linux-configure-clang:
    name: Clang (Linux)
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
    - name: Install Dependencies
      run: |
        sudo apt update
        sudo apt install autoconf automake make gcc g++ qtbase5-dev qtbase5-dev-tools qttools5-dev-tools libgl1-mesa-dev
    - name: autogen.sh
      run: ./autogen.sh
    - name: configure
      run: CC=clang CXX=clang++ ./configure
    - name: make
      run: make
    - name: make install
      run: sudo make install

  linux-make:
    name: Makefile.generic (Linux, GCC)
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
    - name: Install Dependencies
      run: |
        sudo apt update
        sudo apt install autoconf automake make gcc g++ qtbase5-dev qtbase5-dev-tools qttools5-dev-tools libgl1-mesa-dev
    - name: Setup
      run: cp config.h.generic config.h
    - name: make
      run: make -f Makefile.generic
    - name: make install
      run: sudo make -f Makefile.generic install

  linux-flatpak:
    name: Flatpak (Linux, GCC)
    # flatpak-builder fails to download glu on ubuntu-22.04
    # (glu specified in mm3d-flatpak.yml)
    # see https://github.com/flatpak/flatpak-builder/issues/468
    runs-on: ubuntu-20.04
    steps:
    - uses: actions/checkout@v4
      with:
        fetch-depth: 0 # This is needed for "git describe" to work.
    - name: Install Dependencies
      run: |
        sudo apt update
        sudo apt install flatpak flatpak-builder
        flatpak remote-add --user --if-not-exists flathub https://flathub.org/repo/flathub.flatpakrepo
    - name: Set version for the Flatpak
      run: |
        # if not a tag, add version
        if [ $(git describe) != $(git describe --abbrev=0) ]; then
          sed -i "s|<releases>|<releases>\n    <release version=\"$(git describe|sed 's/^v\([0-9]\)/\1/')\" date=\"$(git show -s --pretty=format:%ad --date=short)\" />|" desktop/moe.clover.mm3d.metainfo.xml
        fi
        cat desktop/moe.clover.mm3d.metainfo.xml
    - name: flatpak-builder
      run: flatpak-builder --user --install-deps-from=flathub --repo=flatpak-repo --state-dir=flatpak-state flatpak-build mm3d-flatpak.yaml
    - name: flatpak build-bundle
      run: flatpak build-bundle flatpak-repo moe.clover.mm3d-$(git describe|sed 's/^v\([0-9]\)/\1/').flatpak moe.clover.mm3d
    - name: flatpak install
      run: |
        flatpak install --user -y moe.clover.mm3d-$(git describe|sed 's/^v\([0-9]\)/\1/').flatpak
        flatpak list
    - uses: actions/upload-artifact@v4
      with:
        name: Flatpak Bundle
        path: ./*.flatpak

  macOS:
    name: macOS (x86_64)
    runs-on: macOS-13
    steps:
    - uses: actions/checkout@v4
      with:
        fetch-depth: 0 # This is needed for "git describe" to work.
    - name: Install Dependencies
      run: |
        brew install autoconf automake qt@5
    - name: Configure
      run: |
        qt_version=$(ls -1 /usr/local/Cellar/qt@5 | sort -rV | head -1)
        # Get version from line "QMAKE_MACOSX_DEPLOYMENT_TARGET = 10.13"
        macosx_version_min=$(grep QMAKE_MACOSX_DEPLOYMENT_TARGET /usr/local/Cellar/qt@5/${qt_version}/mkspecs/common/macx.conf | cut -d' ' -f3)
        echo "Qt version: ${qt_version}, minimum macOS version: ${macosx_version_min}"
        if [ -z "${qt_version}" -o -z "${macosx_version_min}" ]; then
          exit 1
        fi
        ./autogen.sh
        ./configure --with-Qt-dir=/usr/local/Cellar/qt@5/${qt_version} --with-macosx-version-min=${macosx_version_min}
    - name: make
      run: make
    - name: make appbundle
      run: make appbundle
    - name: Create DMG
      run: |
        mm3d_version=$(git describe|sed 's/^v\([0-9]\)/\1/')
        mm3d_longname="Maverick Model 3D ${mm3d_version}"
        # Sign .app
        rmdir "Maverick Model 3D.app/Contents/PlugIns/mm3d/1.3"
        codesign --deep --force --sign - "Maverick Model 3D.app"
        mkdir "Maverick Model 3D.app/Contents/PlugIns/mm3d/1.3"
        # Create .dmg
        mkdir "${mm3d_longname}"
        mv "Maverick Model 3D.app" "${mm3d_longname}/."
        hdiutil create -fs HFS+ -srcfolder "${mm3d_longname}" mm3d-${mm3d_version}.dmg
        # Sign .dmg
        codesign -s - mm3d-${mm3d_version}.dmg
    - uses: actions/upload-artifact@v4
      with:
        name: MacOS App (x86_64)
        path: ./*.dmg

  macOS_arm64:
    name: macOS (ARM64)
    runs-on: macOS-latest
    steps:
    - uses: actions/checkout@v4
      with:
        fetch-depth: 0 # This is needed for "git describe" to work.
    - name: Install Dependencies
      run: |
        brew install autoconf automake qt@5
    - name: Configure
      run: |
        qt_version=$(ls -1 /opt/homebrew/Cellar/qt@5 | sort -rV | head -1)
        # Get version from line "QMAKE_MACOSX_DEPLOYMENT_TARGET = 10.13"
        macosx_version_min=$(grep QMAKE_MACOSX_DEPLOYMENT_TARGET /opt/homebrew/Cellar/qt@5/${qt_version}/mkspecs/common/macx.conf | cut -d' ' -f3)
        echo "Qt version: ${qt_version}, minimum macOS version: ${macosx_version_min}"
        if [ -z "${qt_version}" -o -z "${macosx_version_min}" ]; then
          exit 1
        fi
        ./autogen.sh
        ./configure --with-Qt-dir=/opt/homebrew/Cellar/qt@5/${qt_version} --with-macosx-version-min=${macosx_version_min}
    - name: make
      run: make
    - name: make appbundle
      run: make appbundle
    - name: Create DMG
      run: |
        mm3d_version=$(git describe|sed 's/^v\([0-9]\)/\1/')
        mm3d_longname="Maverick Model 3D ${mm3d_version}"
        # Sign .app
        rmdir "Maverick Model 3D.app/Contents/PlugIns/mm3d/1.3"
        codesign --deep --force --sign - "Maverick Model 3D.app"
        mkdir "Maverick Model 3D.app/Contents/PlugIns/mm3d/1.3"
        # Create .dmg
        mkdir "${mm3d_longname}"
        mv "Maverick Model 3D.app" "${mm3d_longname}/."
        hdiutil create -fs HFS+ -srcfolder "${mm3d_longname}" mm3d-${mm3d_version}.dmg
        # Sign .dmg
        codesign -s - mm3d-${mm3d_version}.dmg
    - uses: actions/upload-artifact@v4
      with:
        name: MacOS App (ARM64)
        path: ./*.dmg
