Name:       jellyseerr
Version:    2.5.0
Release:    1%{?dist}
Summary:    Fork of overseerr for jellyfin support

License:    MIT
URL:        https://github.com/Fallenbagel/jellyseerr
Source:     https://github.com/Fallenbagel/jellyseerr/archive/refs/tags/v%{version}.tar.gz

# Use this to make sure the necessary macros are available like _unitdir
BuildRequires: systemd
BuildRequires: systemd-rpm-macros
BuildRequires: tar
BuildRequires: gzip
BuildRequires: pnpm < 10

Requires: pnpm < 10

Requires(post):    systemd
Requires(preun):   systemd
Requires(postun):  systemd

# Turn off debugging
%global debug_package %{nil}
# Turn off scanning node_modules for dependencies
%global __requires_exclude_from ^.*node_modules/.*$

# Tells RPM not to fail the build if it finds binaries in noarch packages
%global _binaries_in_noarch_packages_terminate_build 0

# Tells the shebang mangler script to skip files in node_modules
%global __brp_mangle_shebangs_exclude_from node_modules

%description
Jellyseerr is a free and open source software application for managing requests for your media library. It is a a fork of Overseerr built to bring support for Jellyfin & Emby media servers!


%prep
%autosetup


%build
PATH=%{_builddir}/%{name}-%{version}/node_modules/.bin/:${PATH}

# Install packages
if ! CYPRESS_INSTALL_BINARY=0 pnpm install --frozen-lockfile; then
    cat /etc/resolv.conf
    echo "You need to enable networking for the build of this package"
    exit 1
fi

# Compile everything
pnpm build

# Prune unneeded deps
pnpm prune --prod --ignore-scripts

# Clean out un-needed files
rm -rf ./src ./server ./.next/cache

# Create env file
echo PORT=5055 > ./jellyseerr.conf
echo HOST=127.0.0.1 >> ./jellyseerr.conf

# Create systemd file
cat <<EOT >> %{name}.service
[Unit]
Description=Jellyseerr Service
Wants=network-online.target
After=network-online.target

[Service]
EnvironmentFile=%{_sysconfdir}/%{name}/jellyseerr.conf
Environment=NODE_ENV=production
Type=exec
Restart=on-failure
WorkingDirectory=%{_datadir}/%{name}
ExecStart=pnpm start

[Install]
WantedBy=multi-user.target
EOT

%install
rm -rf %{buildroot}

mkdir -m 755 -p %{buildroot}%{_datadir}/%{name}
mkdir -m 755 -p %{buildroot}%{_sysconfdir}/%{name}

# Install the config file where the systemd unit expects it
install -p -D -m 0644 jellyseerr.conf %{buildroot}%{_sysconfdir}/%{name}/

# Install systemd unit file
install -p -D -m 0644 %{name}.service %{buildroot}%{_unitdir}/%{name}.service

mv package.json %{buildroot}%{_datadir}/%{name}/
mv pnpm-lock.yaml %{buildroot}%{_datadir}/%{name}/
mv *.js %{buildroot}%{_datadir}/%{name}/
mv *.ts %{buildroot}%{_datadir}/%{name}/
mv node_modules %{buildroot}%{_datadir}/%{name}/
mv dist/ %{buildroot}%{_datadir}/%{name}/
mv public/ %{buildroot}%{_datadir}/%{name}/
mv .next/ %{buildroot}%{_datadir}/%{name}/

%post
%systemd_post jellyseerr.service

%preun
%systemd_preun jellyseerr.service


%postun
%systemd_postun_with_restart jellyseerr.service

# TODO Get perms right
%files
%config(noreplace) %{_sysconfdir}/%{name}
%{_datadir}/%{name}
%{_unitdir}/%{name}.service


%changelog
* Tue Mar 11 2025 Brian J. Murrell <brian@interlinx.bc.ca> - 2.5.0-1
- Update to 2.5.0
- drop mv overseerr-api.yml %%{buildroot}%%{_datadir}/%%{name}/

* Mon Jan 20 2025 Brian J. Murrell <brian@interlinx.bc.ca> - 2.3.0-3
- Use RPM packaged pnpm instead of installing with npm

* Mon Jan 20 2025 Brian J. Murrell <brian@interlinx.bc.ca> - 2.3.0-2
- Restrict pnpm version < 10

* Thu Jan 16 2025 Brian J. Murrell <brian@interlinx.bc.ca> - 2.3.0-1
- Update to 2.3.0
- Bump nodejs verison up to 22
- Fix up node binary name in installed pnpm

* Mon Dec 30 2024 Brian J. Murrell <brian@interlinx.bc.ca> - 2.2.2-2
- BR: nodejs20-npm and use npm-20 for fedora > 40