name: Build
on:
  push:
  pull_request:
    types: [opened, synchronize, reopened]
  release:
    types: [published]

jobs:

  build-opensuse:
    name: Build openSUSE
    if: github.repository != 'strawberrymusicplayer/strawberry-private' && github.ref != 'refs/heads/l10n_master'
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        opensuse_version: [ 'tumbleweed', 'leap:15.6' ]
    container:
      image: opensuse/${{matrix.opensuse_version}}
    steps:
      - name: Refresh repositories
        run: zypper -n --gpg-auto-import-keys ref
      - name: Upgrade packages (Tumbleweed)
        if: matrix.opensuse_version == 'tumbleweed'
        run: zypper -n --gpg-auto-import-keys dup
      - name: Upgrade packages (Leap)
        if: matrix.opensuse_version != 'tumbleweed'
        run: zypper -n --gpg-auto-import-keys up
      - name: Install gcc (Tumbleweed)
        if: matrix.opensuse_version == 'tumbleweed'
        run: zypper -n --gpg-auto-import-keys in gcc gcc-c++
      - name: Install gcc (Leap)
        if: matrix.opensuse_version != 'tumbleweed'
        run: zypper -n --gpg-auto-import-keys in gcc14 gcc14-c++
      - name: Install packages
        run: >
            zypper -n --gpg-auto-import-keys in
            lsb-release
            rpm-build
            git
            tar
            make
            cmake
            openssh-clients
            glibc-devel
            libboost_headers-devel
            boost-devel
            glib2-devel
            glib2-tools
            alsa-devel
            libnotify-devel
            sqlite3-devel
            libpulse-devel
            gstreamer-devel
            gstreamer-plugins-base-devel
            taglib-devel
            libicu-devel
            libcdio-devel
            libgpod-devel
            libmtp-devel
            libchromaprint-devel
            fftw3-devel
            libebur128-devel
            desktop-file-utils
            update-desktop-files
            appstream-glib
            hicolor-icon-theme
            qt6-core-devel
            qt6-gui-devel
            qt6-gui-private-devel
            qt6-widgets-devel
            qt6-concurrent-devel
            qt6-network-devel
            qt6-sql-devel
            qt6-dbus-devel
            qt6-test-devel
            qt6-base-common-devel
            qt6-sql-sqlite
            qt6-linguist-devel
            gtest
            gmock
            sparsehash-devel
            rapidjson-devel
      - name: Install kdsingleapplication-qt6-devel
        if: matrix.opensuse_version == 'tumbleweed'
        run: zypper -n --gpg-auto-import-keys in kdsingleapplication-qt6-devel
      - name: Build and install KDSingleApplication
        if: matrix.opensuse_version == 'leap:15.6'
        env:
          CC: gcc-14
          CXX: g++-14
        run: |
          git clone --depth 1 --recurse-submodules https://github.com/KDAB/KDSingleApplication
          cd KDSingleApplication
          cmake -S . -B build -DCMAKE_BUILD_TYPE="Release" -DBUILD_SHARED_LIBS=OFF -DBUILD_STATIC_LIBS=ON -DKDSingleApplication_STATIC=ON
          cmake --build build --config Release --parallel 4
          cmake --install build
      - name: Checkout
        uses: actions/checkout@v5
        with:
          fetch-depth: 0
          submodules: recursive
      - name: Add safe git directory
        run: git config --global --add safe.directory ${GITHUB_WORKSPACE}
      - name: Create Build Environment
        run: cmake -E make_directory build
      - name: Configure CMake
        run: cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug -DBUILD_WERROR=ON
      - name: Create source tarball
        working-directory: build
        run: ../dist/scripts/maketarball.sh
      - name: Create RPM build sources directories
        run: mkdir -p ~/rpmbuild/SOURCES /usr/src/packages/SOURCES
      - name: Copy source tarball
        working-directory: build
        run: cp strawberry-*.tar.xz /usr/src/packages/SOURCES/
      - name: Build RPM (Tumbleweed)
        if: matrix.opensuse_version == 'tumbleweed'
        env:
          RPM_BUILD_NCPUS: 4
        working-directory: build
        run: rpmbuild -ba strawberry.spec
      - name: Build RPM (Leap)
        if: matrix.opensuse_version != 'tumbleweed'
        env:
          RPM_BUILD_NCPUS: 4
          CC: gcc-14
          CXX: g++-14
        working-directory: build
        run: rpmbuild -ba strawberry.spec
      - name: Set subdir
        id: set-subdir
        run: echo "subdir=$(echo ${{matrix.opensuse_version}} | sed 's/leap:/lp/g' | sed 's/\.//g')" > $GITHUB_OUTPUT
      - name: Upload source
        if: matrix.opensuse_version == 'tumbleweed'
        uses: actions/upload-artifact@v4
        with:
          name: source
          path: |
            /usr/src/packages/SOURCES/*.xz
      - name: Upload rpm
        if: matrix.opensuse_version != 'tumbleweed'
        uses: actions/upload-artifact@v4
        with:
          name: opensuse-${{steps.set-subdir.outputs.subdir}}
          path: |
            /usr/src/packages/SRPMS/*.rpm
            /usr/src/packages/RPMS/x86_64/*.rpm


  build-fedora:
    name: Build Fedora
    if: github.repository != 'strawberrymusicplayer/strawberry-private' && github.ref != 'refs/heads/l10n_master'
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        fedora_version: [ '41', '42', '43' ]
    container:
      image: fedora:${{matrix.fedora_version}}
    steps:
      - name: Update repositories
        run: dnf -y update
      - name: Upgrade packages
        run: dnf -y upgrade
      - name: Install dependencies
        run: >
            dnf -y install
            @development-tools
            lsb_release
            which
            git
            glibc
            gcc-c++
            rpmdevtools
            make
            cmake
            pkgconfig
            glib
            man
            tar
            openssh
            rsync
            boost-devel
            sqlite-devel
            alsa-lib-devel
            pulseaudio-libs-devel
            libnotify-devel
            libicu-devel
            qt6-qtbase-devel
            qt6-qtbase-private-devel
            qt6-qttools-devel
            gstreamer1-devel
            gstreamer1-plugins-base-devel
            taglib-devel
            libcdio-devel
            libgpod-devel
            libmtp-devel
            libchromaprint-devel
            libebur128-devel
            fftw-devel
            desktop-file-utils
            libappstream-glib
            hicolor-icon-theme
            kdsingleapplication-qt6-devel
            gtest-devel
            gmock-devel
            sparsehash-devel
            rapidjson-devel
      - name: Checkout
        uses: actions/checkout@v5
        with:
          fetch-depth: 0
          submodules: recursive
      - name: Add safe git directory
        run: git config --global --add safe.directory ${GITHUB_WORKSPACE}
      - name: Create Build Environment
        run: cmake -E make_directory build
      - name: Configure CMake
        run: cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug -DBUILD_WERROR=ON
      - name: Create source tarball
        working-directory: build
        run: ../dist/scripts/maketarball.sh
      - name: Create RPM build sources directories
        working-directory: build
        run: mkdir -p ~/rpmbuild/SOURCES /usr/src/packages/SOURCES
      - name: Copy source tarball
        working-directory: build
        run: cp strawberry-*.tar.xz ~/rpmbuild/SOURCES/
      - name: Build RPM
        env:
          RPM_BUILD_NCPUS: 4
        working-directory: build
        run: rpmbuild -ba strawberry.spec
      - name: Upload artifacts
        uses: actions/upload-artifact@v4
        with:
          name: fedora-${{matrix.fedora_version}}
          path: |
            /github/home/rpmbuild/SRPMS/*.rpm
            /github/home/rpmbuild/RPMS/x86_64/*.rpm


  build-openmandriva:
    name: Build OpenMandriva
    if: github.repository != 'strawberrymusicplayer/strawberry-private' && github.ref != 'refs/heads/l10n_master'
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        openmandriva_version: [ "cooker" ]
    container:
      image: openmandriva/${{matrix.openmandriva_version}}
    steps:
      - name: Update distro
        run: dnf distro-sync --assumeyes
      - name: Install dependencies
        run: >
          dnf install -y
          which
          gcc-c++
          git
          gnutar
          make
          cmake
          lsb-release
          rpmdevtools
          rpm-build
          glibc-devel
          boost-devel
          lib64glib2.0-devel
          lib64asound-devel
          lib64pulseaudio-devel
          lib64sqlite3-devel
          lib64gstreamer-devel
          lib64gst-plugins-base1.0-devel
          lib64taglib-devel
          lib64chromaprint-devel
          lib64ebur128-devel
          lib64fftw-devel
          lib64icu-devel
          lib64cdio-devel
          lib64gpod-devel
          lib64mtp-devel
          lib64Qt6Core-devel
          lib64Qt6Concurrent-devel
          lib64Qt6Network-devel
          lib64Qt6Sql-devel
          lib64Qt6DBus-devel
          lib64Qt6Gui-devel
          lib64Qt6Widgets-devel
          lib64Qt6Test-devel
          lib64kdsingleapplication-devel
          lib64xkbcommon-devel
          lib64gtest-devel
          lib64gmock-devel
          sparsehash-devel
          qt6-cmake
          qt6-qtbase-tools
          qt6-qttools-linguist
          desktop-file-utils
          appstream
          appstream-util
          hicolor-icon-theme
          rapidjson
      - name: Remove files
        run: rm -rf /usr/lib64/qt6/lib/cmake/Qt6Sql/{Qt6QMYSQL*,Qt6QODBCD*,Qt6QPSQL*,Qt6QIBase*}
      - name: Checkout
        uses: actions/checkout@v5
        with:
          fetch-depth: 0
          submodules: recursive
      - name: Add safe git directory
        run: git config --global --add safe.directory ${GITHUB_WORKSPACE}
      - name: Create Build Environment
        run: cmake -E make_directory build
      - name: Configure CMake
        run: cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug -DBUILD_WERROR=ON
      - name: Create source tarball
        working-directory: build
        run: ../dist/scripts/maketarball.sh
      - name: Create RPM build sources directories
        working-directory: build
        run: mkdir -p ~/rpmbuild/SOURCES /usr/src/packages/SOURCES
      - name: Copy source tarball
        working-directory: build
        run: cp strawberry-*.tar.xz ~/rpmbuild/SOURCES/
      - name: Build RPM
        env:
          RPM_BUILD_NCPUS: 4
        working-directory: build
        run: rpmbuild -ba strawberry.spec
      - name: Upload artifacts
        if: matrix.openmandriva_version != 'cooker'
        uses: actions/upload-artifact@v4
        with:
          name: openmandriva-${{matrix.openmandriva_version}}
          path: |
            /github/home/rpmbuild/SRPMS/*.rpm
            /github/home/rpmbuild/RPMS/x86_64/*.rpm


  build-mageia:
    name: Build Mageia
    if: github.repository != 'strawberrymusicplayer/strawberry-private' && github.ref != 'refs/heads/l10n_master'
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        mageia_version: [ '9' ]
    container:
      image: mageia:${{matrix.mageia_version}}
    steps:
      - name: Update repositories
        run: urpmi.update -a
      - name: Upgrade packages
        run: urpmi --auto --auto-update
      - name: Install dependencies
        run: >
          urpmi --auto --force urpmi-debuginfo-install
          git
          glibc
          gcc-c++
          binutils
          make
          cmake
          man
          tar
          rpmdevtools
          lib64boost-devel
          lib64sqlite3-devel
          lib64alsa2-devel
          lib64pulseaudio-devel
          lib64gstreamer1.0-devel
          lib64gstreamer-plugins-base1.0-devel
          lib64cdio-devel
          lib64gpod-devel
          lib64mtp-devel
          lib64taglib-devel
          lib64chromaprint-devel
          lib64ebur128-devel
          lib64icu-devel
          lib64fftw-devel
          lib64dbus-devel
          lib64appstream-devel
          lib64qt6core-devel
          lib64qt6gui-devel
          lib64qt6widgets-devel
          lib64qt6network-devel
          lib64qt6concurrent-devel
          lib64qt6sql-devel
          lib64qt6dbus-devel
          lib64qt6help-devel
          lib64qt6test-devel
          lib64sparsehash-devel
          lib64kdsingleapplication-devel
          desktop-file-utils
          appstream-util
          hicolor-icon-theme
          gtest
          rapidjson
      - name: Build and install KDSingleApplication
        if: matrix.mageia_version == '9'
        run: |
          git clone --depth 1 --recurse-submodules https://github.com/KDAB/KDSingleApplication
          cd KDSingleApplication
          cmake -S . -B build -DCMAKE_BUILD_TYPE="Release" -DBUILD_SHARED_LIBS=OFF -DBUILD_STATIC_LIBS=ON -DKDSingleApplication_STATIC=ON
          cmake --build build --config Release --parallel 4
          cmake --install build
      - name: Checkout
        uses: actions/checkout@v5
        with:
          fetch-depth: 0
          submodules: recursive
      - name: Add safe git directory
        run: git config --global --add safe.directory ${GITHUB_WORKSPACE}
      - name: Create Build Environment
        run: cmake -E make_directory build
      - name: Configure CMake
        run: cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug -DBUILD_WERROR=ON
      - name: Create source tarball
        working-directory: build
        run: ../dist/scripts/maketarball.sh
      - name: Create RPM build sources directories
        working-directory: build
        run: mkdir -p ~/rpmbuild/SOURCES /usr/src/packages/SOURCES
      - name: Copy source tarball
        working-directory: build
        run: cp strawberry-*.tar.xz ~/rpmbuild/SOURCES/
      - name: Build RPM
        env:
          RPM_BUILD_NCPUS: 4
        working-directory: build
        run: rpmbuild -ba strawberry.spec
      - name: Upload artifacts
        uses: actions/upload-artifact@v4
        with:
          name: mageia-${{matrix.mageia_version}}
          path: |
            /github/home/rpmbuild/SRPMS/*.rpm
            /github/home/rpmbuild/RPMS/x86_64/*.rpm


  build-debian:
    name: Build Debian
    if: github.repository != 'strawberrymusicplayer/strawberry-private' && github.ref != 'refs/heads/l10n_master'
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        debian_version: [ 'bookworm', 'trixie' ]
    container:
      image: debian:${{matrix.debian_version}}
    steps:
      - name: Update packages
        run: apt update -y
      - name: Upgrade packages
        run: apt upgrade -y
      - name: Install packages
        env:
          DEBIAN_FRONTEND: noninteractive
        run: >
            apt install -y
            build-essential
            dh-make
            git
            make
            cmake
            gcc
            g++
            pkg-config
            fakeroot
            lsb-release
            dpkg-dev
            libglib2.0-dev
            libboost-dev
            libsqlite3-dev
            libasound2-dev
            libpulse-dev
            libtag1-dev
            libicu-dev
            libgstreamer1.0-dev
            libgstreamer-plugins-base1.0-dev
            gstreamer1.0-alsa
            gstreamer1.0-pulseaudio
            libchromaprint-dev
            libebur128-dev
            libfftw3-dev
            libcdio-dev
            libmtp-dev
            libgpod-dev
            libxkbcommon-dev
            libsparsehash-dev
            qt6-base-dev
            qt6-base-private-dev
            qt6-base-dev-tools
            qt6-tools-dev
            qt6-tools-dev-tools
            qt6-l10n-tools
            rapidjson-dev
      - name: Build and install KDSingleApplication
        run: |
          git clone --depth 1 --recurse-submodules https://github.com/KDAB/KDSingleApplication
          cd KDSingleApplication
          cmake -S . -B build -DCMAKE_BUILD_TYPE="Release" -DBUILD_SHARED_LIBS=OFF -DBUILD_STATIC_LIBS=ON -DKDSingleApplication_STATIC=ON
          cmake --build build --config Release --parallel 4
          cmake --install build
      - name: Checkout
        uses: actions/checkout@v5
        with:
          fetch-depth: 0
          submodules: recursive
      - name: Add safe git directory
        run: git config --global --add safe.directory ${GITHUB_WORKSPACE}
      - name: Create Build Environment
        run: cmake -E make_directory build
      - name: Configure CMake
        run: cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX=/usr -DBUILD_WERROR=ON
      - name: Delete build directory
        run: rm -rf build
      - name: make deb
        run: dpkg-buildpackage -b -d -uc -us -nc -j4
      - name: Copy deb
        run: cp ../*.deb .
      - name: Upload artifacts
        uses: actions/upload-artifact@v4
        with:
          name: debian-${{matrix.debian_version}}
          path: |
            *.deb


  build-ubuntu:
    name: Build Ubuntu
    if: github.repository != 'strawberrymusicplayer/strawberry-private' && github.ref != 'refs/heads/l10n_master'
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        ubuntu_version: [ 'noble', 'plucky' ]
    container:
      image: ubuntu:${{matrix.ubuntu_version}}
    steps:
      - name: Update packages
        run: apt update -y
      - name: Upgrade packages
        run: apt upgrade -y
      - name: Install packages
        env:
          DEBIAN_FRONTEND: noninteractive
        run: >
            apt install -y
            build-essential
            dh-make
            git
            make
            cmake
            pkg-config
            gcc
            g++
            fakeroot
            wget
            curl
            lsb-release
            dpkg-dev
            libglib2.0-dev
            libboost-dev
            libsqlite3-dev
            libasound2-dev
            libpulse-dev
            libtag1-dev
            libicu-dev
            libgstreamer1.0-dev
            libgstreamer-plugins-base1.0-dev
            libgstreamer-plugins-good1.0-dev
            gstreamer1.0-alsa
            gstreamer1.0-pulseaudio
            libchromaprint-dev
            libebur128-dev
            libfftw3-dev
            libcdio-dev
            libmtp-dev
            libgpod-dev
            libxkbcommon-dev
            libsparsehash-dev
            qt6-base-dev
            qt6-base-private-dev
            qt6-base-dev-tools
            qt6-tools-dev
            qt6-tools-dev-tools
            qt6-l10n-tools
            rapidjson-dev
      - name: Build and install KDSingleApplication
        run: |
          git clone --depth 1 --recurse-submodules https://github.com/KDAB/KDSingleApplication
          cd KDSingleApplication
          cmake -S . -B build -DCMAKE_BUILD_TYPE="Release" -DBUILD_SHARED_LIBS=OFF -DBUILD_STATIC_LIBS=ON -DKDSingleApplication_STATIC=ON
          cmake --build build --config Release --parallel 4
          cmake --install build
      - name: Checkout
        uses: actions/checkout@v5
        with:
          fetch-depth: 0
          submodules: recursive
      - name: Add safe git directory
        run: git config --global --add safe.directory ${GITHUB_WORKSPACE}
      - name: Create Build Environment
        run: cmake -E make_directory build
      - name: Configure CMake
        run: cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX=/usr -DBUILD_WERROR=ON
      - name: Delete build directory
        run: rm -rf build
      - name: make deb
        run: dpkg-buildpackage -b -d -uc -us -nc -j4
      - name: Copy deb
        run: cp ../*.deb ../*.ddeb .
      - name: Upload artifacts
        uses: actions/upload-artifact@v4
        with:
          name: ubuntu-${{matrix.ubuntu_version}}
          path: |
            *.deb
            *.ddeb


  upload-ubuntu-ppa:
    name: Upload Ubuntu PPA
    if: github.repository == 'strawberrymusicplayer/strawberry' && github.event.pull_request.head.repo.fork == false && (github.event_name == 'release' || (github.event_name == 'push' && (github.ref == 'refs/heads/master' || github.ref == 'refs/heads/ci' || github.ref == 'refs/heads/1.1')))
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        ubuntu_version: [ 'noble', 'plucky' ]
    container:
      image: ubuntu:${{matrix.ubuntu_version}}
    steps:
      - name: Update packages
        run: apt update -y
      - name: Upgrade packages
        run: apt upgrade -y
      - name: Install packages
        env:
          DEBIAN_FRONTEND: noninteractive
        run: >
            apt install -y
            git
            build-essential
            dh-make
            make
            cmake
            pkg-config
            gcc
            g++
            fakeroot
            lsb-release
            gpg
            dput
            dpkg-dev
            libglib2.0-dev
            libboost-dev
            libsqlite3-dev
            libasound2-dev
            libpulse-dev
            libtag1-dev
            libicu-dev
            libgstreamer1.0-dev
            libgstreamer-plugins-base1.0-dev
            libgstreamer-plugins-good1.0-dev
            libchromaprint-dev
            libebur128-dev
            libfftw3-dev
            libcdio-dev
            libmtp-dev
            libgpod-dev
            libxkbcommon-dev
            libsparsehash-dev
            qt6-base-dev
            qt6-base-private-dev
            qt6-base-dev-tools
            qt6-tools-dev
            qt6-tools-dev-tools
            qt6-l10n-tools
            gstreamer1.0-alsa
            gstreamer1.0-pulseaudio
            libkdsingleapplication-qt6-dev
            rapidjson-dev
      - name: Install keyboxd
        if: matrix.ubuntu_version == 'noble'
        env:
          DEBIAN_FRONTEND: noninteractive
        run: apt install -y keyboxd
      - name: Checkout
        uses: actions/checkout@v5
        with:
          fetch-depth: 0
          submodules: recursive
      - name: Add safe git directory
        run: git config --global --add safe.directory ${GITHUB_WORKSPACE}
      - name: Modify required KDSingleApplication version
        run: |
          sed -i 's/^find_package(KDSingleApplication-qt\${QT_VERSION_MAJOR} 1.1.0 REQUIRED)$/find_package(KDSingleApplication-qt\${QT_VERSION_MAJOR} 1.0.0 REQUIRED)/g' CMakeLists.txt
          sed -i 's/, KDSingleApplication::Option::IncludeUsernameInSocketName);$/);/g' src/main.cpp
      - name: Create Build Environment
        run: cmake -E make_directory build
      - name: Configure CMake
        run: cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX=/usr -DBUILD_WERROR=ON
      - name: Delete build directory
        run: rm -rf build
      - name: Import Ubuntu PPA GPG private key
        uses: crazy-max/ghaction-import-gpg@v6
        with:
          gpg_private_key: ${{secrets.UBUNTU_PPA_GPG_PRIVATE_KEY}}
      - name: dpkg-buildpackage
        run: dpkg-buildpackage -S -d -k573D197B5EA20EDF
      - name: Upload Unstable PPA
        if: github.event_name == 'push'
        run: dput ppa:jonaski/strawberry-unstable ../*_source.changes
      - name: Upload Stable PPA
        if: github.event_name == 'release'
        run: dput ppa:jonaski/strawberry ../*_source.changes


  build-freebsd:
    name: Build FreeBSD
    if: github.repository != 'strawberrymusicplayer/strawberry-private' && github.ref != 'refs/heads/l10n_master'
    runs-on: ubuntu-latest
    steps:
    - name: Checkout
      uses: actions/checkout@v5
      with:
        fetch-depth: 0
        submodules: recursive
    - name: Build FreeBSD
      id: build-freebsd
      uses: vmactions/freebsd-vm@v1.2.3
      with:
        usesh: true
        mem: 4096
        prepare: pkg install -y git cmake pkgconf boost-libs alsa-lib glib qt6-base qt6-tools sqlite gstreamer1 gstreamer1-plugins chromaprint libebur128 taglib libcdio libmtp gdk-pixbuf2 libgpod fftw3 icu kdsingleapplication googletest pulseaudio sparsehash rapidjson
        run: |
          set -e
          git config --global --add safe.directory ${GITHUB_WORKSPACE}
          cmake -E make_directory build
          cmake -S . -B build -DCMAKE_BUILD_TYPE="Debug"
          cmake --build build --config Debug --parallel 4


  build-openbsd:
    name: Build OpenBSD
    if: github.repository != 'strawberrymusicplayer/strawberry-private' && github.ref != 'refs/heads/l10n_master'
    runs-on: ubuntu-latest
    steps:
    - name: Checkout
      uses: actions/checkout@v5
      with:
        fetch-depth: 0
        submodules: recursive
    - name: Build OpenBSD
      id: build-openbsd
      uses: vmactions/openbsd-vm@v1.2.0
      with:
        usesh: true
        mem: 4096
        prepare: pkg_add git cmake pkgconf boost glib2 qt6-qtbase qt6-qttools sqlite gstreamer1 gstreamer1-plugins-base chromaprint libebur128 taglib libcdio libmtp gdk-pixbuf libgpod fftw3 icu4c kdsingleapplication pulseaudio sparsehash rapidjson
        run: |
          set -e
          export LDFLAGS="-L/usr/local/lib"
          git config --global --add safe.directory ${GITHUB_WORKSPACE}
          cmake -E make_directory build
          cmake -S . -B build -DCMAKE_BUILD_TYPE="Debug" -DENABLE_ALSA=OFF -DENABLE_DISCORD_RPC=OFF
          cmake --build build --config Debug --parallel 4


  build-macos-public:
    name: Build macOS Public
    if: github.repository != 'strawberrymusicplayer/strawberry-private' && github.ref != 'refs/heads/l10n_master'

    strategy:
      fail-fast: false
      matrix:
        runner: [ 'macos-13', 'macos-15' ]
        buildtype: [ 'release' ]

    runs-on: ${{ matrix.runner }}

    steps:

      - name: Set MACOSX_DEPLOYMENT_TARGET
        run: |
          for i in 12 13 14 15; do
            if [ -d "/Library/Developer/CommandLineTools/SDKs/MacOSX${i}.sdk" ]; then
              echo "Using macOS SDK ${i}"
              echo "MACOSX_DEPLOYMENT_TARGET=${i}.0" >> $GITHUB_ENV
              break
            fi
          done

      - name: Verify MACOSX_DEPLOYMENT_TARGET
        run: |
          test "${MACOSX_DEPLOYMENT_TARGET}" = "" && false || echo "MACOSX_DEPLOYMENT_TARGET: ${MACOSX_DEPLOYMENT_TARGET}"

      - name: Set arch
        shell: bash
        run: echo "arch=$(uname -m)" >> $GITHUB_ENV

      - name: Set buildtype
        run: echo "buildtype=$(echo ${{matrix.buildtype}} | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV

      - name: Set cmake buildtype
        run: echo "cmake_buildtype=$(echo ${{env.buildtype}} | awk '{print toupper(substr($0,0,1))tolower(substr($0,2))}')" >> $GITHUB_ENV

      - name: Uninstall homebrew
        run: |
          curl -sfLO https://raw.githubusercontent.com/Homebrew/install/master/uninstall.sh
          chmod +x ./uninstall.sh
          sudo ./uninstall.sh --force
          rm -f uninstall.sh

      - name: Checkout
        uses: actions/checkout@v5
        with:
          fetch-depth: 0
          submodules: recursive

      - name: Import certificate file
        if: github.repository == 'strawberrymusicplayer/strawberry' && github.event.pull_request.head.repo.fork == false
        uses: apple-actions/import-codesign-certs@v5
        with:
          p12-file-base64: ${{ secrets.APPLE_DEVELOPER_ID_CERTIFICATE }}
          p12-password: ${{ secrets.APPLE_DEVELOPER_ID_CERTIFICATE_PASSWORD }}

      - name: Download macOS dependencies
        run: curl -f -O -L https://github.com/strawberrymusicplayer/strawberry-macos-dependencies/releases/latest/download/strawberry-macos-${{env.arch}}-${{env.buildtype}}.tar.xz

      - name: Extract macOS dependencies
        run: sudo tar -C / -xf strawberry-macos-${{env.arch}}-${{env.buildtype}}.tar.xz

      - name: Set prefix path
        run: echo "prefix_path=/opt/strawberry_macos_${{env.arch}}_${{env.buildtype}}" >> $GITHUB_ENV

      - name: Update PATH
        run: echo "${{env.prefix_path}}/bin" >> $GITHUB_PATH

      - name: Create Build Environment
        run: cmake -E make_directory build

      - name: Configure CMake
        env:
          PKG_CONFIG_PATH: ${{env.prefix_path}}/lib/pkgconfig
          LDFLAGS: -L${{env.prefix_path}}/lib -Wl,-rpath,${{env.prefix_path}}/lib
        run: >
          cmake
          --log-level="DEBUG"
          -S .
          -B build
          -DCMAKE_BUILD_TYPE="${{env.cmake_buildtype}}"
          -DCMAKE_PREFIX_PATH="${{env.prefix_path}}/lib/cmake"
          -DBUILD_WERROR=ON
          -DUSE_BUNDLE=ON
          -DICU_ROOT="${{env.prefix_path}}"
          -DAPPLE_DEVELOPER_ID=$(test '${{github.repository}}' = 'strawberrymusicplayer/strawberry' && test '${{github.event.pull_request.base.repo.full_name}}' = '${{github.event.pull_request.head.repo.full_name}}' && echo "383J84DVB6" || echo "")
          -DARCH="${{env.arch}}"
          -DENABLE_SPOTIFY=$(test -f "${{env.prefix_path}}/lib/gstreamer-1.0/libgstspotify.dylib" && echo "ON" || echo "OFF")
          -DENABLE_SPARKLE=ON
          -DENABLE_QTSPARKLE=OFF

      - name: Build
        run: cmake --build build --config Release --parallel 4

      - name: Install
        working-directory: build
        run: make install

      - name: Deploy
        env:
          GIO_EXTRA_MODULES: ${{env.prefix_path}}/lib/gio/modules
          GST_PLUGIN_SCANNER: ${{env.prefix_path}}/libexec/gstreamer-1.0/gst-plugin-scanner
          GST_PLUGIN_PATH: ${{env.prefix_path}}/lib/gstreamer-1.0
          LIBSOUP_LIBRARY_PATH: ${{env.prefix_path}}/lib/libsoup-3.0.0.dylib
        working-directory: build
        run: make deploy

      - name: Manually Codesign
        if: github.repository == 'strawberrymusicplayer/strawberry' && github.event.pull_request.head.repo.fork == false && matrix.runner == 'macos-13'
        working-directory: build
        run: codesign -s 383J84DVB6 -f strawberry.app/Contents/Frameworks/{libpcre2-8.0.dylib,libpcre2-16.0.dylib,libpng16.16.dylib,libfreetype.6.dylib,libzstd.1.dylib} strawberry.app/Contents/Frameworks/png.framework/png strawberry.app

      - name: Manually Codesign
        if: github.repository == 'strawberrymusicplayer/strawberry' && github.event.pull_request.head.repo.fork == false && matrix.runner == 'macos-15'
        working-directory: build
        run: codesign -s 383J84DVB6 -f strawberry.app/Contents/Frameworks/png.framework/png strawberry.app

      - name: Deploy check
        working-directory: build
        run: make deploycheck

      - name: Verify code-signing
        if: github.repository == 'strawberrymusicplayer/strawberry' && github.event.pull_request.head.repo.fork == false
        working-directory: build
        run: codesign --deep -v strawberry.app

      - name: Create DMG
        working-directory: build
        run: make dmg

      - name: SSH Setup
        if: github.repository == 'strawberrymusicplayer/strawberry' && (github.event_name == 'release' || (github.event_name == 'push' && github.event.pull_request.head.repo.fork == false && (github.ref == 'refs/heads/master' || github.ref == 'refs/heads/ci' || github.ref == 'refs/heads/macos')))
        uses: shimataro/ssh-key-action@v2
        with:
          known_hosts: ${{secrets.SSH_KNOWN_HOSTS}}
          key: ${{ secrets.SSH_KEY }}

      - name: Set Upload path
        id: set-upload-path
        if: github.repository == 'strawberrymusicplayer/strawberry' && (github.event_name == 'release' || (github.event_name == 'push' && github.event.pull_request.head.repo.fork == false && (github.ref == 'refs/heads/master' || github.ref == 'refs/heads/ci' || github.ref == 'refs/heads/macos')))
        run: |
          if [ "${{github.event_name}}" = "release" ]; then
            echo "upload_path=${{secrets.DOWNLOADS_PATH}}/stable_releases/macos" >> $GITHUB_OUTPUT
          else
            echo "upload_path=${{secrets.DOWNLOADS_PATH}}/development_releases/macos" >> $GITHUB_OUTPUT
          fi

      - name: Create server path
        if: steps.set-upload-path.outputs.upload_path != ''
        run: ssh -p ${{secrets.SSH_PORT}} -o StrictHostKeyChecking=no ${{secrets.SSH_USER}}@${{secrets.SSH_HOST}} mkdir -p ${{steps.set-upload-path.outputs.upload_path}}

      - name: rsync
        if: steps.set-upload-path.outputs.upload_path != ''
        run: rsync -e "ssh -p ${{secrets.SSH_PORT}} -o StrictHostKeyChecking=no" -var build/*.dmg ${{secrets.SSH_USER}}@${{secrets.SSH_HOST}}:${{steps.set-upload-path.outputs.upload_path}}/


  build-macos-private:
    name: Build macOS Private
    if: github.repository == 'strawberrymusicplayer/strawberry-private'

    strategy:
      fail-fast: false
      matrix:
        runner: [ 'macos-arm64' ]
        buildtype: [ 'release' ]

    runs-on: ${{ matrix.runner }}

    steps:

      - name: Set MACOSX_DEPLOYMENT_TARGET
        run: |
          for i in 12 13 14 15; do
            if [ -d "/Library/Developer/CommandLineTools/SDKs/MacOSX${i}.sdk" ]; then
              echo "Using macOS SDK ${i}"
              echo "MACOSX_DEPLOYMENT_TARGET=${i}.0" >> $GITHUB_ENV
              break
            fi
          done

      - name: Verify MACOSX_DEPLOYMENT_TARGET
        run: |
          test "${MACOSX_DEPLOYMENT_TARGET}" = "" && false || echo "MACOSX_DEPLOYMENT_TARGET: ${MACOSX_DEPLOYMENT_TARGET}"

      - name: Set arch
        shell: bash
        run: echo "arch=$(uname -m)" >> $GITHUB_ENV

      - name: Set buildtype
        run: echo "buildtype=$(echo ${{matrix.buildtype}} | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV

      - name: Set cmake buildtype
        run: echo "cmake_buildtype=$(echo ${{env.buildtype}} | awk '{print toupper(substr($0,0,1))tolower(substr($0,2))}')" >> $GITHUB_ENV

      - name: Checkout
        uses: actions/checkout@v5
        with:
          fetch-depth: 0
          submodules: recursive

      - name: Unlock keychain
        run: security unlock-keychain -p ${{ secrets.MACOS_KEYCHAIN_PASSWORD }}

      - name: Set prefix path
        run: echo "prefix_path=/opt/strawberry_macos_${{env.arch}}_${{env.buildtype}}" >> $GITHUB_ENV

      - name: Update PATH
        run: echo "${{env.prefix_path}}/bin" >> $GITHUB_PATH

      - name: Create Build Environment
        run: cmake -E make_directory build

      - name: Configure CMake
        env:
          PKG_CONFIG_PATH: ${{env.prefix_path}}/lib/pkgconfig
          LDFLAGS: -L${{env.prefix_path}}/lib -Wl,-rpath,${{env.prefix_path}}/lib
        run: >
          cmake
          --log-level="DEBUG"
          -S .
          -B build
          -DCMAKE_BUILD_TYPE="${{env.cmake_buildtype}}"
          -DCMAKE_PREFIX_PATH="${{env.prefix_path}}/lib/cmake"
          -DBUILD_WERROR=ON
          -DUSE_BUNDLE=ON
          -DICU_ROOT="${{env.prefix_path}}"
          -DAPPLE_DEVELOPER_ID="383J84DVB6"
          -DARCH="${{env.arch}}"
          -DENABLE_SPOTIFY=$(test -f "${{env.prefix_path}}/lib/gstreamer-1.0/libgstspotify.dylib" && echo "ON" || echo "OFF")
          -DENABLE_SPARKLE=ON
          -DENABLE_QTSPARKLE=OFF

      - name: Build
        run: cmake --build build --config Release --parallel 4

      - name: Install
        working-directory: build
        run: make install

      - name: Deploy
        env:
          GIO_EXTRA_MODULES: ${{env.prefix_path}}/lib/gio/modules
          GST_PLUGIN_SCANNER: ${{env.prefix_path}}/libexec/gstreamer-1.0/gst-plugin-scanner
          GST_PLUGIN_PATH: ${{env.prefix_path}}/lib/gstreamer-1.0
          LIBSOUP_LIBRARY_PATH: ${{env.prefix_path}}/lib/libsoup-3.0.0.dylib
        working-directory: build
        run: make deploy

      - name: Manually Codesign
        working-directory: build
        run: codesign -s 383J84DVB6 -f strawberry.app/Contents/Frameworks/png.framework/png strawberry.app

      - name: Deploy check
        working-directory: build
        run: make deploycheck

      - name: Verify code-signing
        working-directory: build
        run: codesign --deep -v strawberry.app

      - name: Create DMG
        working-directory: build
        run: make dmg

      - name: Set Upload path
        id: set-upload-path
        run: |
          if [ "${{github.event_name}}" = "release" ]; then
            echo "upload_path=${{secrets.DOWNLOADS_PATH}}/stable_releases/macos" >> $GITHUB_OUTPUT
          else
            echo "upload_path=${{secrets.DOWNLOADS_PATH}}/development_releases/macos" >> $GITHUB_OUTPUT
          fi

      #- name: Create server path
        #run: ssh -p ${{secrets.SSH_PORT}} -o StrictHostKeyChecking=no ${{secrets.SSH_USER}}@${{secrets.SSH_HOST}} mkdir -p ${{steps.set-upload-path.outputs.upload_path}}

      #- name: rsync
        #run: rsync -e "ssh -p ${{secrets.SSH_PORT}} -o StrictHostKeyChecking=no" -var build/*.dmg ${{secrets.SSH_USER}}@${{secrets.SSH_HOST}}:${{steps.set-upload-path.outputs.upload_path}}/


  build-windows-mingw:
    name: Build Windows MinGW
    if: github.repository != 'strawberrymusicplayer/strawberry-private' && github.ref != 'refs/heads/l10n_master'
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        arch: [ 'i686', 'x86_64' ]
        buildtype: [ 'debug', 'release' ]
    container:
      image: jonaski/strawberry-mxe-${{matrix.arch}}-${{matrix.buildtype}}
    steps:

      - name: Set cmake buildtype
        shell: bash
        run: echo "cmake_buildtype=$(echo ${{matrix.buildtype}} | sed 's/.*/\u&/')" >> $GITHUB_ENV

      - name: Checkout
        uses: actions/checkout@v5
        with:
          fetch-depth: 0
          submodules: recursive

      - name: Add safe git directory
        run: git config --global --add safe.directory ${GITHUB_WORKSPACE}

      - name: Create Build Environment
        run: cmake -E make_directory build

      - name: Link MXE directory
        run: ln -s /strawberry-mxe ~/mxe-shared

      - name: Run CMake
        env:
          PKG_CONFIG_PATH: /strawberry-mxe/usr/${{matrix.arch}}-w64-mingw32.shared/lib/pkgconfig
        run: >
          cmake
          -S .
          -B build
          -DCMAKE_TOOLCHAIN_FILE="../cmake/Toolchain-${{matrix.arch}}-w64-mingw32-shared.cmake"
          -DCMAKE_BUILD_TYPE="${{env.cmake_buildtype}}"
          -DCMAKE_PREFIX_PATH="/strawberry-mxe/usr/${{matrix.arch}}-w64-mingw32.shared/qt6"
          -DBUILD_WERROR=ON
          -DARCH="${{matrix.arch}}"
          -DENABLE_WIN32_CONSOLE=$(test "${{matrix.buildtype}}" = "debug" && echo "ON" || echo "OFF")
          -DENABLE_GIO=OFF
          -DENABLE_AUDIOCD=OFF
          -DENABLE_MTP=OFF
          -DENABLE_GPOD=OFF
          -DENABLE_SPOTIFY=OFF

      - name: Run Make
        run: cmake --build build --config "${{env.cmake_buildtype}}" --parallel $(nproc)

      - name: Create directories
        working-directory: build
        run: mkdir -p gio-modules platforms sqldrivers tls imageformats styles gstreamer-plugins nsisplugins

      - name: Copy GIO modules
        working-directory: build
        run: cp /strawberry-mxe/usr/${{matrix.arch}}-w64-mingw32.shared/lib/gio/modules/{libgiognutls.dll,libgioopenssl.dll} ${GITHUB_WORKSPACE}/build/gio-modules/

      - name: Copy Qt platform plugins
        working-directory: build
        run: cp /strawberry-mxe/usr/${{matrix.arch}}-w64-mingw32.shared/qt6/plugins/platforms/qwindows.dll ${GITHUB_WORKSPACE}/build/platforms/

      - name: Copy Qt styles
        working-directory: build
        run: cp /strawberry-mxe/usr/${{matrix.arch}}-w64-mingw32.shared/qt6/plugins/styles/qmodernwindowsstyle.dll ${GITHUB_WORKSPACE}/build/styles/

      - name: Copy Qt TLS plugins
        working-directory: build
        run: cp /strawberry-mxe/usr/${{matrix.arch}}-w64-mingw32.shared/qt6/plugins/tls/{qschannelbackend.dll,qopensslbackend.dll} ${GITHUB_WORKSPACE}/build/tls/

      - name: Copy Qt SQL drivers
        working-directory: build
        run: cp /strawberry-mxe/usr/${{matrix.arch}}-w64-mingw32.shared/qt6/plugins/sqldrivers/qsqlite.dll ${GITHUB_WORKSPACE}/build/sqldrivers/

      - name: Copy Qt imageformats
        working-directory: build
        run: cp /strawberry-mxe/usr/${{matrix.arch}}-w64-mingw32.shared/qt6/plugins/imageformats/{qgif.dll,qico.dll,qjpeg.dll} ${GITHUB_WORKSPACE}/build/imageformats/

      - name: Copy gstreamer plugins
        working-directory: build
        run: cp /strawberry-mxe/usr/${{matrix.arch}}-w64-mingw32.shared/bin/gstreamer-1.0/*.dll ${GITHUB_WORKSPACE}/build/gstreamer-plugins/

      - name: Copy extra binaries
        working-directory: build
        run: cp /strawberry-mxe/usr/${{matrix.arch}}-w64-mingw32.shared/bin/{sqlite3.exe,gst-launch-1.0.exe,gst-play-1.0.exe,gst-discoverer-1.0.exe,libsoup-3.0-0.dll,libnghttp2.dll} .

      - name: Copy extra binaries (debug)
        if: matrix.buildtype == 'debug'
        working-directory: build
        run: cp /strawberry-mxe/usr/${{matrix.arch}}-w64-mingw32.shared/bin/{gdb.exe,libreadline8.dll} .

      - name: Copy dependencies
        working-directory: build
        run: >
          /strawberry-mxe/tools/copydlldeps.sh
          -c
          -d .
          -F .
          -F ./platforms
          -F ./styles
          -F ./imageformats
          -F ./tls
          -F ./sqldrivers
          -F ./gstreamer-plugins
          -R /strawberry-mxe/usr/${{matrix.arch}}-w64-mingw32.shared

      - name: Strip binaries
        if: matrix.buildtype == 'release'
        working-directory: build
        run: find . -type f \( -iname \*.dll -o -iname \*.exe \) -exec /strawberry-mxe/usr/bin/${{matrix.arch}}-w64-mingw32.shared-strip {} \;

      - name: Strip gdb.exe
        if: matrix.buildtype == 'debug'
        working-directory: build
        run: /strawberry-mxe/usr/bin/${{matrix.arch}}-w64-mingw32.shared-strip gdb.exe

      - name: Copy nsis files
        working-directory: build
        run: cp ${GITHUB_WORKSPACE}/dist/windows/*.nsh ${GITHUB_WORKSPACE}/dist/windows/*.ico .

      - name: Copy COPYING license file
        working-directory: build
        run: cp ${GITHUB_WORKSPACE}/COPYING .

      - name: List files
        working-directory: build
        run: ls

      - name: Check that all files are included in nsi
        shell: bash
        working-directory: build
        run: |
          files_missing=
          for i in $(ls -1 *.dll *.exe); do
            nsi_file_entry=$(grep -i "^\s\+File\s\+\"$i\"$" strawberry.nsi || true)
            if [ "${nsi_file_entry}" = "" ]; then
              echo "File ${i} is missing File entry."
            fi
            nsi_file_delete_entry=$(grep -i "^\s\+Delete\s\+\"\$INSTDIR\\\\$i\"$" strawberry.nsi || true)
            if [ "${nsi_file_delete_entry}" = "" ] ; then
              echo "File ${i} is missing Delete entry."
            fi
            if [ "${nsi_file_entry}" = "" ] || [ "${nsi_file_delete_entry}" = "" ] ; then
              if [ "${files_missing}" = "" ]; then
                files_missing="${i}"
              else
                files_missing="${files_missing} $i"
              fi
            fi
          done
          if ! [ "${files_missing}" = "" ]; then
            echo "Files missing in nsi: ${files_missing}"
            exit 1
          fi

      - name: Build Windows installer
        working-directory: build
        run: makensis strawberry.nsi

      - name: Set Upload path
        id: set-upload-path
        if: github.repository == 'strawberrymusicplayer/strawberry' && (github.event_name == 'release' || (github.event_name == 'push' && github.event.pull_request.head.repo.fork == false && (github.ref == 'refs/heads/master' || github.ref == 'refs/heads/ci' || github.ref == 'refs/heads/windows')))
        run: |
          if [ "${{github.event_name}}" = "release" ]; then
            echo "upload_path=${{secrets.DOWNLOADS_PATH}}/stable_releases/windows/mingw" >> $GITHUB_OUTPUT
          else
            echo "upload_path=${{secrets.DOWNLOADS_PATH}}/development_releases/windows/mingw" >> $GITHUB_OUTPUT
          fi

      - name: SSH Setup
        if: steps.set-upload-path.outputs.upload_path != ''
        uses: shimataro/ssh-key-action@v2
        with:
          known_hosts: ${{secrets.SSH_KNOWN_HOSTS}}
          key: ${{ secrets.SSH_KEY }}

      - name: Create server path
        if: steps.set-upload-path.outputs.upload_path != ''
        run: ssh -p ${{secrets.SSH_PORT}} -o StrictHostKeyChecking=no ${{secrets.SSH_USER}}@${{secrets.SSH_HOST}} mkdir -p ${{steps.set-upload-path.outputs.upload_path}}

      - name: rsync
        if: steps.set-upload-path.outputs.upload_path != ''
        run: rsync -e "ssh -p ${{secrets.SSH_PORT}} -o StrictHostKeyChecking=no" -var build/StrawberrySetup*.exe ${{secrets.SSH_USER}}@${{secrets.SSH_HOST}}:${{steps.set-upload-path.outputs.upload_path}}/


  build-windows-msvc:
    name: Build Windows MSVC
    if: github.repository != 'strawberrymusicplayer/strawberry-private' && github.ref != 'refs/heads/l10n_master'
    strategy:
      fail-fast: false
      matrix:
       include:
         - name: "x86_64 debug"
           runner: windows-2022
           arch: x86_64
           buildtype: debug

         - name: "x86_64 release"
           runner: windows-2022
           arch: x86_64
           buildtype: release

         - name: "x86 debug"
           runner: windows-2022
           arch: x86
           buildtype: debug

         - name: "x86 release"
           runner: windows-2022
           arch: x86
           buildtype: release

         - name: "arm64 debug"
           runner: windows-11-arm
           arch: arm64
           buildtype: debug

         - name: "arm64 release"
           runner: windows-11-arm
           arch: arm64
           buildtype: release

    runs-on: ${{matrix.runner}}

    steps:

      - name: Set prefix path
        shell: bash
        run: |
          echo "prefix_path_backslash=c:\strawberry_msvc_${{matrix.arch}}_${{matrix.buildtype}}" >> $GITHUB_ENV
          echo "prefix_path_forwardslash=c:/strawberry_msvc_${{matrix.arch}}_${{matrix.buildtype}}" >> $GITHUB_ENV
          echo "prefix_path_unix=/c/strawberry_msvc_${{matrix.arch}}_${{matrix.buildtype}}" >> $GITHUB_ENV

      - name: Set cmake buildtype
        shell: bash
        run: echo "cmake_buildtype=$(echo ${{matrix.buildtype}} | sed 's/.*/\u&/')" >> $GITHUB_ENV

      - name: Show SDK versions
        shell: bash
        run: ls -la "c:/Program Files (x86)/Windows Kits/10/include"

      - name: Set SDK version
        if: matrix.arch != 'arm64'
        shell: bash
        run: echo "sdk_version=10.0.19041.0" >> $GITHUB_ENV

      - name: Set SDK version
        if: matrix.arch == 'arm64'
        shell: bash
        run: echo "sdk_version=10.0.26100.0" >> $GITHUB_ENV

      - name: Install rsync
        shell: cmd
        run: choco install --no-progress rsync

      - name: Cleanup PATH
        uses: egor-tensin/cleanup-path@v4
        with:
          dirs: ${{env.prefix_path_backslash}}\bin;C:\Windows;C:\Windows\system32;C:\Program Files\Git\bin;C:\Program Files\CMake\bin;C:\Program Files\GitHub CLI;C:\ProgramData\Chocolatey\bin;C:\Program Files (x86)\NSIS

      - name: Create downloads directory
        shell: cmd
        run: mkdir downloads

      - name: Download Windows MSVC dependencies
        shell: cmd
        working-directory: downloads
        run: curl -f -O -L https://github.com/strawberrymusicplayer/strawberry-msvc-dependencies/releases/latest/download/strawberry-msvc-${{matrix.arch}}-${{matrix.buildtype}}.tar.xz

      - name: Extract Windows MSVC dependencies
        shell: bash
        working-directory: downloads
        run: tar -C /c -xf strawberry-msvc-${{matrix.arch}}-${{matrix.buildtype}}.tar.xz

      - name: Update PATH
        run: echo "${{env.prefix_path_backslash}}\bin" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append

      - name: Copy bin files
        shell: bash
        run: cp /c/strawberry/c/bin/{patch.exe,strip.exe,strings.exe,objdump.exe} ${{env.prefix_path_unix}}/bin

      - name: Delete conflicting files
        shell: bash
        run: rm -rf /c/{msys64,mingw32,mingw64} /c/strawberry/c "/c/program files/OpenSSL"

      - name: Delete conflicting icu
        shell: bash
        run: find "/c/program files (x86)/windows kits/" -type f \( -iname 'icu*.lib' -o -iname 'icu*.h' \) -print -delete

      - name: Download NSIS LockedList plugin
        shell: cmd
        working-directory: downloads
        run: curl -f -O -L https://nsis.sourceforge.io/mediawiki/images/d/d3/LockedList.zip

      - name: Extract NSIS LockedList plugin
        shell: cmd
        working-directory: downloads
        run: 7z x LockedList.zip

      - name: Download NSIS Registry plugin
        shell: cmd
        working-directory: downloads
        run: curl -f -O -L https://nsis.sourceforge.io/mediawiki/images/4/47/Registry.zip

      - name: Extract NSIS Registry plugin
        shell: cmd
        working-directory: downloads
        run: 7z x Registry.zip

      - name: Download NSIS Inetc plugin
        shell: cmd
        working-directory: downloads
        run: curl -f -O -L https://nsis.sourceforge.io/mediawiki/images/c/c9/Inetc.zip

      - name: Extract NSIS Inetc plugin
        shell: cmd
        working-directory: downloads
        run: 7z x Inetc.zip

      - name: Install NSIS plugins
        shell: cmd
        working-directory: downloads
        run: |
          copy "Plugins\LockedList64.dll" "C:\Program Files (x86)\NSIS\Plugins\"
          copy "Plugins\x86-unicode\LockedList.dll" "C:\Program Files (x86)\NSIS\Plugins\x86-unicode\"
          copy "Desktop\Plugin\registry.dll" "C:\Program Files (x86)\NSIS\Plugins\"
          copy "Desktop\Plugin\registry.dll" "C:\Program Files (x86)\NSIS\Plugins\x86-unicode\"
          copy "Plugins\x86-unicode\INetC.dll" "C:\Program Files (x86)\NSIS\Plugins\x86-unicode\"

      - name: Setup MSVC Environment
        uses: ilammy/msvc-dev-cmd@v1
        with:
          arch: ${{matrix.arch}}
          sdk: ${{env.sdk_version}}
          vsversion: 2022

      - name: Checkout
        uses: actions/checkout@v5
        with:
          fetch-depth: 0
          submodules: recursive

      - name: Add safe git directory
        shell: bash
        run: git config --global --add safe.directory ${GITHUB_WORKSPACE}

      - name: Create Build Environment
        shell: cmd
        run: cmake -E make_directory build

      - name: Set ENABLE_WIN32_CONSOLE
        shell: bash
        run: echo "enable_win32_console=$(test "${{matrix.buildtype}}" = "debug" && echo "ON" || echo "OFF")" >> $GITHUB_ENV

      - name: Set ENABLE_SPOTIFY
        shell: bash
        run: echo "enable_spotify=$(test -f "${{env.prefix_path_unix}}/lib/gstreamer-1.0/gstspotify.dll" && echo "ON" || echo "OFF")" >> $GITHUB_ENV

      - name: Remove -lm from .pc files
        if: matrix.arch == 'arm64'
        shell: bash
        run: sed -i 's/\-lm$//g' ${{env.prefix_path_unix}}/lib/pkgconfig/*.pc

      - name: Run CMake
        shell: cmd
        run: >
          cmake
          -S .
          -B build
          -G "Ninja"
          -DCMAKE_BUILD_TYPE="${{env.cmake_buildtype}}"
          -DCMAKE_PREFIX_PATH="${{env.prefix_path_forwardslash}}/lib/cmake"
          -DARCH="${{matrix.arch}}"
          -DENABLE_WIN32_CONSOLE=${{env.enable_win32_console}}
          -DPKG_CONFIG_EXECUTABLE="${{env.prefix_path_forwardslash}}/bin/pkg-config.exe"
          -DICU_ROOT="${{env.prefix_path_forwardslash}}"
          -DENABLE_GIO=OFF
          -DENABLE_AUDIOCD=OFF
          -DENABLE_MTP=OFF
          -DENABLE_GPOD=OFF
          -DENABLE_SPOTIFY=${{env.enable_spotify}}

      - name: Run Make
        shell: cmd
        env:
          CL: "/MP"
        run: cmake --build build --config "${{env.cmake_buildtype}}" --parallel 4

      - name: Copy extra binaries
        shell: cmd
        working-directory: build
        run: |
          copy ${{env.prefix_path_backslash}}\bin\libssl-3*.dll
          copy ${{env.prefix_path_backslash}}\bin\libcrypto-3*.dll
          copy ${{env.prefix_path_backslash}}\bin\soup-3.0-0.dll
          copy ${{env.prefix_path_backslash}}\bin\gst-launch-1.0.exe
          copy ${{env.prefix_path_backslash}}\bin\gst-play-1.0.exe
          copy ${{env.prefix_path_backslash}}\bin\gst-discoverer-1.0.exe
          copy ${{env.prefix_path_backslash}}\bin\sqlite3.exe

      - name: Create directories
        shell: cmd
        working-directory: build
        run: |
          mkdir gio-modules
          mkdir platforms
          mkdir styles
          mkdir tls
          mkdir sqldrivers
          mkdir imageformats
          mkdir gstreamer-plugins
          mkdir nsisplugins

      - name: Copy GIO modules
        shell: cmd
        working-directory: build
        run: copy ${{env.prefix_path_backslash}}\lib\gio\modules\*.dll .\gio-modules\

      - name: Copy Qt platform plugins
        shell: cmd
        working-directory: build
        run: copy ${{env.prefix_path_backslash}}\plugins\platforms\qwindows*.dll .\platforms\

      - name: Copy Qt styles
        shell: cmd
        working-directory: build
        run: copy ${{env.prefix_path_backslash}}\plugins\styles\qmodernwindowsstyle*.dll .\styles\

      - name: Copy Qt TLS plugins
        shell: cmd
        working-directory: build
        run: copy ${{env.prefix_path_backslash}}\plugins\tls\*.dll .\tls\

      - name: Copy Qt SQL drivers
        shell: cmd
        working-directory: build
        run: copy ${{env.prefix_path_backslash}}\plugins\sqldrivers\qsqlite*.dll .\sqldrivers\

      - name: Copy Qt imageformats plugins
        shell: cmd
        working-directory: build
        run: copy ${{env.prefix_path_backslash}}\plugins\imageformats\*.dll .\imageformats\

      - name: Copy gstreamer plugins
        shell: cmd
        working-directory: build
        run: copy ${{env.prefix_path_backslash}}\lib\gstreamer-1.0\*.dll .\gstreamer-plugins\

      - name: Download copydlldeps.sh
        if: matrix.arch != 'arm64'
        shell: bash
        working-directory: build
        run: curl -f -O -L https://raw.githubusercontent.com/strawberrymusicplayer/strawberry-mxe/master/tools/copydlldeps.sh

      - name: Copy dependencies
        # copydlldeps.sh doesn't work with arm64 binaries.
        if: matrix.arch != 'arm64'
        shell: bash
        working-directory: build
        run: >
          ./copydlldeps.sh
          -c
          -d .
          -F .
          -F ./platforms
          -F ./styles
          -F ./tls
          -F ./sqldrivers
          -F ./imageformats
          -F ./gio-modules
          -F ./gstreamer-plugins
          -R ${{env.prefix_path_unix}}/bin

      - name: Copy dependencies
        if: matrix.arch == 'arm64'
        shell: bash
        working-directory: build
        run: cp -v ${{env.prefix_path_unix}}/bin/{avcodec*.dll,avfilter*.dll,avformat*.dll,avutil*.dll,brotlicommon.dll,brotlidec.dll,chromaprint.dll,ebur128.dll,faad-2.dll,fdk-aac.dll,ffi-7.dll,FLAC.dll,freetype*.dll,getopt.dll,gio-2.0-0.dll,glib-2.0-0.dll,gme.dll,gmodule-2.0-0.dll,gobject-2.0-0.dll,gst-discoverer-1.0.exe,gst-launch-1.0.exe,gst-play-1.0.exe,gstadaptivedemux-1.0-0.dll,gstapp-1.0-0.dll,gstaudio-1.0-0.dll,gstbadaudio-1.0-0.dll,gstbase-1.0-0.dll,gstcodecparsers-1.0-0.dll,gstfft-1.0-0.dll,gstisoff-1.0-0.dll,gstmpegts-1.0-0.dll,gstnet-1.0-0.dll,gstpbutils-1.0-0.dll,gstreamer-1.0-0.dll,gstriff-1.0-0.dll,gstrtp-1.0-0.dll,gstrtsp-1.0-0.dll,gstsdp-1.0-0.dll,gsttag-1.0-0.dll,gsturidownloader-1.0-0.dll,gstvideo-1.0-0.dll,gstwinrt-1.0-0.dll,harfbuzz.dll,icudt*.dll,icuin*.dll,icuuc*.dll,intl-8.dll,jpeg62.dll,kdsingleapplication*.dll,libbs2b.dll,libcrypto-3-*.dll,fftw3.dll,libiconv*.dll,liblzma.dll,libmp3lame.dll,libopenmpt.dll,libpng16*.dll,libspeex*.dll,libssl-3-*.dll,libxml2*.dll,mpcdec.dll,mpg123.dll,nghttp2.dll,ogg.dll,opus.dll,orc-0.4-0.dll,pcre2-16*.dll,pcre2-8*.dll,postproc*.dll,psl-5.dll,Qt6Concurrent*.dll,Qt6Core*.dll,Qt6Gui*.dll,Qt6Network*.dll,Qt6Sql*.dll,Qt6Widgets*.dll,qtsparkle-qt6.dll,soup-3.0-0.dll,sqlite3.dll,sqlite3.exe,swresample*.dll,swscale*.dll,tag.dll,vorbis.dll,vorbisfile.dll,wavpackdll.dll,zlib*.dll} .

      - name: Copy nsis files
        shell: cmd
        working-directory: build
        run: |
          copy ..\dist\windows\*.nsh .
          copy ..\dist\windows\*.ico .

      - name: Copy COPYING license file
        shell: cmd
        working-directory: build
        run: copy ..\COPYING .

      - name: List files
        shell: cmd
        working-directory: build
        run: dir

      - name: Check that all files are included in nsi
        shell: bash
        working-directory: build
        run: |
          files_missing=
          for i in $(ls -1 *.dll *.exe); do
            nsi_file_entry=$(grep -i "^\s\+File\s\+\"$i\"$" strawberry.nsi || true)
            if [ "${nsi_file_entry}" = "" ]; then
              echo "File ${i} is missing File entry."
            fi
            nsi_file_delete_entry=$(grep -i "^\s\+Delete\s\+\"\$INSTDIR\\\\$i\"$" strawberry.nsi || true)
            if [ "${nsi_file_delete_entry}" = "" ] ; then
              echo "File ${i} is missing Delete entry."
            fi
            if [ "${nsi_file_entry}" = "" ] || [ "${nsi_file_delete_entry}" = "" ] ; then
              if [ "${files_missing}" = "" ]; then
                files_missing="${i}"
              else
                files_missing="${files_missing} $i"
              fi
            fi
          done
          if ! [ "${files_missing}" = "" ]; then
            echo "Files missing in nsi: ${files_missing}"
            exit 1
          fi

      - name: Download MSVC runtime
        shell: bash
        working-directory: build
        run: curl -f -O -L https://aka.ms/vs/17/release/vc_redist.$(test "${{matrix.arch}}" = "x86_64" && echo "x64" || echo "${{matrix.arch}}").exe

      - name: Create nsis installer
        shell: cmd
        working-directory: build
        run: makensis strawberry.nsi

      - name: Set Upload path
        id: set-upload-path
        shell: bash
        if: github.repository == 'strawberrymusicplayer/strawberry' && (github.event_name == 'release' || (github.event_name == 'push' && github.event.pull_request.head.repo.fork == false && (github.ref == 'refs/heads/master' || github.ref == 'refs/heads/ci' || github.ref == 'refs/heads/windows')))
        run: |
          if [ "${{github.event_name}}" = "release" ]; then
            echo "upload_path=${{secrets.DOWNLOADS_PATH}}/stable_releases/windows/msvc" >> $GITHUB_OUTPUT
          else
            echo "upload_path=${{secrets.DOWNLOADS_PATH}}/development_releases/windows/msvc" >> $GITHUB_OUTPUT
          fi

      - name: SSH Setup
        if: steps.set-upload-path.outputs.upload_path != ''
        shell: bash
        run: |
          mkdir -p ~/.ssh
          echo "${{secrets.SSH_KNOWN_HOSTS}}" > ~/.ssh/known_hosts
          echo "${{secrets.SSH_KEY}}" > ~/.ssh/id_rsa

      - name: Create server path
        if: steps.set-upload-path.outputs.upload_path != ''
        shell: bash
        run: ssh -p ${{secrets.SSH_PORT}} -o StrictHostKeyChecking=no ${{secrets.SSH_USER}}@${{secrets.SSH_HOST}} mkdir -p ${{steps.set-upload-path.outputs.upload_path}}

      - name: rsync
        if: steps.set-upload-path.outputs.upload_path != ''
        shell: bash
        run: rsync -e "/c/ProgramData/chocolatey/lib/rsync/tools/bin/ssh.exe -p ${{secrets.SSH_PORT}} -o StrictHostKeyChecking=no" -var build/StrawberrySetup*.exe ${{secrets.SSH_USER}}@${{secrets.SSH_HOST}}:${{steps.set-upload-path.outputs.upload_path}}/


  upload:
    name: Upload
    if: (success() || failure()) && github.repository == 'strawberrymusicplayer/strawberry' && (github.event_name == 'release' || (github.event_name == 'push' && github.event.pull_request.head.repo.fork == false && (github.ref == 'refs/heads/master' || github.ref == 'refs/heads/ci')))
    runs-on: ubuntu-latest
    needs:
      - build-opensuse
      - build-fedora
      - build-mageia
      - build-debian
      - build-ubuntu
    steps:
      - name: Install packages
        env:
          DEBIAN_FRONTEND: noninteractive
        run: sudo apt install -y git rsync
      - name: Checkout
        uses: actions/checkout@v5
        with:
          fetch-depth: 0
      - name: Download artifacts
        uses: actions/download-artifact@v5
        with:
          path: artifacts
      - name: SSH Setup
        uses: shimataro/ssh-key-action@v2
        with:
          known_hosts: ${{secrets.SSH_KNOWN_HOSTS}}
          key: ${{secrets.SSH_KEY}}
      - name: Upload
        run: |
          for i in $(find artifacts -type f); do
            if [ "${{github.event_name}}" = "release" ]; then
              upload_path="${{secrets.RELEASES_PATH}}/"
            else
              distro=$(echo "$i" | cut -d '/' -f 2)
              if [ -z "$(echo "${distro}" | grep '-' || true)" ]; then
                upload_path="${{secrets.BUILDS_PATH}}/${distro}/"
              else
                distro_name=$(echo "${distro}" | cut -d '-' -f 1)
                distro_version=$(echo "${distro}" | cut -d '-' -f 2)
                upload_path="${{secrets.BUILDS_PATH}}/${distro_name}/${distro_version}/"
              fi
            fi
            ssh -p ${{secrets.SSH_PORT}} -o StrictHostKeyChecking=no ${{secrets.SSH_USER}}@${{secrets.SSH_HOST}} mkdir -p ${upload_path}
            rsync -e "ssh -p ${{secrets.SSH_PORT}} -o StrictHostKeyChecking=no" -var $i ${{secrets.SSH_USER}}@${{secrets.SSH_HOST}}:${upload_path}/
          done


  attach:
    name: Attach to release
    if: (success() || failure()) && github.event_name == 'release'
    runs-on: ubuntu-latest
    needs:
      - build-opensuse
      - build-fedora
      - build-mageia
      - build-debian
      - build-ubuntu
    steps:
      - name: Install packages
        env:
          DEBIAN_FRONTEND: noninteractive
        run: sudo apt install -y git jq gh
      - name: Checkout
        uses: actions/checkout@v5
        with:
          fetch-depth: 0
      - name: Show release assets
        env:
          GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
        run: gh release view "${{github.event.release.tag_name}}" --json assets | jq -r '.assets[].name'
      - name: Download artifacts
        uses: actions/download-artifact@v5
        with:
          path: artifacts
      - name: Add artifacts to release
        env:
          GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
        run: |
          echo "Release version: ${{github.event.release.tag_name}}"
          filenames=()
          files=()
          for i in $(find artifacts -type f); do
            filename=$(basename $i)
            if [[ ${filenames[@]} =~ ${filename} ]]; then
              echo "Skipping duplicate file: ${filename}"
              continue
            fi
            filenames+=("${filename}")
            existing_asset=$(gh release view "${{github.event.release.tag_name}}" --json assets | jq -r '.assets[].name' | tr -d '[:blank:]' | grep ".*/${filename}\$" 2>/dev/null || true)
            if [ "${existing_asset}" = "" ]; then
              echo "Adding file: ${filename}"
              files+=("${i}")
            else
              echo "Release already has file: ${filename}"
            fi
          done
          files_list="${files[@]}"
          if ! [ "${files_list}" = "" ]; then
            echo "Adding files to GitHub release"
            gh release upload "${{github.event.release.tag_name}}" ${files_list}
          fi
