%global systemd (0%{?fedora} >= 18) || (0%{?rhel} >= 7)
%global systemd_runtimedir (0%{?fedora} >= 21) || (0%{?rhel} >= 8)
%global tmpfiles ((0%{?fedora} >= 15) || (0%{?rhel} >= 7)) && !%{systemd_runtimedir}

Summary: An open source library and milter for providing ARC service
Name: openarc
Version: @VERSION@
Release: 1%{?dist}
License: BSD and Sendmail
URL: https://github.com/flowerysong/OpenARC
Source0: openarc-%{version}.tar.gz

BuildRequires: gcc
BuildRequires: libtool
BuildRequires: make
BuildRequires: python3-devel
BuildRequires: pkgconfig(jansson)
BuildRequires: pkgconfig(libbsd)
BuildRequires: pkgconfig(openssl)
BuildRequires: pkgconfig(libidn2)

# sendmail-devel renamed for F25+
%if 0%{?fedora} > 25
BuildRequires: sendmail-milter-devel
%else
BuildRequires: sendmail-devel
%endif

Requires: lib%{name}%{?_isa} = %{version}-%{release}
Requires: libopenarc = %{version}-%{release}
Requires(pre): shadow-utils
%if %systemd
# Required for systemd
%{?systemd_requires}
BuildRequires: systemd
%else
# Required for SysV
Requires(post): chkconfig
Requires(preun): chkconfig, initscripts
%endif


%description
OpenARC is a community effort to develop and maintain a C library
for producing ARC-aware applications and an open source milter for
providing ARC service through milter-enabled MTAs.

%package -n libopenarc
Summary: An open source ARC library

%description -n libopenarc
This package contains the library files required for running services built
using libopenarc.

%package -n libopenarc-devel
Summary: Development files for libopenarc
Requires: lib%{name}%{?_isa} = %{version}-%{release}

%description -n libopenarc-devel
This package contains the static libraries, headers, and other support files
required for developing applications against libopenarc.

%package -n openarc-tools
Summary: Utilities related to the ARC protocol
BuildArch: noarch
Requires: python3 >= 3.7

%description -n openarc-tools
This packages contains utilities that may be useful for people deploying ARC.

%prep
%autosetup -p1

%build
%configure --disable-static
%make_build

%install
%make_install
mkdir -p -m 0700 %{buildroot}%{_sysconfdir}/%{name}
mkdir -p -m 0750 %{buildroot}%{_rundir}/%{name}
rm -r %{buildroot}%{_prefix}/share/doc/openarc
rm %{buildroot}/%{_libdir}/*.la


cat > %{buildroot}%{_sysconfdir}/openarc.conf <<EOF
## See openarc.conf(5) or %{_docdir}/%{name}%{?rhel:-%{version}}/openarc.conf.sample for more
#PidFile %{_rundir}/%{name}/%{name}.pid
Syslog  yes
UserID  openarc:openarc
Socket  local:%{_rundir}/%{name}/%{name}.sock
SignHeaders to,subject,message-id,date,from,mime-version,dkim-signature
PeerList %{_sysconfdir}/%{name}/PeerList
MilterDebug 6
EnableCoredumps yes

## After setting Mode to "sv", create a key and zone file by running
##   # openarc-keygen -D %{_sysconfdir}/openarc/keys -s selector -d `hostname --domain`
## which will create a key in the directory %{_sysconfdir}/openarc/keys
## See man openarc-keygen(1) for more information.
#Mode                    sv
#Canonicalization        relaxed/simple
#Domain                  example.com # change to domain
#Selector                selector # e.g. 20250107
#KeyFile                 %{_sysconfdir}/openarc/keys/selector._domainkey.domain.key
#SignatureAlgorithm rsa-sha256
EOF

# Don't sign or validate connections from localhost
cat > %{buildroot}%{_sysconfdir}/%{name}/PeerList <<EOF
127.0.0.1/32
[::1]/128
EOF
chmod 0640 %{buildroot}%{_sysconfdir}/%{name}/PeerList

%if %systemd
install -d -m 0755 %{buildroot}%{_unitdir}
cat > %{buildroot}%{_unitdir}/%{name}.service <<EOF
[Unit]
Description=Authenticated Receive Chain (ARC) Milter
Documentation=man:%{name}(8) man:%{name}.conf(5)
After=network.target nss-lookup.target syslog.target

[Service]
Type=simple
%if %{systemd_runtimedir}
RuntimeDirectory=%{name}
RuntimeDirectoryMode=0750
%endif
EnvironmentFile=-%{_sysconfdir}/sysconfig/%{name}
ExecStart=/usr/sbin/%{name} -f $OPTIONS
ExecStartPost=/sbin/restorecon -r -F %{_rundir}/%{name}
ExecReload=/bin/kill -USR1 $MAINPID
Restart=on-failure
User=%{name}
Group=%{name}
UMask=0007
ProtectSystem=strict
ProtectHome=true

[Install]
WantedBy=multi-user.target
EOF
%else
mkdir -p %{buildroot}%{_initrddir}
install -m 0755 contrib/init/redhat/%{name} %{buildroot}%{_initrddir}/%{name}
%endif

%if %{tmpfiles}
install -p -d %{buildroot}%{_tmpfilesdir}
cat > %{buildroot}%{_tmpfilesdir}/%{name}.conf <<EOF
D %{_rundir}/%{name} 0750 %{name} %{name} -
EOF
%endif

%pre
if ! getent passwd %{name} >/dev/null 2>&1; then
    %{_sbindir}/useradd -M -d %{_localstatedir}/lib/%{name} -r -s /sbin/nologin %{name}
    if ! getent group %{name} >/dev/null; then
        %{_sbindir}/groupadd %{name}
        %{_sbindir}/usermod -g %{name} %{name}
    fi
    if getent group mail >/dev/null; then
        %{_sbindir}/usermod -G mail %{name}
    fi
fi
exit 0


%post

%if %systemd
%systemd_post %{name}.service
%else
/sbin/chkconfig --add %{name} || :
%endif


%preun
%if %systemd
%systemd_preun %{name}.service
%else
if [ $1 -eq 0 ]; then
    service %{name} stop >/dev/null || :
    /sbin/chkconfig --del %{name} || :
fi
exit 0
%endif

%postun
%if %systemd
%systemd_postun_with_restart %{name}.service
%endif

%ldconfig_scriptlets -n libopenarc

%files
%license LICENSE LICENSE.Sendmail
%doc README.md CHANGELOG.md %{name}/%{name}.conf.sample
%dir %attr(0755,root,%{name}) %{_sysconfdir}/%{name}
%config(noreplace) %attr(0644,root,%{name}) %{_sysconfdir}/%{name}.conf
%config(noreplace) %attr(0440,%{name},%{name}) %{_sysconfdir}/%{name}/PeerList

%if %{tmpfiles}
%{_tmpfilesdir}/%{name}.conf
%endif
%if !%{tmpfiles} && !%{systemd_runtimedir}
%dir %attr(0750,%{name},%{name}) %{_rundir}/%{name}
%endif

%if %{systemd}
%{_unitdir}/%{name}.service
%else
%{_initrddir}/%{name}
%endif
%{_mandir}/man5/openarc.conf.5*
%{_mandir}/man8/openarc.8*
%{_sbindir}/*

%files -n libopenarc
%license LICENSE LICENSE.Sendmail
%defattr(-,root,root)
%{_libdir}/*.so.*

%files -n libopenarc-devel
%{_includedir}/*
%{_libdir}/*.so
%{_libdir}/pkgconfig/*.pc

%files -n openarc-tools
%{_bindir}/openarc-keygen
%{_mandir}/man1/openarc-keygen.1*

%changelog
* %(date "+%a %b %d %Y") (Automated RPM build) - %{version}-%{release}
- See git log for actual changes.
