%bcond_with systemd

Name:           postsrsd
Version:        1.11
Release:        1%{?_dist_release}%{?with_systemd:.systemd}
Summary:        Sender Rewriting Scheme (SRS) provider
Group:          servers
Vendor:         Project Vine
Distribution:   Vine Linux

License:        GPLv2+
URL:            https://github.com/roehling/postsrsd
Source0:        https://github.com/roehling/%{name}/archive/%{version}/%{name}-%{version}.tar.gz
Source1:        postsrsd.conf
Source2:        postsrsd.init

BuildRequires:  gcc
BuildRequires:  cmake
BuildRequires:  help2man

%if %{with systemd}
%{?systemd_requires}
%else
Requires(post): /sbin/chkconfig
Requires(preun): /sbin/chkconfig
Requires(preun): /sbin/service
Requires(postun): /sbin/service
%endif
Requires:       postfix

%description
PostSRSd provides the Sender Rewriting Scheme (SRS) via TCP-based lookup tables for Postfix.
SRS is needed if your mail server acts as forwarder.


%debug_package


%prep
%autosetup -n %{name}-%{version}
%cmake \
%if %{with systemd}
	-DINIT_FLAVOR=systemd \
%else
	-DINIT_FLAVOR=sysv-redhat \
%endif
	-DGENERATE_SRS_SECRET=OFF \
	-DUSE_SELINUX=OFF \
	%{nil}


%build
%cmake_build


%install
rm -rf %{buildroot}
%cmake_install

%if %{with systemd}
mkdir -p %{buildroot}/%{_unitdir}
mv -f %{buildroot}/%{_sysconfdir}/systemd/system/%{name}.service \
	%{buildroot}%{_unitdir}/
mkdir -p %{buildroot}%{_tmpfilesdir}/
install -m644 %{SOURCE1} %{buildroot}%{_tmpfilesdir}/
%else
rm -f %{buildroot}/%{_sysconfdir}/init.d/postsrsd
mkdir -p %{buildroot}/%{_initdir}
install -m755 %{SOURCE2} %{buildroot}%{_initdir}/postsrsd
%endif

# %%ghost file requires it is present in the build root
touch %{buildroot}/%{_sysconfdir}/postsrsd.secret

# chroot directory
# (also move default config which is in the way)
sed -i 's/^CHROOT=.*/CHROOT=\/run\/postsrsd/' %{buildroot}/%{_sysconfdir}/default/%{name}


%files
%license LICENSE
%ghost %{_sysconfdir}/postsrsd.secret
%config(noreplace) %{_sysconfdir}/default/%{name}
%if %{with systemd}
%{_datadir}/postsrsd/postsrsd-systemd-launcher
%{_unitdir}/postsrsd.service
%{_tmpfilesdir}/*
%else
%{_initdir}/postsrsd
%exclude %{_datadir}/postsrsd/postsrsd-systemd-launcher
%endif
%{_sbindir}/postsrsd
%{_docdir}/%{name}
%{_mandir}/man8/postsrsd.8.gz


%post
[ -f %{_sysconfdir}/postsrsd.secret ] || dd if=/dev/urandom bs=18 count=1 2>/dev/null | base64 >%{_sysconfdir}/postsrsd.secret
%if %{with systemd}
%systemd_post %{name}.service
%else
/sbin/chkconfig --add postsrsd
%endif

%preun
%if %{with systemd}
%systemd_preun %{name}.service
%else
if [ $1 -eq 0 -o -x /bin/systemctl ]; then
	/sbin/service %{name} stop
	/sbin/chkconfig --del %{name}
fi
%endif

%postun
%if %{with systemd}
%systemd_postun_with_restart %{name}.service
%else
if [ "$1" -ge "1" -a ! -x /bin/systemctl ]; then
  /sbin/service %{name} condrestart 
fi
%endif


%changelog
* Wed Jul 07 2021 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> - 1.11-1
- new upstream release.

* Wed Dec 23 2020 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> - 1.10-1
- new upstream release.

* Sat Apr 18 2020 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> - 1.6-2
- made to create chroot directory at boot.

* Thu Apr 09 2020 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> - 1.6-1
- new upstream release.
- added systemd support (disabled as default).

* Sat Jan 26 2019 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> - 1.5-1
- initial build for Vine Linux.
- new upstream release.

* Fri Jul 13 2018 Fedora Release Engineering <releng@fedoraproject.org> - 1.4-11.20170118gita77bf99
- Rebuilt for https://fedoraproject.org/wiki/Fedora_29_Mass_Rebuild

* Fri Feb 09 2018 Fedora Release Engineering <releng@fedoraproject.org> - 1.4-10.20170118gita77bf99
- Rebuilt for https://fedoraproject.org/wiki/Fedora_28_Mass_Rebuild

* Wed Oct 04 2017 Marc Dequènes (Duck) <duck@redhat.com> - 1.4-9.20170118gita77bf99
- use the %%ghost feature to ensure the secret file is owned by the package
- it is then not necessary to handle its removal in %%postun

* Thu Sep 28 2017 Marc Dequènes (Duck) <duck@redhat.com> - 1.4-8.20170118gita77bf99
- Thanks Matthias Runge Mauchin for the review
- break description line too long
- build dependency on gcc is not needed

* Wed Sep 27 2017 Marc Dequènes (Duck) <duck@redhat.com> - 1.4-7.20170118gita77bf99
- make the changelog more readable
- stop recreating buildroot, it is made clean already

* Wed Aug 23 2017 Marc Dequènes (Duck) <duck@redhat.com> - 1.4-6.20170118gita77bf99
- remove unnecessary Requires on make
- use _sysconfdir macro
- use name macro when it makes sense
- remove unnecessary %%doc as the buildsys already populates docdir

* Tue Aug 22 2017 Marc Dequènes (Duck) <duck@redhat.com> - 1.4-5.20170118gita77bf99
- remove %%clean section, not needed in Fedora

* Tue Aug 22 2017 Marc Dequènes (Duck) <duck@redhat.com> - 1.4-4.20170118gita77bf99
- don't remove secret file during upgrade
- start service at the end of post scriptlet
- improve SELinux rules handling (now requires a running SELinux)

* Tue Aug 22 2017 Marc Dequènes (Duck) <duck@redhat.com> - 1.4-3.20170118gita77bf99
- Thanks Robert-André Mauchin for the review
- snapshot is packaged to get this necessary fix: https://github.com/roehling/postsrsd/pull/65
- fixed version
- fixed source URL
- use macros for standard paths and build steps
- add missing systemd scriptlets
- specify doc and license files
- remove unnecessary Requires on base64
- remove Group information unsupported in Fedora

* Fri Apr 14 2017 Marc Dequènes (Duck) <duck@redhat.com> - 1.4-2
- create /etc/postsrsd.secret if missing
- move systemd config into directory for packages
- move chroot directory into /run
- autocreate chroot directory

* Thu Mar 30 2017 Marc Dequènes (Duck) <duck@redhat.com> - 1.4-1
- initial packaging

