##
##  postgrey.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:         postgrey
Summary:      Postfix Greylisting Daemon
URL:          http://postgrey.schweikert.ch/
Vendor:       David Schweikert
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        BASE
Group:        Mail
License:      GPL
Version:      1.37
Release:      20160922

#   list of sources
Source0:      http://postgrey.schweikert.ch/pub/postgrey-%{version}.tar.gz
Source1:      rc.postgrey
Source2:      whitelist.clients
Source3:      whitelist.recipients
Patch0:       postgrey.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101
PreReq:       OpenPKG, openpkg >= 20160101
BuildPreReq:  perl
PreReq:       perl, perl-sys, perl-net, perl-db, perl-dns

%description
    Postgrey is a Postfix MTA policy server implementing Greylisting.
    Related to whitelists and blacklists, a greylist indicates those
    emails (originating from unknown IPs) which are rejected with a
    "try again later" message. The email in question will be delayed
    until the sending MTA tries again, but this is where spam loses
    out. Most spam is not sent out using RFC compliant MTAs, and so
    the spamming software will not "try again later".

%track
    prog postgrey = {
        version   = %{version}
        url       = http://postgrey.schweikert.ch/pub/
        regex     = postgrey-(__VER__)\.tar\.gz
    }

%prep
    %setup -q
    %patch -p0

%build
    #   build manual page
    for i in postgrey contrib/postgreyreport; do
        pod2man $i >$i.8
        %{l_shtool} subst \
            -e 's;/etc/postfix/postgrey_\([a-z_.]\{1,\}\);%{l_prefix}/etc/postgrey/\1;g' \
            -e 's;/etc/postfix;%{l_prefix}/etc/postfix;g' \
            -e 's;/etc/main.cf;%{l_prefix}/etc/postfix/main.cf;g' \
            -e 's;/var/log/maillog;%{l_prefix}/var/postfix/log/postfix.log;g' \
            -e 's;/var/spool/postfix/postgrey;%{l_prefix}/var/postgrey;g' \
            $i.8
    done

%install
    #   create installation hierarchy
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/sbin \
        $RPM_BUILD_ROOT%{l_prefix}/man/man8 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d \
        $RPM_BUILD_ROOT%{l_prefix}/etc/postgrey \
        $RPM_BUILD_ROOT%{l_prefix}/var/postgrey

    #   install programs and manual pages
    %{l_shtool} install -c -m 755 \
        -e 's;#!/usr/bin/perl;#!%{l_prefix}/bin/perl;g' \
        -e "s;\\(.\\)Sys::Syslog\\(. : undef\\);\\1%{l_prefix}/var/postgrey/postgrey.log\\2;" \
        postgrey $RPM_BUILD_ROOT%{l_prefix}/sbin/
    %{l_shtool} install -c -m 755 \
        -e 's;#!/usr/bin/perl;#!%{l_prefix}/bin/perl;g' \
        -e 's;/var/spool/postfix/postgrey;%{l_prefix}/var/postgrey;' \
        -e 's;\(=> .\)postgrey\(.\);\1%{l_rusr}\2;' \
        contrib/postgreyreport $RPM_BUILD_ROOT%{l_prefix}/sbin/
    %{l_shtool} install -c -m 644 \
        postgrey.8 contrib/postgreyreport.8 \
        $RPM_BUILD_ROOT%{l_prefix}/man/man8/

    #   install configuration files
    %{l_shtool} install -c -m 644 \
        %{SOURCE whitelist.clients} \
        %{SOURCE whitelist.recipients} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/postgrey/

    #   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.postgrey} $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d/

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

%files -f files

%clean

%post
    #   after upgrade, restart service
    [ $1 -eq 2 ] || exit 0
    eval `%{l_rc} postgrey status 2>/dev/null`
    [ ".$postgrey_active" = .yes ] && %{l_rc} postgrey restart
    exit 0

%preun
    #   before erase, stop service and remove log files
    [ $1 -eq 0 ] || exit 0
    %{l_rc} postgrey stop 2>/dev/null
    rm -f $RPM_INSTALL_PREFIX/var/postgrey/*
    exit 0

