name: CI

on:
  schedule:
    - cron: "0 20 * * *"
  workflow_dispatch:

jobs:
  build-linux:
    runs-on: ubuntu-latest
    
    steps:
    - name: Checkout code
      uses: actions/checkout@v4
      
    - name: Install dependencies
      run: |
        sudo apt-get update
        sudo apt-get install -y \
          build-essential \
          ruby \
          qmake6 \
          libqt6core6 \
          libqt6gui6 \
          libqt6svg6-dev \
          libqt6core5compat6-dev \
          zlib1g-dev \
          libgl1-mesa-dev \
          libssl-dev \
          qt6-base-dev \
          qt6-svg-dev
          
    - name: Prepare environment
      run: ruby prepare.rb
      
    - name: Build libfiletype
      run: |
        cd filetype
        bash build-gcc.sh
        ls -la lib
        cd ..
        
    - name: Build Guitar
      run: |
        rm -fr _build
        mkdir _build
        cd _build
        qmake6 "CONFIG+=release" ../Guitar.pro
        make -j$(nproc)
        
    - name: Test build
      run: |
        ls -la _bin/
        file _bin/Guitar
        
    - name: Upload artifacts
      uses: actions/upload-artifact@v4
      with:
        name: guitar-linux-amd64
        path: _bin/Guitar

  build-macos:
    runs-on: macos-latest
    
    steps:
    - name: Checkout code
      uses: actions/checkout@v4
      
    - name: Install Qt6
      run: |
        brew install qt@6 ruby
        echo "/opt/homebrew/opt/qt@6/bin" >> $GITHUB_PATH
        
    - name: Prepare environment
      run: ruby prepare.rb
      
    - name: Build libfiletype
      run: |
        brew install autoconf automake libtool
        cd filetype
        bash build-gcc.sh
        ls -la lib
        cd ..
        
    - name: Build Guitar
      run: |
        rm -fr _build
        mkdir _build
        cd _build
        qmake "CONFIG+=release" ../Guitar.pro
        make -j$(sysctl -n hw.ncpu)
        
    - name: Test build
      run: |
        ls -la _bin/
        cd _bin
        zip -r Guitar.zip Guitar.app
        
    - name: Upload artifacts
      uses: actions/upload-artifact@v4
      with:
        name: guitar-macos
        path: _bin/Guitar.zip

  build-windows:
    runs-on: windows-latest
    
    steps:
    - name: Checkout code
      uses: actions/checkout@v4
      
    - name: Install Qt6
      uses: jurplel/install-qt-action@v4
      with:
        version: '6.8.3'
        host: 'windows'
        target: 'desktop'
        arch: 'win64_msvc2022_64'
        
    - name: Setup MSVC 2022
      uses: microsoft/setup-msbuild@v2
      
    - name: Setup Visual Studio 2022 environment
      uses: ilammy/msvc-dev-cmd@v1
      with:
        arch: x64
        vsversion: 2022
        
    - name: Remove Git tools from PATH (to avoid conflicts)
      run: |
        echo Current PATH:
        echo %PATH%
        echo.
        echo Removing Git bin directories from PATH to avoid tool conflicts...
        set "PATH=%PATH:C:\Program Files\Git\usr\bin;=%"
        set "PATH=%PATH:C:\Program Files\Git\bin;=%"
        echo Updated PATH:
        echo %PATH%
        echo PATH > path_update.bat
        echo set "PATH=%PATH%" >> path_update.bat
      shell: cmd
        
    - name: Install vcpkg, zlib and openssl
      run: |
        echo Installing vcpkg...
        if exist "C:\vcpkg" (
          echo vcpkg directory already exists, removing it...
          rmdir /s /q "C:\vcpkg"
        )
        git clone https://github.com/Microsoft/vcpkg.git C:\vcpkg
        if errorlevel 1 (
          echo Failed to clone vcpkg
          exit /b 1
        )
        echo Running vcpkg bootstrap...
        call C:\vcpkg\bootstrap-vcpkg.bat
        if errorlevel 1 (
          echo Failed to bootstrap vcpkg
          exit /b 1
        )
        echo Installing zlib via vcpkg...
        C:\vcpkg\vcpkg.exe install zlib:x64-windows
        if errorlevel 1 (
          echo Failed to install zlib
          exit /b 1
        )
        echo zlib installation completed successfully
        echo Installing openssl via vcpkg...
        C:\vcpkg\vcpkg.exe install openssl:x64-windows
        if errorlevel 1 (
          echo Failed to install openssl
          exit /b 1
        )
        echo openssl installation completed successfully
      shell: cmd
      
    - name: Verify environment
      run: |
        call path_update.bat
        echo Checking environment variables after PATH cleanup...
        echo VCINSTALLDIR: %VCINSTALLDIR%
        echo VisualStudioVersion: %VisualStudioVersion%
        echo QMAKESPEC: %QMAKESPEC%
        echo.
        echo Setting vcpkg environment...
        set "VCPKG_ROOT=C:\vcpkg"
        echo VCPKG_ROOT: %VCPKG_ROOT%
        echo.
        echo Checking tool locations:
        where nmake
        where qmake
        where cl
        echo.
        echo Checking link.exe (should be MSVC linker, not Git ln):
        where link
        link 2>nul || echo link.exe found
        echo.
        echo Qt and qmake information:
        qmake -query QT_VERSION
        qmake -query QMAKE_SPEC
        echo.
        echo Checking vcpkg zlib installation:
        dir "C:\vcpkg\installed\x64-windows\include" | findstr zlib || echo zlib headers not found
        dir "C:\vcpkg\installed\x64-windows\lib" | findstr zlib || echo zlib libraries not found
      shell: cmd
        
    - name: Setup Ruby
      uses: ruby/setup-ruby@v1
      with:
        ruby-version: '3.1'
        
    - name: Prepare environment
      run: ruby prepare.rb
      shell: bash
      
    - name: Build libfiletype
      run: |
        call path_update.bat
        set "VCPKG_ROOT=C:\vcpkg"
        if exist "_build" rmdir /s /q "_build"
        mkdir "_build"
        cd "_build"
        qmake -spec win32-msvc "CONFIG+=release" ../filetype/libfiletype.pro
        ..\misc\jom.exe
        cd ..
        if exist "_build" rmdir /s /q "_build"
        mkdir "_build"
        cd "_build"
        qmake -spec win32-msvc "CONFIG+=release" ../filetype/liboniguruma.pro
        ..\misc\jom.exe
        cd ..
        if exist "_build" rmdir /s /q "_build"
        mkdir "_build"
        cd "_build"
        qmake -spec win32-msvc "CONFIG+=release" ../filetype/libfile.pro
        ..\misc\jom.exe
        cd ..
      shell: cmd
      
    - name: Build Guitar
      run: |
        call path_update.bat
        set "VCPKG_ROOT=C:\vcpkg"
        if exist "_build" rmdir /s /q "_build"
        mkdir "_build"
        cd "_build"
        qmake -spec win32-msvc "CONFIG+=release" ../Guitar.pro
        ..\misc\jom.exe
        cd ..
      shell: cmd
      
    - name: Test build
      run: |
        dir "_bin\"
        if exist "_bin\Guitar.exe" (
          echo Guitar.exe found
        ) else (
          echo Guitar.exe not found
          exit /b 1
        )
      shell: cmd
      
    - name: Upload artifacts
      uses: actions/upload-artifact@v4
      with:
        name: guitar-windows-msvc2022-x64
        path: _bin/Guitar.exe
