%global systemd (0%{?fedora} >= 18) || (0%{?rhel} >= 7)
%global tmpfiles (0%{?fedora} >= 15) || (0%{?rhel} >= 7)

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/mskucherawy/OpenARC

BuildRequires: libtool
BuildRequires: pkgconfig(openssl)
BuildRequires: pkgconfig(libbsd)

# 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
Requires(postun): initscripts
%endif

Source0: openarc-%{version}.tar.gz
Prefix: %{_prefix}

%description
The Trusted Domain Project 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.

%prep
%autosetup -p1

%build
%configure --disable-static

make %{?_smp_mflags}
%install
make install DESTDIR=%{buildroot}
mkdir -p %{buildroot}%{_sysconfdir}
mkdir -p -m 0700 %{buildroot}%{_localstatedir}/run/%{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}-%{version}/openarc.conf.sample for more
PidFile %{_localstatedir}/run/openarc/openarc.pid
Syslog  yes
#Umask   002
UserID  openarc:openarc
Socket  inet:8894@localhost

## After setting Mode to "sv", running
## opendkim-genkey -D %{_sysconfdir}/openarc -s key -d `hostname --domain`
## and putting %{_sysconfdir}/openarc
#Canonicalization        relaxed/simple
#Domain                  example.com # change to domain
#Selector                key
#KeyFile                 %{_sysconfdir}/openarc/key.private
#SignatureAlgorithm rsa-sha256
EOF


%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) http://www.trusteddomain.org/%{name}/
After=network.target nss-lookup.target syslog.target

[Service]
Type=forking
PIDFile=%{_localstatedir}/run/%{name}/%{name}.pid
EnvironmentFile=-%{_sysconfdir}/sysconfig/%{name}
ExecStart=/usr/sbin/%{name} $OPTIONS
ExecReload=/bin/kill -USR1 $MAINPID
User=%{name}
Group=%{name}

[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 %{_localstatedir}/run/%{name} 0700 %{name} %{name} -
EOF
%endif

%pre
if ! getent passwd openarc >/dev/null 2>&1; then
    %{_sbindir}/useradd -M -d %{_localstatedir}/lib -r -s /bin/false openarc
    if ! getent group openarc >/dev/null; then
        %{_sbindir}/groupadd openarc
        %{_sbindir}/usermod -g openarc openarc
    fi
    if getent group mail >/dev/null; then
        %{_sbindir}/usermod -G mail openarc
    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

%post -n libopenarc -p /sbin/ldconfig

%postun -n libopenarc -p /sbin/ldconfig


%files
%defattr(-,root,root)
%doc LICENSE LICENSE.Sendmail README RELEASE_NOTES
%config(noreplace) %{_sysconfdir}/openarc.conf

%if %{tmpfiles}
%{_tmpfilesdir}/%{name}.conf
%else
%dir %attr(-,%{name},%{name}) %{_localstatedir}/run/%{name}
%endif

%if %{systemd}
%{_unitdir}/%{name}.service
%else
%{_initrddir}/%{name}
%endif
%{_mandir}/*/*
%{_sbindir}/*


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

%files -n libopenarc-devel
%defattr(-,root,root)
%doc LICENSE LICENSE.Sendmail
%{_includedir}/*
%{_libdir}/*.so
%{_libdir}/pkgconfig/*.pc

%changelog
* Sun Jul 23 2017  Matt Domsch <matt@domsch.com> 0.1.0-1
- update to Fedora Packaging Guidelines
