##
##  postfix.spec -- OpenPKG RPM Specification
##  Copyright (c) 2000-2003 Cable & Wireless Deutschland GmbH
##  Copyright (c) 2000-2003 The OpenPKG Project <http://www.openpkg.org/>
##  Copyright (c) 2000-2003 Ralf S. Engelschall <rse@engelschall.com>
##
##  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      2.0.0.2
%define       V_tls          0.8.12-2.0.0.1-0.9.6h
%define       V_pflogsumm    1.0.7

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

#   package options
%option       with_tls       no
%option       with_sasl      no
%option       with_mysql     no

#   list of sources
Source0:      ftp://ftp.porcupine.org/mirrors/postfix-release/official/postfix-%{V_postfix}.tar.gz
Source1:      ftp://ftp.aet.tu-cottbus.de/pub/postfix_tls/pfixtls-%{V_tls}.tar.gz
Source2:      http://jimsun.linxnet.com/downloads/pflogsumm-%{V_pflogsumm}.tar.gz
Source3:      etc.tar
Source4:      fsl.postfix
Source5:      rc.postfix
Patch0:       pflogsumm.patch

#   build information
Prefix:       %{l_prefix}
BuildRoot:    %{l_buildroot}
BuildPreReq:  OpenPKG, openpkg >= 20030103, perl, fsl
PreReq:       OpenPKG, openpkg >= 20030103, perl, fsl, procmail, perl-time
BuildPreReq:  db, pcre
%if "%{with_tls}" == "yes"
BuildPreReq:  openssl
%endif
%if "%{with_sasl}" == "yes"
BuildPreReq:  sasl
PreReq:       sasl
%endif
%if "%{with_mysql}" == "yes"
BuildPreReq:  mysql
PreReq:       mysql
%endif
AutoReq:      no
AutoReqProv:  no
Provides:     MTA
Conflicts:    sendmail, ssmtp, exim

%description
    Postfix is a new-generation Mail Transfer Agent (MTA) able to fully
    replace the Sendmail MTA.

    Local specifics in this OpenPKG version:
    o Postfix delivers locally via Procmail
    o Postfix logs directly to the filesystem and not via syslog(3)
    o Berkeley-DB lookup table support
    o PCRE matching support
    o Optional STARTTLS support (see package options below)
    o Optional SASL2 authentication support (see package options below)
    o Optional MySQL support (see package options below)

%prep
    #   unpack distribution files
    %setup0 -q
%if "%{with_tls}" == "yes"
    %setup1 -q -T -D -a 1
%endif
    %setup2 -q -T -D -a 2
    %setup3 -q -T -D -a 3

    #   apply external TLS patches
%if "%{with_tls}" == "yes"
    %{l_patch} -p1 <pfixtls-%{V_tls}/pfixtls.diff
%endif

    #   apply pflogsumm patch
    ( cd pflogsumm-%{V_pflogsumm}; %{l_patch} -p0 <%{SOURCE pflogsumm.patch} )

%build
    #   configure Postfix (hard-core part I)
    %{l_shtool} subst \
        -e 's/var_config_dir, /var_command_dir, /' \
        src/postfix/postfix.c
    %{l_shtool} subst \
        -e 's;config_directory/postfix-script;command_directory/postfix-script;' \
        -e 's;config_directory/post-install;command_directory/postfix-install;' \
        conf/postfix-script
    %{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 -I%{l_prefix}/include"
    CCARGS="$CCARGS -DDEF_CONFIG_DIR=\\\"%{l_prefix}/etc/postfix\\\""
    AUXLIBS=""
    AUXLIBS="$AUXLIBS -L%{l_prefix}/lib"
    CCARGS="$CCARGS -DHAS_DB"
    AUXLIBS="$AUXLIBS -ldb"
    CCARGS="$CCARGS -DHAS_PCRE"
    AUXLIBS="$AUXLIBS -lpcre"
%if "%{with_mysql}" == "yes"
    CCARGS="$CCARGS -DHAS_MYSQL -I%{l_prefix}/include/mysql"
    AUXLIBS="$AUXLIBS -L%{l_prefix}/lib/mysql -lmysqlclient -lz -lm"
%endif
%if "%{with_sasl}" == "yes"
    CCARGS="$CCARGS -DUSE_SASL_AUTH -I%{l_prefix}/include/sasl"
    AUXLIBS="$AUXLIBS -lsasl2 -lcrypt"
    if [ -f /usr/lib/libdl.so -o -f /usr/lib/libdl.a ]; then
        AUXLIBS="$AUXLIBS -ldl"
    fi
%endif
%if "%{with_tls}" == "yes"
    CCARGS="$CCARGS -DHAS_SSL"
    AUXLIBS="$AUXLIBS -lssl -lcrypto"
%endif
    AUXLIBS="$AUXLIBS `%{l_prefix}/bin/fsl-config --all --ldflags --libs`"
    %{l_make} %{l_mflags} 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
    rm -rf $RPM_BUILD_ROOT

    #   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 \
        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}
    %{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

    #   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
    %{l_shtool} subst \
        -e "s;^\\(mail_owner[^=]*=\\).*;\\1 %{l_musr};" \
        $RPM_BUILD_ROOT%{l_prefix}/etc/postfix/main.cf
    rm -f $RPM_BUILD_ROOT%{l_prefix}/etc/postfix/*.orig
    rm -f $RPM_BUILD_ROOT%{l_prefix}/etc/postfix/LICENSE
    mv $RPM_BUILD_ROOT%{l_prefix}/etc/postfix/postfix-script \
       $RPM_BUILD_ROOT%{l_prefix}/sbin/postfix-script
    rm -f $RPM_BUILD_ROOT%{l_prefix}/etc/postfix/postfix-script*
    mv $RPM_BUILD_ROOT%{l_prefix}/etc/postfix/post-install \
       $RPM_BUILD_ROOT%{l_prefix}/sbin/postfix-install
    ( cd $RPM_BUILD_ROOT%{l_prefix}/etc/postfix
      %{l_shtool} mkdir -f -p -m 755 sample
      for cfg in access aliases canonical pcre_table regexp_table \
                 relocated sample-* transport virtual \
                 main.cf master.cf main.cf.default; do
          mv $cfg sample/
      done )

    #   install default configuration
    %{l_shtool} install -c -m 644 \
        -e 's;@l_prefix@;%{l_prefix};g' \
        -e 's;@l_musr@;%{l_musr};g' \
        -e 's;@l_mgrp@;%{l_mgrp};g' \
        -e 's;@l_rusr@;%{l_rusr};g' \
        -e 's;@l_rgrp@;%{l_rgrp};g' \
        -e 's;@l_nusr@;%{l_nusr};g' \
        -e 's;@l_ngrp@;%{l_ngrp};g' \
        etc/* $RPM_BUILD_ROOT%{l_prefix}/etc/postfix/

    #   pre-create variable stuff
    ( cd $RPM_BUILD_ROOT%{l_prefix}/var/postfix
      %{l_shtool} mkdir -f -p -m 700 \
          active bounce corrupt defer deferred \
          flush incoming private saved
      %{l_shtool} mkdir -f -p -m 730 maildrop
      %{l_shtool} mkdir -f -p -m 710 public
      %{l_shtool} mkdir -f -p -m 755 log pid )

    #   install addons
    ( 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/ )

    #   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 \
        -e 's;@l_prefix@;%{l_prefix};g' \
        -e 's;@l_susr@;%{l_susr};g' \
        -e 's;@l_musr@;%{l_musr};g' \
        -e 's;@l_mgrp@;%{l_mgrp};g' \
        %{SOURCE rc.postfix} $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d/

    #   OSSP fsl support
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/fsl
    %{l_shtool} install -c -m 644 \
        -e 's;@l_prefix@;%{l_prefix};g' \
        %{SOURCE fsl.postfix} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/fsl/

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

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

%files -f files

%clean
    rm -rf $RPM_BUILD_ROOT

%post
    if [ $1 -eq 1 ]; then
        #   generate logfile with correct owner and permissions
        if [ ! -f $RPM_INSTALL_PREFIX/var/postfix/log/postfix.log ]; then
            $RPM_INSTALL_PREFIX/lib/openpkg/shtool \
                install -c -m 755 -o %{l_musr} -g %{l_mgrp} \
                /dev/null $RPM_INSTALL_PREFIX/var/postfix/log/postfix.log
        fi
        #   generate initial configuration
        (cd $RPM_INSTALL_PREFIX/etc/postfix; %{l_make} all >/dev/null 2>&1 || true)
    elif [ $1 -gt 1 ]; then
        #   re-genersate configuration
        (cd $RPM_INSTALL_PREFIX/etc/postfix; %{l_make} clean all >/dev/null 2>&1 || true)
        #   re-load daemon
        $RPM_INSTALL_PREFIX/etc/rc postfix reload >/dev/null 2>&1 || true
    fi

%preun
    if [ $1 -eq 0 ]; then
        #   stop daemon
        $RPM_INSTALL_PREFIX/etc/rc postfix stop >/dev/null 2>&1 || true
        #   remove generated configuration files
        (cd $RPM_INSTALL_PREFIX/etc/postfix/; %{l_make} clean >/dev/null 2>&1 || true)
        #   remove generated run-time files and directories
        rm -rf $RPM_INSTALL_PREFIX/etc/postfix/prng_exch
        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
    fi

