name: Build and Release on Tag

on:
  push:
    tags:
      - v*

jobs:
  build-linux:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v4.1.7

    - name: Create Build Environment
      run: |
        sudo apt-get update
        sudo apt-get install -y build-essential libgl1-mesa-dev and libglvnd-dev cmake qt6-base-dev
        mkdir build
  
    - name: Configure CMake
      working-directory: ${{github.workspace}}/build
      shell: bash
      run: |
        cmake -DCMAKE_BUILD_TYPE=Release ..

    - name: Build
      working-directory: ${{github.workspace}}/build
      shell: bash
      run: |
        make -j4
        
    - name: Archive Artifact
      working-directory: ${{github.workspace}}/build
      shell: bash
      run: |
        tar -czvf NDSFactory-${{ github.ref_name }}-Linux-x64.tar.gz NDSFactory ../README.md ../LICENSE
    
    - name: Release on GitHub
      uses: softprops/action-gh-release@v2.0.8
      if: startsWith(github.ref, 'refs/tags/')
      with:
        files: "build/NDSFactory-${{ github.ref_name }}-Linux-x64.tar.gz"
        
  build-macos:
    runs-on: macos-latest

    steps:
    - uses: actions/checkout@v4.1.7
    
    - name: Install Qt
      uses: jurplel/install-qt-action@v4
      with:
        version: '6.7.2'
      
    - name: Install CMake
      uses: lukka/get-cmake@latest

    - name: Create Build Environment
      run: |
        mkdir build
        
    - name: Configure CMake
      working-directory: ${{github.workspace}}/build
      shell: bash
      run: |
        cmake -DCMAKE_BUILD_TYPE=Release ..

    - name: Build
      working-directory: ${{github.workspace}}/build
      shell: bash
      run: |
        make -j4
        macdeployqt NDSFactory.app

    - name: Archive Artifact
      working-directory: ${{github.workspace}}/build
      shell: bash
      run: |
        zip -r NDSFactory-${{ github.ref_name }}-macOS.zip NDSFactory.app ../README.md ../LICENSE

    - name: Release on GitHub
      uses: softprops/action-gh-release@v2.0.8
      if: startsWith(github.ref, 'refs/tags/')
      with:
        files: "build/NDSFactory-${{ github.ref_name }}-macOS.zip"

  build-windows:
    runs-on: windows-latest

    steps:
    - uses: actions/checkout@v4.1.7
    
    - name: Install Qt
      uses: jurplel/install-qt-action@v4
      with:
        version: '6.7.2'
      
    - name: Install CMake
      uses: lukka/get-cmake@latest

    - name: Create Build Environment
      run: |
        mkdir build
        
    - name: Configure CMake
      working-directory: ${{github.workspace}}/build
      run: |
        cmake ..

    - name: Build
      working-directory: ${{github.workspace}}/build
      run: |
        cmake --build . -j4 --config Release
        mkdir .\Release\prod
        move .\Release\NDSFactory.exe .\Release\prod
        windeployqt .\Release\prod\NDSFactory.exe
        
    - name: Archive Artifact
      working-directory: ${{github.workspace}}/build/Release
      run: |
        xcopy ..\..\README.md .\prod
        xcopy ..\..\LICENSE .\prod
        powershell "Compress-Archive -Path .\prod\* -DestinationPath .\NDSFactory-${{ github.ref_name }}-Windows-x64.zip"

    - name: Release on GitHub
      uses: softprops/action-gh-release@v2.0.8
      if: startsWith(github.ref, 'refs/tags/')
      with:
        files: "build/Release/NDSFactory-${{ github.ref_name }}-Windows-x64.zip"