##
##  milter.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 version
%define       V_libmilter        8.18.1
%define       V_sendmail_pmilter 1.00
%define       V_mail_milter      0.07

#   package information
Name:         milter
Summary:      Mail Filtering API
URL:          http://www.sendmail.org/doc/sendmail-current/libmilter/docs/
Vendor:       Eric Allman, Todd Vierling
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Mail
License:      BSD/Artistic
Version:      %{V_libmilter}
Release:      20240204

#   package options
%option       with_perl  no

#   list of sources
Source0:      ftp://ftp.sendmail.org/pub/sendmail/sendmail.%{V_libmilter}.tar.gz
Source1:      http://www.cpan.org/authors/id/A/AV/AVAR/Sendmail-PMilter-%{V_sendmail_pmilter}.tar.gz
Source2:      http://download.sourceforge.net/pmilter/Mail-Milter-%{V_mail_milter}.tar.gz
Source3:      milter.pc
Patch0:       milter.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101
PreReq:       OpenPKG, openpkg >= 20160101
%if "%{with_perl}" == "yes"
BuildPreReq:  perl, perl-openpkg
PreReq:       perl
%endif

%description
    MILTER is the mail and SMTP protocol filtering API developed with
    the Sendmail 8 MTA. This package provides Sendmail's original
    client-side C API (libmilter) and optionally the alternative
    client-side Perl APIs (Sendmail::Milter, Sendmail::PMilter,
    Mail::Milter).

%track
    prog milter:libmilter = {
        version   = %{V_libmilter}
        url       = ftp://ftp.sendmail.org/pub/sendmail/
        regex     = sendmail.(\d+\.\d+\.\d+)\.tar\.gz
    }
    prog milter:Sendmail-PMilter = {
        version   = %{V_sendmail_pmilter}
        url       = http://www.cpan.org/authors/id/A/AV/AVAR/
        regex     = Sendmail-PMilter-(__VER__)\.tar\.gz
    }
    prog milter:Mail-Milter = {
        version   = %{V_mail_milter}
        url       = http://sourceforge.net/projects/pmilter/files/
        regex     = Mail-Milter-(__VER__)\.tar\.gz
    }

%prep
    %setup -q -c
    %setup -q -T -D -a 1
    %setup -q -T -D -a 2
    %patch -p0

%build
    #   build C API
    ( cd sendmail-%{V_libmilter}
      extlibs="%{l_fsl_libs} -ldb"
      case "%{l_platform -t}" in
          *-sunos* ) extlibs="$extlibs -lrt" ;;
      esac
      (   echo "define(\`confCC', \`%{l_cc} %{l_cflags}')dnl"
          echo "APPENDDEF(\`confINCDIRS', \`%{l_cppflags}')dnl"
          echo "APPENDDEF(\`confLIBDIRS', \`%{l_ldflags}')dnl"
          echo "APPENDDEF(\`confLIBS', \`$extlibs')dnl"
          echo "define(\`confSTDIO_TYPE', \`portable')dnl"
          echo "APPENDDEF(\`conf_sendmail_ENVDEF', \`-DMILTER=1')dnl"
          echo "APPENDDEF(\`conf_libmilter_ENVDEF', \`-D_FFR_MILTER_ROOT_UNSAFE')dnl"
      ) >devtools/Site/site.config.m4
      ( cd include
        ln -s libmilter milter
      ) || exit $?
      ( cd libmilter
        ./Build
      ) || exit $?
    ) || exit $?

    #   build Perl API
%if "%{with_perl}" == "yes"
    %{l_prefix}/bin/perl-openpkg prepare
    %{l_prefix}/bin/perl-openpkg -d Sendmail-PMilter-%{V_sendmail_pmilter} configure build
    %{l_prefix}/bin/perl-openpkg -d Mail-Milter-%{V_mail_milter} configure build
%endif

%install
    #   install C API
    ( cd sendmail-%{V_libmilter}
      %{l_shtool} mkdir -f -p -m 755 \
          $RPM_BUILD_ROOT%{l_prefix}/include/milter \
          $RPM_BUILD_ROOT%{l_prefix}/lib/milter
      %{l_shtool} install -c -m 644 \
          include/libmilter/*.h \
          $RPM_BUILD_ROOT%{l_prefix}/include/milter/
      %{l_shtool} install -c -m 644 \
          obj.*/libmilter/libmilter.a \
          $RPM_BUILD_ROOT%{l_prefix}/lib/
    ) || exit $?
    libs=`grep "^LDOPTS=" sendmail-%{V_libmilter}/obj.*/libmilter/Makefile | sed -e 's;^LDOPTS=;;'`
    libs="$libs -lmilter"
    case "%{l_platform -t}" in
        *-sunos* ) libs="$libs -lrt" ;;
    esac
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/lib/pkgconfig
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        -e "s;@version@;%{version};" \
        -e "s;@libs@;$libs;" \
        %{SOURCE milter.pc} $RPM_BUILD_ROOT%{l_prefix}/lib/pkgconfig/

    #   install Perl API
%if "%{with_perl}" == "yes"
    %{l_prefix}/bin/perl-openpkg -d Sendmail-PMilter-%{V_sendmail_pmilter} install
    %{l_prefix}/bin/perl-openpkg -d Mail-Milter-%{V_mail_milter} install
    %{l_prefix}/bin/perl-openpkg -F perl-openpkg-files fixate cleanup
%else
    >perl-openpkg-files
%endif

    #   create parent run-time directory of MILTER application sockets
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/var/milter

    #   determine installation files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std} `cat perl-openpkg-files`

%files -f files

%clean

%post
    #   create run-time directory of MILTER application sockets
    #   (which are required to have particular user/group ownerships
    #   in order to allow all MTAs to access it under run-time)
    mkdir $RPM_INSTALL_PREFIX/var/milter/socket 2>/dev/null || true
    chown %{l_rusr}:%{l_mgrp} $RPM_INSTALL_PREFIX/var/milter/socket
    chmod 2775 $RPM_INSTALL_PREFIX/var/milter/socket

%preun
    #   try to remove run-time directory of MILTER application sockets
    rmdir $RPM_INSTALL_PREFIX/var/milter/socket 2>/dev/null || true

