##
##  opendkim.spec -- OpenPKG RPM Package Specification
##  Copyright (c) 2000-2025 OpenPKG Project <http://openpkg.org/>
##
##  Permission to use, copy, modify, and distribute this software for
##  any purpose with or without fee is hereby granted, provided that
##  the above copyright notice and this permission notice appear in all
##  copies.
##
##  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
##  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
##  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
##  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
##  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
##  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
##  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
##  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
##  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
##  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
##  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
##  SUCH DAMAGE.
##

#   package information
Name:         opendkim
Summary:      DKIM Sender Authentication for Mail
URL:          http://www.opendkim.org/
Vendor:       OpenDKIM Project
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Mail
License:      BSD
Version:      2.10.3
Release:      20150513

#   package options
%option       with_fsl  yes

#   list of sources
Source0:      http://downloads.sourceforge.net/opendkim/opendkim-%{version}.tar.gz
Source1:      fsl.opendkim
Source2:      rc.opendkim
Patch0:       opendkim.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101
PreReq:       OpenPKG, openpkg >= 20160101
BuildPreReq:  milter, openssl
PreReq:       milter, openssl
%if "%{with_fsl}" == "yes"
BuildPreReq:  fsl
PreReq:       fsl
%endif

%description
    OpenDKIM is an implementation of the DomainKeys Identified Mail
    (DKIM) sender authentication system as proposed by the IETF RFC4871
    standard. The OpenDKIM package consists of a library that implements
    the DKIM service and a milter-based filter application that can plug
    in to any milter-aware MTA to provide that service to sufficiently
    recent Sendmail and Postfix MTAs and other MTAs that support the
    MILTER protocol.

%track
    prog opendkim = {
        version   = %{version}
        url       = http://sourceforge.net/projects/opendkim/files/
        regex     = opendkim-(\d+\.\d+\.\d+)\.tar\.gz
    }

%prep
    %setup -q
    %patch -p0

%build
    #   configure package
    CC="%{l_cc}" \
    CFLAGS="%{l_cflags -O}" \
    CPPFLAGS="%{l_cppflags}" \
    LDFLAGS="%{l_ldflags} %{l_fsl_ldflags}" \
    LIBS="%{l_fsl_libs}" \
    ./configure \
        --prefix=%{l_prefix} \
        --mandir=%{l_prefix}/man \
        --with-openssl=%{l_prefix} \
        --with-milter=%{l_prefix} \
        --disable-shared

    #   build package
    %{l_make} %{l_mflags -O}

%install
    #   install package
    %{l_make} %{l_mflags} install AM_MAKEFLAGS="DESTDIR=$RPM_BUILD_ROOT"

    #   strip down and post-adjust installation
    strip $RPM_BUILD_ROOT%{l_prefix}/sbin/* >/dev/null 2>&1 || true
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/share/opendkim
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/opendkim
    mv  $RPM_BUILD_ROOT%{l_prefix}/share/doc/opendkim/opendkim.conf.sample \
        $RPM_BUILD_ROOT%{l_prefix}/etc/opendkim/opendkim.conf
    %{l_shtool} subst \
        -e 's;^\([A-Z]\);#\1;' \
        $RPM_BUILD_ROOT%{l_prefix}/etc/opendkim/opendkim.conf
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/share

    #   install run-command script
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE rc.opendkim} $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d/

    #   install OSSP fsl configuration
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/fsl
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE fsl.opendkim} $RPM_BUILD_ROOT%{l_prefix}/etc/fsl/

    #   create run-time directory
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/var/opendkim \
        $RPM_BUILD_ROOT%{l_prefix}/var/opendkim/opendkim.tmp

    #   determine installation files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std} \
        '%config %{l_prefix}/etc/fsl/*' \
        '%config %{l_prefix}/etc/opendkim/*' \
        '%attr(755,%{l_rusr},%{l_mgrp}) %{l_prefix}/var/opendkim/*' \
        '%attr(755,%{l_rusr},%{l_mgrp}) %{l_prefix}/var/opendkim'

%files -f files

%clean

%post
    #   after upgrade, restart service
    if [ $1 -eq 1 ]; then
        if [ ! -f $RPM_INSTALL_PREFIX/etc/opendkim/mail.private ]; then
            $RPM_INSTALL_PREFIX/sbin/opendkim-genkey \
                -s mail -d example.com -D $RPM_INSTALL_PREFIX/etc/opendkim -b 1024
        fi
    elif [ $1 -eq 2 ]; then
        eval `%{l_rc} opendkim status 2>/dev/null`
        [ ".$opendkim_active" = .yes ] && %{l_rc} opendkim restart
    fi
    exit 0

%preun
    #   before erase, stop service and remove log files
    if [ $1 -eq 0 ]; then
        %{l_rc} opendkim stop 2>/dev/null
        rm -f $RPM_INSTALL_PREFIX/etc/opendkim/mail.*          >/dev/null 2>&1 || true
        rm -f $RPM_INSTALL_PREFIX/var/opendkim/opendkim.log    >/dev/null 2>&1 || true
        rm -f $RPM_INSTALL_PREFIX/var/opendkim/opendkim.pid    >/dev/null 2>&1 || true
        rm -f $RPM_INSTALL_PREFIX/var/opendkim/opendkim.tmp/*  >/dev/null 2>&1 || true
    fi
    exit 0

