##
##  postfix.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 versions
%define       V_postfix      3.10.4
%define       V_pflogsumm    1.1.6
%define       V_whoson       2.4.0

#   package information
Name:         postfix
Summary:      Mail Transfer Agent (MTA)
URL:          http://www.postfix.org/
Vendor:       Wietse Venema
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        BASE
Group:        Mail
License:      IPL
Version:      %{V_postfix}
Release:      20250823

#   package options
%option       with_fsl       yes
%option       with_ssl       no
%option       with_sasl      no
%option       with_sqlite    no
%option       with_mysql     no
%option       with_pgsql     no
%option       with_ldap      no
%option       with_whoson    no
%option       with_fdsetsize no

#   list of sources
Source0:      ftp://ftp.porcupine.org/mirrors/postfix-release/official/postfix-%{V_postfix}.tar.gz
Source1:      http://jimsun.linxnet.com/downloads/pflogsumm-%{V_pflogsumm}.tar.gz
Source2:      postfix.txt
Source3:      fsl.postfix
Source4:      rc.postfix
Patch0:       postfix.patch
Patch1:       http://download.openpkg.org/components/versioned/postfix/postfix-%{V_whoson}-whoson.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, perl, gcc
PreReq:       OpenPKG, openpkg >= 20160101, perl, procmail, perl-time
BuildPreReq:  make, pcre, db
PreReq:       make, pcre, db
%if "%{with_fsl}" == "yes"
BuildPreReq:  fsl
PreReq:       fsl
%endif
%if "%{with_ssl}" == "yes"
BuildPreReq:  openssl
PreReq:       openssl
%endif
%if "%{with_sasl}" == "yes"
BuildPreReq:  sasl
PreReq:       sasl
%endif
%if "%{with_sqlite}" == "yes"
BuildPreReq:  sqlite
PreReq:       sqlite
%endif
%if "%{with_mysql}" == "yes"
BuildPreReq:  mysql
PreReq:       mysql
%endif
%if "%{with_pgsql}" == "yes"
BuildPreReq:  postgresql, openssl
PreReq:       postgresql, openssl
%endif
%if "%{with_ldap}" == "yes"
BuildPreReq:  openldap, openssl
PreReq:       openldap, openssl
%endif
%if "%{with_whoson}" == "yes"
BuildPreReq:  whoson
PreReq:       whoson
%endif
Provides:     MTA
Conflicts:    exim, sendmail, ssmtp

%description
    Postfix is a new-generation Mail Transfer Agent (MTA) able to fully
    replace the Sendmail MTA. It is fully standards compliant and
    supports SMTP, ESMTP, LMTP over IPv4/IPv6 with optional TLS/SASL.

    Local specifics in this OpenPKG version:
    o Postfix delivers locally via Procmail
    o Postfix logs directly to the filesystem via OSSP fsl
    o Berkeley-DB dictionary support
    o PCRE matching support
    o Optional STARTTLS encryption support   (see package options)
    o Optional SASL2 authentication support  (see package options)
    o Optional SQLite dictionary support     (see package options)
    o Optional MySQL dictionary support      (see package options)
    o Optional PostgreSQL dictionary support (see package options)
    o Optional OpenLDAP dictionary support   (see package options)
    o Optional WHOSON dictionary support     (see package options)

%track
    prog postfix = {
        version   = %{V_postfix}
        url       = ftp://ftp.porcupine.org/mirrors/postfix-release/official/
        regex     = postfix-(\d+\.\d+\.\d+)\.tar\.gz
    }
    prog postfix:pflogsumm = {
        version   = %{V_pflogsumm}
        url       = http://jimsun.linxnet.com/postfix_contrib.html
        regex     = pflogsumm-(__VER__)\.tar\.gz
    }
    prog postfix:whoson = {
        version   = %{V_whoson}
        url       = http://download.openpkg.org/components/versioned/postfix/
        regex     = postfix-(__VER__)-whoson.patch
    }

%prep
    #   unpack distribution files
    %setup -q
    %setup -q -T -D -a 1

    #   apply OpenPKG patches
    %patch -p0

    #   apply vendor WHOSON patch
%if "%{with_whoson}" == "yes"
    %patch -p0 -P 1
%endif

%build
    #   configure Postfix (hard-core part I)
    %{l_shtool} subst \
        -e 's;/usr/include;%{l_prefix}/include;g' \
        makedefs

    #   configure Postfix (regular part)
    unset LD_LIBRARY_PATH || true
    CCARGS=""
    CCARGS="$CCARGS %{l_cflags -O}"
    CCARGS="$CCARGS %{l_cppflags}"
    CCARGS="$CCARGS -DDEF_COMMAND_DIR=\\\"%{l_prefix}/sbin\\\""
    CCARGS="$CCARGS -DDEF_SENDMAIL_PATH=\\\"%{l_prefix}/sbin/sendmail\\\""
    CCARGS="$CCARGS -DDEF_CONFIG_DIR=\\\"%{l_prefix}/etc/postfix\\\""
    CCARGS="$CCARGS -DDEF_DAEMON_DIR=\\\"%{l_prefix}/libexec/postfix\\\""
    CCARGS="$CCARGS -DDEF_QUEUE_DIR=\\\"%{l_prefix}/var/postfix\\\""
    CCARGS="$CCARGS -DDEF_DATA_DIR=\\\"%{l_prefix}/var/postfix/data\\\""
    AUXLIBS=""
    AUXLIBS="$AUXLIBS %{l_ldflags}"
    CCARGS="$CCARGS -DHAS_DB"
    AUXLIBS="$AUXLIBS -ldb"
    CCARGS="$CCARGS -DHAS_PCRE"
    AUXLIBS="$AUXLIBS -lpcre"
%if "%{with_sqlite}" == "yes"
    CCARGS="$CCARGS -DHAS_SQLITE"
    AUXLIBS="$AUXLIBS -lsqlite3 -lm"
%endif
%if "%{with_mysql}" == "yes"
    CCARGS="$CCARGS -DHAS_MYSQL %{l_cppflags mysql .}"
    AUXLIBS="$AUXLIBS %{l_ldflags mysql .} -lmysqlclient -lz -lm"
%endif
%if "%{with_pgsql}" == "yes"
    CCARGS="$CCARGS -DHAS_PGSQL %{l_cppflags postgresql .}"
    AUXLIBS="$AUXLIBS -lpq -lssl -lcrypto -lcrypt"
%endif
%if "%{with_sasl}" == "yes"
    CCARGS="$CCARGS -DUSE_SASL_AUTH -DUSE_CYRUS_SASL %{l_cppflags sasl .}"
    AUXLIBS="$AUXLIBS -lsasl2 -lcrypt"
    if [ -f /usr/lib/libdl.so -o -f /usr/lib/libdl.a ]; then
        AUXLIBS="$AUXLIBS -ldl"
    elif [ -f /usr/lib64/libdl.so -o -f /usr/lib64/libdl.a ]; then
        AUXLIBS="$AUXLIBS -ldl"
    elif [ -f /lib/x86_64-linux-gnu/libdl.so.2 -o -f /lib/x86_64-linux-gnu/libdl.a ]; then
        AUXLIBS="$AUXLIBS -ldl"
    fi
%endif
%if "%{with_ssl}" == "yes"
    CCARGS="$CCARGS -DUSE_TLS"
    AUXLIBS="$AUXLIBS -lssl -lcrypto"
%endif
%if "%{with_ldap}" == "yes"
    CCARGS="$CCARGS -DHAS_LDAP"
    AUXLIBS="$AUXLIBS -lldap -llber -lssl -lcrypto"
%endif
%if "%{with_fsl}" == "yes"
    AUXLIBS="$AUXLIBS %{l_fsl_ldflags} %{l_fsl_libs}"
    CCARGS="$CCARGS -DUSE_SOFTLIMITONLY"
%endif
%if "%{with_fdsetsize}" != "no"
%if "%{with_fdsetsize}" == "yes"
    CCARGS="$CCARGS -DFD_SETSIZE=1024"
%else
    CCARGS="$CCARGS -DFD_SETSIZE=%{with_fdsetsize}"
%endif
%endif
    case "%{l_platform -t}" in
        *-sunos* ) AUXLIBS="$AUXLIBS -lrt" ;;
    esac
    %{l_make} %{l_mflags} -f Makefile.init makefiles \
        CC="%{l_cc}" CCARGS="$CCARGS" AUXLIBS="$AUXLIBS"

    #   configure Postfix (hard-core part II)
    %{l_shtool} subst \
        -e 's;#define HAS_DBM;#define HAS_DBM_DISABLED;' \
        -e 's;#define HAS_DB;#define HAS_DB_DISABLED;' \
        src/util/sys_defs.h

    #   build Postfix
    %{l_make} %{l_mflags}

%install
    #   perform standard installation procedure
    %{l_shtool} subst -e "s;chown;true;" postfix-install
    sh postfix-install -non-interactive \
        install_root=$RPM_BUILD_ROOT \
        config_directory=%{l_prefix}/etc/postfix \
        daemon_directory=%{l_prefix}/libexec/postfix \
        command_directory=%{l_prefix}/sbin \
        queue_directory=%{l_prefix}/var/postfix \
        data_directory=%{l_prefix}/var/postfix/data \
        sendmail_path=%{l_prefix}/sbin/sendmail \
        newaliases_path=%{l_prefix}/sbin/newaliases \
        mailq_path=%{l_prefix}/sbin/mailq \
        manpage_directory=%{l_prefix}/man \
        mail_user=%{l_musr} \
        setgid_group=%{l_rgrp}

    #   post-adjust binaries
    rm -f $RPM_BUILD_ROOT%{l_prefix}/sbin/mailq
    ln $RPM_BUILD_ROOT%{l_prefix}/sbin/sendmail \
       $RPM_BUILD_ROOT%{l_prefix}/sbin/mailq
    rm -f $RPM_BUILD_ROOT%{l_prefix}/sbin/newaliases
    ln $RPM_BUILD_ROOT%{l_prefix}/sbin/sendmail \
       $RPM_BUILD_ROOT%{l_prefix}/sbin/newaliases
    strip $RPM_BUILD_ROOT%{l_prefix}/sbin/* >/dev/null 2>&1 || true
    strip $RPM_BUILD_ROOT%{l_prefix}/libexec/postfix/* >/dev/null 2>&1 || true

    #   post-adjust configuration
    for cfg in \
        *LICENSE makedefs.out bounce.cf.default access aliases \
        canonical header_checks relocated transport virtual \
        main.cf main.cf.proto master.cf master.cf.proto main.cf.default; do
        rm -f $RPM_BUILD_ROOT%{l_prefix}/etc/postfix/$cfg
    done

    #   install default configuration
    for name in `grep "^<file" %{SOURCE postfix.txt} | sed -e 's;^.*name=";;' -e 's;".*$;;'`; do
        (echo ""; cat %{SOURCE postfix.txt}; echo "") |\
            sed -e "1,/^<file name=\"$name\">/d" -e "/<\/file>/,\$d" >$name
        %{l_shtool} install -c -m 644 %{l_value -s -a} \
            $name $RPM_BUILD_ROOT%{l_prefix}/etc/postfix/
    done
    %{l_shtool} subst \
%if "%{with_ssl}" == "yes"
        -e 's/^SSL://' \
%else
        -e '/^SSL:/d' \
%endif
%if "%{with_sasl}" == "yes"
        -e 's/^SASL://' \
%else
        -e '/^SASL:/d' \
%endif
        $RPM_BUILD_ROOT%{l_prefix}/etc/postfix/main.cf

    #   pre-create variable stuff
    ( cd $RPM_BUILD_ROOT%{l_prefix}/var/postfix
      %{l_shtool} mkdir -f -p -m 700 data
      %{l_shtool} mkdir -f -p -m 755 log
    ) || exit $?

    #   install addons
    %{l_shtool} install -c -m 755 \
        -e 's;/usr/sbin/sendmail;%{l_prefix}/sbin/sendmail;g' \
        auxiliary/rmail/rmail $RPM_BUILD_ROOT%{l_prefix}/sbin/rmail
    %{l_shtool} install -c -m 755 \
        -e 's;/usr/bin/perl;%{l_prefix}/bin/perl;g' \
        -e 's;postconf -h;%{l_prefix}/sbin/postconf -h;' \
        auxiliary/qshape/qshape.pl $RPM_BUILD_ROOT%{l_prefix}/sbin/qshape
    %{l_shtool} install -c -m 644 \
        man/man1/qshape.1 $RPM_BUILD_ROOT%{l_prefix}/man/man8/qshape.8
    ( cd pflogsumm-%{V_pflogsumm}
      %{l_shtool} install -c -m 755 \
          -e 's;/usr/bin/perl;%{l_prefix}/bin/perl;g' \
          pflogsumm.pl $RPM_BUILD_ROOT%{l_prefix}/sbin/pflogsumm
      %{l_shtool} install -c -m 644 \
          pflogsumm.1 $RPM_BUILD_ROOT%{l_prefix}/man/man1/
    ) || exit $?

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

    #   adjust installation to avoid file name conflicts
    ( cd $RPM_BUILD_ROOT%{l_prefix}/man/man8
      mv master.8 postfix_master.8
    ) || exit $?

    #   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.postfix} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/fsl/

    #   generate file list
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std} \
        '%config %{l_prefix}/etc/fsl/fsl.postfix' \
        '%attr(-,%{l_susr},%{l_mgrp}) %{l_prefix}/etc/postfix' \
        '%config %attr(-,%{l_susr},%{l_mgrp}) %{l_prefix}/etc/postfix/*' \
        '%attr(-,%{l_susr},%{l_mgrp}) %{l_prefix}/libexec/postfix/*' \
        '%attr(2755,%{l_musr},%{l_rgrp}) %{l_prefix}/sbin/{postdrop,postqueue}' \
        '%dir %attr(-,%{l_susr},%{l_mgrp}) %{l_prefix}/libexec/postfix' \
        '%dir %attr(-,%{l_susr},%{l_mgrp}) %{l_prefix}/var/postfix' \
        '%dir %attr(-,%{l_susr},%{l_mgrp}) %{l_prefix}/var/postfix/pid' \
        '%dir %attr(-,%{l_musr},%{l_rgrp}) %{l_prefix}/var/postfix/{maildrop,public}'

%files -f files

%clean

%pre
    #   before upgrade, save status and stop service
    [ $1 -eq 2 ] || exit 0
    eval `%{l_rc} postfix status 2>/dev/null | tee %{l_tmpfile}`
    %{l_rc} postfix stop 2>/dev/null
    exit 0

%post
    if [ $1 -eq 1 ]; then
        #   after install, generate configuration
        ( cd $RPM_INSTALL_PREFIX/etc/postfix && %{l_make} all; true ) >/dev/null 2>&1
    fi
    if [ $1 -eq 2 ]; then
        #   after upgrade, regenerate configuration
        ( cd $RPM_INSTALL_PREFIX/etc/postfix && %{l_make} clean all; true ) >/dev/null 2>&1
        #   after upgrade, restore status
        eval `cat %{l_tmpfile}`; rm -f %{l_tmpfile} >/dev/null 2>&1 || true
        [ ".$postfix_active" = .yes ] && %{l_rc} postfix start
    fi
    exit 0

%preun
    #   before erase, stop service and remove log files
    [ $1 -eq 0 ] || exit 0
    %{l_rc} postfix stop 2>/dev/null
    rm -f $RPM_INSTALL_PREFIX/var/postfix/log/postfix.log* >/dev/null 2>&1 || true
    rm -f $RPM_INSTALL_PREFIX/var/postfix/log/postfix.sum* >/dev/null 2>&1 || true
    #   remove generated configuration files
    ( cd $RPM_INSTALL_PREFIX/etc/postfix && %{l_make} clean >/dev/null 2>&1; true ) || true
    #   remove generated run-time files and directories
    rm -rf $RPM_INSTALL_PREFIX/var/postfix/data/*
    rm -rf $RPM_INSTALL_PREFIX/var/postfix/pid/*
    rm -rf $RPM_INSTALL_PREFIX/var/postfix/private/*
    rm -rf $RPM_INSTALL_PREFIX/var/postfix/public/*
    find $RPM_INSTALL_PREFIX/var/postfix/active/ -type d -print |\
        xargs rmdir >/dev/null 2>&1 || true
    find $RPM_INSTALL_PREFIX/var/postfix/incoming/ -type d -print |\
        xargs rmdir >/dev/null 2>&1 || true
    exit 0

