Name:           lemonade
Version:        11.8.0
Release:        1%{?dist}
Summary:        Lightweight, high-performance local LLM server
License:        Apache-2.0
URL:            https://lemonade-server.ai/
# Upstream source is vendored via the lemonade/ git submodule (pinned at v%%{version}).
# The tarball is generated by tito's SubmoduleAwareBuilder and submitted to COPR as an SRPM.
Source0:        %{name}-%{version}.tar.gz
%global upstream lemonade

%global debug_package %{nil}

# C++ build toolchain (server + tray)
BuildRequires:  cmake
BuildRequires:  ninja-build
BuildRequires:  gcc-c++
BuildRequires:  git

# lemonade-server
BuildRequires:  systemd-rpm-macros
BuildRequires:  systemd-devel
BuildRequires:  libcurl-devel
BuildRequires:  libzstd-devel
BuildRequires:  libwebsockets-devel
BuildRequires:  libdrm-devel
BuildRequires:  nlohmann-json-devel >= 3.11.3
BuildRequires:  cli11-devel >= 2.4.2
BuildRequires:  cpp-httplib-devel >= 0.26.0
BuildRequires:  mbedtls-devel

# lemonade-tray
BuildRequires:  gtk3-devel
BuildRequires:  libappindicator-gtk3-devel
BuildRequires:  libnotify-devel

# lemonade-desktop (Tauri)
BuildRequires:  nodejs
BuildRequires:  npm
BuildRequires:  rust
BuildRequires:  cargo
BuildRequires:  webkit2gtk4.1-devel
BuildRequires:  openssl-devel
BuildRequires:  libsoup3-devel
BuildRequires:  javascriptcoregtk4.1-devel

# packaging validation
BuildRequires:  desktop-file-utils
BuildRequires:  libappstream-glib

Requires:       (lemonade-server%{?_isa} = %{version}-%{release} or lemonade-server-embedded%{?_isa} = %{version}-%{release})
Requires:       %{name}-desktop%{?_isa} = %{version}-%{release}

%description
Lemonade is a lightweight, high-performance local LLM server with support for
multiple backends including llama.cpp, FastFlowLM, and RyzenAI.

This package is a meta-package that installs both the Lemonade server and the
desktop application.

%package server
Summary:        Server components for Lemonade
Provides:       lemond = %{version}-%{release}
Provides:       lemonade-server-variant = %{version}-%{release}
Conflicts:      %{name}-server-embedded%{?_isa}
Requires:       %{name}-cli%{?_isa} = %{version}-%{release}
# The lemonade system user/group is created from the sysusers.d file
# (%%sysusers_create_compat in %%pre). On F42+ this macro is a no-op and
# the user is created automatically from %%{_sysusersdir}/lemonade.conf.
%{?sysusers_requires_compat}
%{?systemd_requires}
# owns /usr/share/icons/hicolor/ tree where the app icon is installed
Requires:       hicolor-icon-theme
Requires:       unzip
Requires:       tar
Requires:       gzip
Requires:       xz
Requires:       pciutils

%description server
The Lemonade server subpackage contains the core LLM server and web interface.

%package server-embedded
Summary:        Embedded standalone server for Lemonade
Provides:       lemond = %{version}-%{release}
Provides:       lemonade-server-variant = %{version}-%{release}
Conflicts:      %{name}-server%{?_isa}
Requires:       hicolor-icon-theme
Requires:       unzip
Requires:       tar
Requires:       gzip
Requires:       xz
Requires:       pciutils

%description server-embedded
The Lemonade server embedded subpackage contains a portable, self-contained build of
the core LLM server and bundled resource configurations. It runs standalone without
requiring system service user setup.

%package cli
Summary:        Command-line interface for Lemonade
Conflicts:      %{name}-server%{?_isa} < 10.9.0-3

%description cli
The Lemonade CLI subpackage contains the command-line client for interacting
with the Lemonade LLM server.

%package tray
Summary:        System tray application for Lemonade
Requires:       (lemonade-server%{?_isa} = %{version}-%{release} or lemonade-server-embedded%{?_isa} = %{version}-%{release})

%description tray
Provides the lemonade-tray system tray application. Users can configure it
to start at login via their desktop environment's autostart settings.

%package desktop
Summary:        Desktop application for Lemonade
Requires:       (lemonade-server%{?_isa} = %{version}-%{release} or lemonade-server-embedded%{?_isa} = %{version}-%{release})
Obsoletes:      %{name}-app < %{version}-%{release}

%description desktop
A modern desktop interface for managing and interacting with the
Lemonade LLM server.

%package web
Summary:        Web browser launcher for Lemonade
Requires:       (lemonade-server%{?_isa} = %{version}-%{release} or lemonade-server-embedded%{?_isa} = %{version}-%{release})
Requires:       xdg-utils
Requires:       jq

%description web
Provides the lemonade-web launcher script and desktop entry for opening
the Lemonade web interface in a browser.

%prep
%autosetup -n %{name}-%{version}

%build
# Configure from within the upstream source subdirectory (%%{upstream}/)
pushd %{upstream}
%cmake -G Ninja \
    -DBUILD_ELECTRON_APP=OFF \
    -DBUILD_TAURI_APP=OFF \
    -DUSE_SYSTEM_JSON=ON \
    -DUSE_SYSTEM_CLI11=ON \
    -DUSE_SYSTEM_HTTPLIB=ON \
    -DUSE_SYSTEM_NODEJS_MODULES=OFF \
    -DREQUIRE_LINUX_TRAY=ON
%cmake_build
%cmake_build --target embeddable
popd

# Build the Tauri app
pushd %{upstream}/src/app
npm config set audit false
npm install --no-audit --no-fund --prefer-offline
CARGO_BUILD_JOBS=%{?_smp_build_ncpus} npm run build:nobundle
popd

%install
pushd %{upstream}
%cmake_install
popd

# --- Icons ---
# Install the application icon into the hicolor theme.
# Use the reverse-DNS name the tray binary looks up at runtime via find_icon_path().
install -Dpm 0644 %{upstream}/src/app/assets/logo.svg \
    %{buildroot}%{_datadir}/icons/hicolor/scalable/apps/ai.lemonade_server.Lemonade.svg

# --- Web app launcher ---
# Shell script that opens the built-in web interface in the user's browser.
install -Dpm 0755 %{upstream}/data/lemonade-web-app \
    %{buildroot}%{_bindir}/lemonade-web

# --- Desktop entries ---
# lemonade.desktop: Tauri desktop app (desktop subpackage)
desktop-file-install \
    --dir=%{buildroot}%{_datadir}/applications \
    --set-icon=ai.lemonade_server.Lemonade \
    --set-name="Lemonade Desktop" \
    %{upstream}/data/lemonade-app.desktop
mv %{buildroot}%{_datadir}/applications/lemonade-app.desktop \
   %{buildroot}%{_datadir}/applications/lemonade.desktop

# lemonade-web.desktop: web interface launcher (base package)
desktop-file-install \
    --dir=%{buildroot}%{_datadir}/applications \
    --set-key=Exec --set-value="lemonade-web %u" \
    --set-icon=ai.lemonade_server.Lemonade \
    %{upstream}/data/lemonade-web-app.desktop
mv %{buildroot}%{_datadir}/applications/lemonade-web-app.desktop \
   %{buildroot}%{_datadir}/applications/lemonade-web.desktop

# --- AppStream metadata ---
install -Dpm 0644 /dev/stdin \
    %{buildroot}%{_datadir}/metainfo/lemonade.appdata.xml << 'EOF'
<?xml version="1.0" encoding="UTF-8"?>
<component type="desktop-application">
  <id>lemonade</id>
  <metadata_license>FSFAP</metadata_license>
  <project_license>Apache-2.0</project_license>
  <name>Lemonade</name>
  <summary>Lightweight, high-performance local LLM server</summary>
  <description>
    <p>
      Lemonade is a lightweight, high-performance local LLM server with support for
      multiple backends including llama.cpp, FastFlowLM, and RyzenAI.
    </p>
  </description>
  <launchable type="desktop-id">lemonade.desktop</launchable>
  <url type="homepage">https://lemonade-server.ai/</url>
  <provides>
    <binary>lemond</binary>
    <binary>lemonade-app</binary>
  </provides>
</component>
EOF

# --- Tauri app ---
# The tauri build --no-bundle command produces a single binary in the target directory.
install -Dpm 0755 %{upstream}/src/app/src-tauri/target/release/lemonade-app \
    %{buildroot}%{_bindir}/lemonade-app

# --- Tray desktop entry ---
# No upstream .desktop file for lemonade-tray; write it inline.
# Users can configure lemonade-tray to start at login via their DE's autostart UI.
install -Dpm 0644 /dev/stdin \
    %{buildroot}%{_datadir}/applications/lemonade-tray.desktop << 'EOF'
[Desktop Entry]
Type=Application
Name=Lemonade Tray
Comment=Lemonade Server system tray
Icon=ai.lemonade_server.Lemonade
Exec=lemonade-tray
Terminal=false
Categories=Utility;
EOF

# --- System user home directory ---
# /var/lib/lemonade is the lemond service WorkingDirectory (StateDirectory).
install -dm 0750 %{buildroot}%{_sharedstatedir}/lemonade

# --- Embedded server subpackage installation ---
EMBED_DIR=$(find %{upstream}/%{_vpath_builddir} -maxdepth 1 -type d -name "lemonade-embeddable-*" | head -n 1)
install -dm 0755 %{buildroot}%{_libexecdir}/lemonade-embedded
install -Dpm 0755 ${EMBED_DIR}/lemond %{buildroot}%{_libexecdir}/lemonade-embedded/lemond

install -dm 0755 %{buildroot}%{_libexecdir}/lemonade-embedded/resources
install -Dpm 0644 ${EMBED_DIR}/resources/* %{buildroot}%{_libexecdir}/lemonade-embedded/resources/

%pre server
# Create the lemonade system user/group from the sysusers.d config.
# No-op on F42+ (rpm creates it from the packaged sysusers.d file instead).
%sysusers_create_compat %{upstream}/data/lemonade.sysusers

%post server
%systemd_post lemond.service
%systemd_user_post lemond.service

# 1. Handle migration from upstream CPack home directory (/opt/var/lib/lemonade)
if getent passwd lemonade >/dev/null; then
    CURRENT_HOME=$(getent passwd lemonade | cut -d: -f6)
    if [ "$CURRENT_HOME" = "/opt/var/lib/lemonade" ]; then
        # Change home directory path without moving files (avoids failure if target exists)
        usermod -d %{_sharedstatedir}/lemonade lemonade || true
        # Manually migrate files safely
        if [ -d "/opt/var/lib/lemonade" ]; then
            mkdir -p %{_sharedstatedir}/lemonade
            cp -a /opt/var/lib/lemonade/. %{_sharedstatedir}/lemonade/ 2>/dev/null || true
            rm -rf /opt/var/lib/lemonade || true
        fi
    fi
fi

# Ensure state directory exists and has correct ownership
mkdir -p %{_sharedstatedir}/lemonade
chown lemonade:lemonade %{_sharedstatedir}/lemonade
chmod 0750 %{_sharedstatedir}/lemonade

# 2. Migrate existing user cache structure if upgrading from previous versions
if [ -d "%{_sharedstatedir}/lemonade/.cache/lemonade" ]; then
    find "%{_sharedstatedir}/lemonade/.cache/lemonade" -mindepth 1 -maxdepth 1 -exec mv -t "%{_sharedstatedir}/lemonade" {} + || true
    rmdir "%{_sharedstatedir}/lemonade/.cache/lemonade" || true
fi

if [ -d "%{_sharedstatedir}/lemonade/.cache/huggingface" ]; then
    if [ ! -d "%{_sharedstatedir}/lemonade/huggingface" ]; then
        mv "%{_sharedstatedir}/lemonade/.cache/huggingface" "%{_sharedstatedir}/lemonade/huggingface" || true
    fi
    rmdir "%{_sharedstatedir}/lemonade/.cache" 2>/dev/null || true
fi

# Ensure ownership and permissions are correct after file movements
chown -R lemonade:lemonade %{_sharedstatedir}/lemonade
chmod 0750 %{_sharedstatedir}/lemonade
find %{_sharedstatedir}/lemonade -type d -exec chmod 0750 {} + 2>/dev/null || true
find %{_sharedstatedir}/lemonade -type f -exec chmod 0640 {} + 2>/dev/null || true

# 3. Add lemonade user to render and video groups for hardware acceleration
if getent group render >/dev/null; then
    usermod -a -G render lemonade || true
fi
if getent group video >/dev/null; then
    usermod -a -G video lemonade || true
fi

%preun server
%systemd_preun lemond.service
%systemd_user_preun lemond.service

%postun server
%systemd_postun_with_restart lemond.service
%systemd_user_postun_with_restart lemond.service

%post server-embedded
ln -sf %{_libexecdir}/lemonade-embedded/lemond %{_bindir}/lemond

%postun server-embedded
if [ $1 -eq 0 ]; then
    rm -f %{_bindir}/lemond
fi

%check
appstream-util validate-relax --nonet %{buildroot}%{_datadir}/metainfo/lemonade.appdata.xml
appstream-util validate-relax --nonet %{buildroot}%{_datadir}/metainfo/ai.lemonadeserver.webapp.metainfo.xml
desktop-file-validate %{buildroot}%{_datadir}/applications/lemonade.desktop
desktop-file-validate %{buildroot}%{_datadir}/applications/lemonade-tray.desktop
desktop-file-validate %{buildroot}%{_datadir}/applications/lemonade-web.desktop


%files

%files server
%license %{upstream}/LICENSE
%doc %{upstream}/README.md
%{_bindir}/lemond
%{_mandir}/man1/lemond.1*
%{_datadir}/lemonade/
%{_datadir}/lemonade-server/
%{_datadir}/icons/hicolor/scalable/apps/ai.lemonade_server.Lemonade.svg
%config(noreplace) %{_sysconfdir}/default/lemond
%{_unitdir}/lemond.service
%{_userunitdir}/lemond.service
%{_sysusersdir}/lemonade.conf
%dir %{_sharedstatedir}/lemonade

%files server-embedded
%license %{upstream}/LICENSE
%doc %{upstream}/README.md
%{_libexecdir}/lemonade-embedded/
%ghost %{_bindir}/lemond
%{_mandir}/man1/lemond.1*

%files cli
%license %{upstream}/LICENSE
%{_bindir}/lemonade
%{_mandir}/man1/lemonade.1*

%files tray
%{_bindir}/lemonade-tray
%{_datadir}/applications/lemonade-tray.desktop

%files desktop
%{_bindir}/lemonade-app
%{_datadir}/applications/lemonade.desktop
%{_datadir}/metainfo/lemonade.appdata.xml
%{_datadir}/pixmaps/lemonade-app.svg

%files web
%{_bindir}/lemonade-web
%{_bindir}/lemonade-web-app
%{_datadir}/applications/lemonade-web.desktop
%{_datadir}/metainfo/ai.lemonadeserver.webapp.metainfo.xml

%changelog
* Wed Aug 26 2026 Arun Babu Neelicattu <arun.neelicattu@gmail.com> 11.8.0-1
- Bump version to 11.8.0, drop upstreamed patch, and update spec
  (arun.neelicattu@gmail.com)

* Wed Aug 19 2026 Arun Babu Neelicattu <arun.neelicattu@gmail.com> 11.7.0-1
- Bump version to 11.7.0 and update submodule (arun.neelicattu@gmail.com)

* Sat Aug 15 2026 Arun Babu Neelicattu <arun.neelicattu@gmail.com> 11.6.0-1
- Bump version to 11.6.0 and update submodule (arun.neelicattu@gmail.com)
- Revert "spec: add explicit libgomp dependency for llama-server runtime
  backend" (arun.neelicattu@gmail.com)
- spec: add explicit libgomp dependency for llama-server runtime backend
  (arun.neelicattu@gmail.com)
- spec: optimize npm install flags and Cargo build parallelism
  (arun.neelicattu@gmail.com)
- spec: add explicit gzip and xz dependencies for backend extraction
  (arun.neelicattu@gmail.com)
- feat: add lemonade-server-embedded subpackage (arun.neelicattu@gmail.com)

* Thu Aug 06 2026 Arun Babu Neelicattu <arun.neelicattu@gmail.com> 11.5.2-1
- Bump version to 11.5.2 and update submodule (arun.neelicattu@gmail.com)

* Thu Jul 30 2026 Arun Babu Neelicattu <arun.neelicattu@gmail.com> 11.5.1-1
- Bump version to 11.5.1 and update submodule (arun.neelicattu@gmail.com)

* Sat Jul 25 2026 Arun Babu Neelicattu <arun.neelicattu@gmail.com> 11.5.0-2
- spec: fix CLI HTTPS support when built with system cpp-httplib
  (arun.neelicattu@gmail.com)

* Thu Jul 23 2026 Arun Babu Neelicattu <arun.neelicattu@gmail.com> 11.5.0-1
- Bump version to 11.5.0 and update submodule (arun.neelicattu@gmail.com)

* Fri Jul 17 2026 Arun Babu Neelicattu <arun.neelicattu@gmail.com> 11.0.0-2
- spec: Add unzip, tar, and pciutils runtime dependencies to server subpackage
  (arun.neelicattu@gmail.com)
- chore: add bump-lemonade-version skill to workspace
  (arun.neelicattu@gmail.com)

* Thu Jul 16 2026 Arun Babu Neelicattu <arun.neelicattu@gmail.com> 11.0.0-1
- Bump version to 11.0.0 and update submodule (arun.neelicattu@gmail.com)
- docs: document lemonade-cli subpackage in README (arun.neelicattu@gmail.com)

* Wed Jul 08 2026 Arun Babu Neelicattu <arun.neelicattu@gmail.com> 10.10.0-1
- update to v10.10.0 (arun.neelicattu@gmail.com)

* Fri Jul 03 2026 Arun Babu Neelicattu <arun.neelicattu@gmail.com> 10.9.0-3
- spec: split lemonade-cli into its own subpackage (arun.neelicattu@gmail.com)

* Thu Jul 02 2026 Arun Babu Neelicattu <arun.neelicattu@gmail.com> 10.9.0-2
- spec: implement paths config, post-install migrations, and guidelines fixes
  (arun.neelicattu@gmail.com)

* Wed Jul 01 2026 Arun Babu Neelicattu <arun.neelicattu@gmail.com> 10.9.0-1
- update to v10.9.0 (arun.neelicattu@gmail.com)

* Fri Jun 26 2026 Arun Babu Neelicattu <arun.neelicattu@gmail.com> 10.8.1-1
- update to v10.8.1 (arun.neelicattu@gmail.com)

* Wed Jun 17 2026 Arun Babu Neelicattu <arun.neelicattu@gmail.com> 10.8.0-1
- update to v10.8.0 (arun.neelicattu@gmail.com)

* Thu Jun 11 2026 Arun Babu Neelicattu <arun.neelicattu@gmail.com> 10.7.0-1
- update to v10.7.0 (arun.neelicattu@gmail.com)

* Fri May 22 2026 Arun Babu Neelicattu <arun.neelicattu@gmail.com> 10.6.0-1
- update to v10.6.0 (arun.neelicattu@gmail.com)
- spec: Fedora packaging guideline compliance (COPR-targeted batch)
  (arun.neelicattu@gmail.com)

* Tue May 19 2026 Arun Babu Neelicattu <arun.neelicattu@gmail.com> 10.5.1-1
- update to v10.5.1 (arun.neelicattu@gmail.com)
- spec: drop lemonade-server.service migration from %%post
  (arun.neelicattu@gmail.com)

* Sun May 17 2026 Arun Babu Neelicattu <arun.neelicattu@gmail.com> 10.5.0-1
- update to v10.5.0 (arun.neelicattu@gmail.com)
- feat: add lemond user systemd service (arun.neelicattu@gmail.com)

* Wed May 13 2026 Arun Babu Neelicattu <arun.neelicattu@gmail.com> 10.4.0-1
- update to v10.4.0 (arun.neelicattu@gmail.com)

* Wed May 13 2026 Arun Babu Neelicattu <arun.neelicattu@gmail.com> 10.3.0-2
- spec: add upgrade migration for package rename and service rename
  (arun.neelicattu@gmail.com)
- docs: update README for v10.3.0 packaging (arun.neelicattu@gmail.com)

* Wed May 13 2026 Arun Babu Neelicattu <arun.neelicattu@gmail.com>
- spec: add upgrade migration for package rename and service rename
  (arun.neelicattu@gmail.com)
- docs: update README for v10.3.0 packaging (arun.neelicattu@gmail.com)

* Tue May 12 2026 Arun Babu Neelicattu <arun.neelicattu@gmail.com> 10.3.0-1
- chore: add .gitignore for build artifacts (arun.neelicattu@gmail.com)
- spec: packaging overhaul for v10.3.0 Tauri migration
  (arun.neelicattu@gmail.com)
- update to v10.3.0, migrate desktop app from Electron to Tauri
  (arun.neelicattu@gmail.com)
- fix: use %%{SOURCE1} for httplib patch in spec file
  (arun.neelicattu@gmail.com)
- update to v10.1.0 (arun.neelicattu@gmail.com)

* Mon May 04 2026 Arun Babu Neelicattu <arun.neelicattu@gmail.com> 10.3.0-1
- update to 10.3.0 (arun.neelicattu@gmail.com)
- migrate from Electron to Tauri for the desktop app (arun.neelicattu@gmail.com)
- drop Electron dependencies (arun.neelicattu@gmail.com)

* Mon Mar 30 2026 Arun Babu Neelicattu <arun.neelicattu@gmail.com> 10.0.1-5
- fix: license and doc files are under upstream submodule directory
  (arun.neelicattu@gmail.com)

* Mon Mar 30 2026 Arun Babu Neelicattu <arun.neelicattu@gmail.com> 10.0.1-4
- fix: cmake_install must run from within upstream source directory
  (arun.neelicattu@gmail.com)

* Mon Mar 30 2026 Arun Babu Neelicattu <arun.neelicattu@gmail.com> 10.0.1-3
- fix: vendor upstream source via submodule for COPR compatibility
  (arun.neelicattu@gmail.com)

* Mon Mar 30 2026 Arun Babu Neelicattu <arun.neelicattu@gmail.com> 10.0.1-2
- fix(tito): use correct builder (arun.neelicattu@gmail.com)
- fix: lemonade-tray.service should use lemonade-tray binary
  (arun.neelicattu@gmail.com)
- docs: document lemonade-tray as a standalone install option
  (arun.neelicattu@gmail.com)

* Mon Mar 30 2026 Arun Babu Neelicattu <arun.neelicattu@gmail.com> 10.0.1-1
- spec: update to 10.0.1 (arun.neelicattu@gmail.com)
- Automatic commit of package [lemonade] minor release [9.4.1-2].
  (arun.neelicattu@gmail.com)

* Sun Mar 01 2026 Arun Babu Neelicattu <arun.neelicattu@gmail.com> 9.4.1-2
- 

* Sun Mar 01 2026 Arun Babu Neelicattu <arun.neelicattu@gmail.com> 9.4.1-1
- update lemonade submodule to v9.4.1 (arun.neelicattu@gmail.com)

* Fri Feb 27 2026 Arun Babu Neelicattu <arun.neelicattu@gmail.com> 9.3.4-6
- fix: tray systemd unit cannot depend on lemonade-server
  (arun.neelicattu@gmail.com)

* Fri Feb 27 2026 Arun Babu Neelicattu <arun.neelicattu@gmail.com> 9.3.4-5
- spec: fix autosetup after rename (arun.neelicattu@gmail.com)

* Fri Feb 27 2026 Arun Babu Neelicattu <arun.neelicattu@gmail.com> 9.3.4-4
- spec: fix incorrect source after rename (arun.neelicattu@gmail.com)

* Fri Feb 27 2026 Arun Babu Neelicattu <arun.neelicattu@gmail.com> 9.3.4-3
- tito: update tagger (arun.neelicattu@gmail.com)

* Fri Feb 27 2026 Arun Babu Neelicattu <arun.neelicattu@gmail.com> 9.3.4-2
- new package built with tito

* Thu Feb 26 2026 Arun Neelicattu <arun.neelicattu@gmail.com> - 9.3.4-1
Initial package source
