%bcond_with systemd

Summary:          A simple CalDAV (calendar) and CardDAV (contact) server
Name:             radicale
Version:          2.1.12
Release:          1%{?_dist_release}%{?with_systemd:.systemd}
Group:            servers
Vendor:           Project Vine
Distribution:     Vine Linux

License:          GPLv3+
URL:              https://radicale.org
Source0:          https://github.com/Kozea/Radicale/archive/%{version}/%{name}-%{version}.tar.gz
Source1:          %{name}.service
Source2:          %{name}-logrotate
Source7:          %{name}-tmpfiles.conf
Source1000:       %{name}.init

BuildArch:        noarch
BuildRequires:    python3-rpm-macros
BuildRequires:    python3-devel
BuildRequires:    python3-setuptools

Requires:         python3-%{name} = %{version}-%{release}
Requires(pre):    shadow-utils
%if %{with systemd}
BuildRequires:    systemd
%{?systemd_requires}
%else
Requires(post):   chkconfig
Requires(preun):  chkconfig
Requires(preun):  /sbin/service
Requires(postun): /sbin/service
%endif

%description
The Radicale Project is a CalDAV (calendar) and CardDAV (contact) server. It
aims to be a light solution, easy to use, easy to install, easy to configure.
As a consequence, it requires few software dependencies and is pre-configured
to work out-of-the-box.

The Radicale Project runs on most of the UNIX-like platforms (Linux, BSD,
MacOS X) and Windows. It is known to work with Evolution, Lightning, iPhone
and Android clients. It is free and open-source software, released under GPL
version 3.


%package -n python3-%{name}
Summary:          A simple CalDAV (calendar) and CardDAV (contact) server
Group:            programming
Requires:         python3-vobject
Recommends:       python3-bcrypt
Recommends:       python3-passlib
%{?python_provide:%python_provide python3-%{name}}

%description -n python3-%{name}
The Radicale Project is a CalDAV (calendar) and CardDAV (contact) server. It
aims to be a light solution, easy to use, easy to install, easy to configure.
As a consequence, it requires few software dependencies and is pre-configured
to work out-of-the-box.

The Radicale Project runs on most of the UNIX-like platforms (Linux, BSD,
MacOS X) and Windows. It is known to work with Evolution, Lightning, iPhone
and Android clients. It is free and open-source software, released under GPL
version 3.


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


%build
%py3_build


%install
%py3_install

# Install configuration files
mkdir -p %{buildroot}%{_sysconfdir}/%{name}/
install -p -m 640 config %{buildroot}%{_sysconfdir}/%{name}/
install -p -m 644 logging %{buildroot}%{_sysconfdir}/%{name}/
install -p -m 644 rights %{buildroot}%{_sysconfdir}/%{name}/

# Install wsgi file
mkdir -p %{buildroot}%{_datadir}/%{name}
sed -i 's|^#!/usr/bin/env python3$|#!/usr/bin/python3|' radicale.wsgi
sed -i 's|^#!/usr/bin/env python3$|#!/usr/bin/python3|' radicale.fcgi
install -p -m 755 radicale.wsgi %{buildroot}%{_datadir}/%{name}/
install -p -m 755 radicale.fcgi %{buildroot}%{_datadir}/%{name}/

# Create folder where the calendar will be stored
mkdir -p  %{buildroot}%{_sharedstatedir}/%{name}/

%if %{with systemd}
install -D -p -m 644 %{SOURCE1} %{buildroot}%{_unitdir}/%{name}.service
install -D -p -m 644 %{SOURCE7} %{buildroot}%{_tmpfilesdir}/%{name}.conf
%else
install -D -p -m 755 %{SOURCE1000} %{buildroot}%{_initdir}/%{name}
%endif
install -D -p -m 644 %{SOURCE2} %{buildroot}%{_sysconfdir}/logrotate.d/%{name}
mkdir -p %{buildroot}/run/%{name}

mkdir -p %{buildroot}%{_localstatedir}/log/%{name}


%pre
getent group %{name} >/dev/null || groupadd -r %{name}
getent passwd %{name} >/dev/null || \
    useradd -r -g %{name} -d %{_sharedstatedir}/%{name} -s /sbin/nologin \
    -c "Radicale service account" %{name}
exit 0


%post
%if %{with systemd}
%systemd_post %{name}.service
%else
if [ $1 -eq 1 ]; then
  /sbin/chkconfig --add radicale
fi
%endif

%preun
%if %{with systemd}
%systemd_preun %{name}.service
%else
if [ $1 -eq 0 -o /bin/systemctl ]; then
  /sbin/service radicale status >/dev/null 2>&1 && \
    /sbin/service radicale stop ||:
  /sbin/chkconfig --del radicale
fi
%endif

%postun
%if %{with systemd}
%systemd_postun_with_restart %{name}.service 
%else
if [ $1 -gt 0 ]; then
  /sbin/service radicale condrestart ||:
fi
%endif


%files
%license COPYING
%doc README.md NEWS.md
%{_bindir}/%{name}
%dir %{_sysconfdir}/%{name}/
%config(noreplace) %attr(0640, root, %{name}) %{_sysconfdir}/%{name}/config
%config(noreplace) %{_sysconfdir}/%{name}/logging
%config(noreplace) %{_sysconfdir}/%{name}/rights
%config(noreplace) %{_sysconfdir}/logrotate.d/%{name}
%if %{with systemd}
%{_unitdir}/%{name}.service
%{_tmpfilesdir}/%{name}.conf
%else
%{_initdir}/%{name}
%endif
%dir %attr(750, %{name}, %{name}) %{_localstatedir}/log/%{name}
%dir %attr(750, %{name}, %{name}) %{_sharedstatedir}/%{name}/
%{_datadir}/%{name}/*
%ghost %dir %attr(755, %{name}, %{name}) /run/%{name}

%files -n python3-%{name}
%license COPYING
%{python3_sitelib}/%{name}
%{python3_sitelib}/Radicale-*.egg-info


%changelog
* Sat Aug 15 2020 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 2.1.12-1
- new upstream release.

* Tue May 05 2020 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 2.1.11-2
- added systemd support (disabled as default).

* Thu Feb 14 2019 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 2.1.11-1
- new upstream release.

* Wed Jan 31 2018 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 2.1.8-1
- new upstream release.

* Thu Oct 22 2015 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 1.0.1-1
- new upstream release.

* Thu Apr 03 2014 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 0.9-1.beta1
- new upstream release.

* Sun Aug 11 2013 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 0.8-1
- new upstream release.

* Thu Oct 13 2011 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 0.6.2-1
- new upstream release.

* Tue Aug 23 2011 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 0.6-1
- new upstream release.

* Sun May 22 2011 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 0.5-1
- initial build.

