##
##  exim.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:         exim
Summary:      Exim Mail Transfer Agent
URL:          http://www.exim.org/
Vendor:       Philip Hazel
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        PLUS
Group:        Mail
License:      GPL
Version:      4.71
Release:      20100123

#   package options
%option       with_auth_cram_md5  no
%option       with_auth_dovecot   no
%option       with_auth_plaintext no
%option       with_auth_sasl      no
%option       with_content_scan   no
%option       with_dlfunc         no
%option       with_dsearch        no
%option       with_ipv6           no
%option       with_maildir        no
%option       with_mailstore      no
%option       with_mbx            no
%option       with_mysql          no
%option       with_pgsql          no
%option       with_spf            no
%option       with_sqlite         no
%option       with_ssl            no
%option       with_wrap           no

#   list of sources
Source0:      ftp://gd.tuwien.ac.at/infosys/mail/exim/exim/exim4/exim-%{version}.tar.bz2
Source1:      rc.exim
Source2:      exim.conf
Source3:      aliases

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, make, perl, gzip
PreReq:       OpenPKG, openpkg >= 20160101, perl, gzip
BuildPreReq:  db >= 4.1.24, libiconv
PreReq:       db >= 4.1.24, libiconv
%if "%{with_auth_sasl}" == "yes"
BuildPreReq:  sasl
PreReq:       sasl
%endif
%if "%{with_dlfunc}" == "yes" || "%{with_spf}" == "yes"
BuildPreReq:  gcc
%endif
%if "%{with_mysql}" == "yes"
BuildPreReq:  mysql, zlib
PreReq:       mysql, zlib
%endif
%if "%{with_pgsql}" == "yes"
BuildPreReq:  postgresql, openssl
PreReq:       postgresql, openssl
%endif
%if "%{with_spf}" == "yes"
BuildPreReq:  libspf2
PreReq:       libspf2
%endif
%if "%{with_sqlite}" == "yes"
BuildPreReq:  sqlite
PreReq:       sqlite
%endif
%if "%{with_ssl}" == "yes"
BuildPreReq:  openssl
PreReq:       openssl
%endif
%if "%{with_wrap}" == "yes"
BuildPreReq:  tcpwrappers
PreReq:       tcpwrappers
%endif
Provides:     MTA
Conflicts:    postfix, sendmail, ssmtp

%description
    Exim is a message transfer agent (MTA) developed at the University
    of Cambridge for use on Unix systems connected to the Internet.
    In style it is similar to Smail 3, but its facilities are more
    extensive, and in particular it has some defences against mail
    bombs and unsolicited junk mail in the form of options for refusing
    messages from particular hosts, networks, or senders. It can be
    installed in place of sendmail, although the configuration of Exim
    is quite different to that of Sendmail.

%track
    prog exim = {
        version   = %{version}
        url       = ftp://gd.tuwien.ac.at/infosys/mail/exim/exim/exim4/
        regex     = exim-(__VER__)\.tar\.bz2
    }

%prep
    %setup -q
    %{l_shtool} subst \
        -e 's;STRING_UNKNOWN;"unknown";g' \
        src/smtp_in.c
    %{l_shtool} subst \
        -e 's;\(if \[ $name = exim${EXE}\)\( \]\; then\);\1 -a ".$DESTDIR" = . \2;' \
        scripts/exim_install

%build
    #   correctly specify missing realtime functions library
    EXTRALIBS="%{l_ldflags} -liconv"
    case "%{l_platform -t}" in
        *-sunos* ) EXTRALIBS="$EXTRALIBS -lrt" ;;
    esac
%if "%{with_dlfunc}" == "yes"
    EXTRALIBS="$EXTRALIBS -export-dynamic -shared"
%endif

    #   create local compile-time configuration
    ( cat src/EDITME
      echo 'CC=%{l_cc}'
      echo 'INCLUDE=%{l_cppflags}'
      echo 'CFLAGS=%{l_cflags -O}'
      echo 'LDFLAGS=%{l_ldflags}'
      echo "EXTRALIBS+=$EXTRALIBS"
      echo "EXTRALIBS_EXIM+=$EXTRALIBS"

      #   user/group settings
      echo 'EXIM_USER=%{l_rusr}'
      echo 'EXIM_GROUP=%{l_rgrp}'
      echo 'FIXED_NEVER_USERS=%{l_susr}'

      #   directory layout
      echo 'BIN_DIRECTORY=%{l_prefix}/sbin'
      echo 'CONFIGURE_FILE=%{l_prefix}/etc/exim/exim.conf'
      echo 'SYSTEM_ALIASES_FILE=%{l_prefix}/etc/exim/aliases'
      echo 'LOG_FILE_PATH=%{l_prefix}/var/exim/log/%s.log'
      echo 'PID_FILE_PATH=%{l_prefix}/var/exim/run/exim.pid'
      echo 'SPOOL_DIRECTORY=%{l_prefix}/var/exim/spool'

      #   dynamically loading support
%if "%{with_dlfunc}" == "yes"
      echo 'EXPAND_DLFUNC=yes'
%endif

      #   content scanning support
%if "%{with_content_scan}" == "yes"
      echo 'WITH_CONTENT_SCAN=yes'
%endif

      #   helper applications
      echo 'PERL_COMMAND=%{l_prefix}/bin/perl'
      echo 'COMPRESS_COMMAND=%{l_prefix}/bin/gzip'
      echo 'ZCAT_COMMAND=%{l_prefix}/bin/zcat'

      #   don't build exim monitor
      echo 'EXIM_MONITOR='

      #   character code conversions used by $header_xxx expansion and
      #   Sieve filter
      echo 'HAVE_ICONV=YES'
      echo 'HEADERS_CHARSET=ISO-8859-1'

      #   use Berkeley DB in native mode as DBM library
      echo 'USE_DB=yes'
      echo 'DBMLIB=%{l_ldflags} -ldb'

      #   LMTP support
      echo 'TRANSPORT_LMTP=yes'

      #   move frozen mails out of the main spool directory
      echo 'SUPPORT_MOVE_FROZEN_MESSAGES=yes'

      #   support for external AUTH mechanisms
%if "%{with_auth_dovecot}" == "yes"
      echo 'AUTH_DOVECOT=yes'
%endif
%if "%{with_auth_sasl}" == "yes"
      echo 'AUTH_CYRUS_SASL=yes'
      echo 'AUTH_LIBS=%{l_ldflags} -lsasl2'
%endif

      #   AUTH extensions of the SMTP as defined by RFC 2554
%if "%{with_auth_cram_md5}" == "yes"
      echo 'AUTH_CRAM_MD5=yes'
%endif
%if "%{with_auth_plaintext}" == "yes"
      echo 'AUTH_PLAINTEXT=yes'
%endif

      #   IPv6 support
%if "%{with_ipv6}" == "yes"
      echo 'HAVE_IPV6=YES'
%endif

      #   support for dsearch lookups
%if "%{with_dsearch}" == "yes"
      echo 'LOOKUP_DSEARCH=yes'
%endif

      #   support for MySQL lookups
%if "%{with_mysql}" == "yes"
      echo 'LOOKUP_MYSQL=yes'
      echo 'LOOKUP_INCLUDE+=%{l_cppflags mysql}'
      echo 'LOOKUP_LIBS+=%{l_ldflags mysql} -lmysqlclient -lz -lm'
%endif

      #   support for PostgreSQL lookups
%if "%{with_pgsql}" == "yes"
      echo 'LOOKUP_PGSQL=yes'
      echo 'LOOKUP_INCLUDE+=%{l_cppflags postgresql}'
      echo 'LOOKUP_LIBS+=%{l_ldflags} -lpq -lssl -lcrypto -lcrypt'
%endif

      #   support for SQLite lookups
%if "%{with_sqlite}" == "yes"
      echo 'LOOKUP_SQLITE=yes'
      echo 'LOOKUP_LIBS+=%{l_ldflags} -lsqlite3'
%endif

      #   support for Transport layer security using SSL
%if "%{with_ssl}" == "yes"
      echo 'SUPPORT_TLS=yes'
      echo 'TLS_INCLUDE=%{l_cppflags}'
      echo 'TLS_LIBS=%{l_ldflags} -lssl -lcrypto'
%endif

      #   TCP wrapper support
%if "%{with_wrap}" == "yes"
      echo 'USE_TCP_WRAPPERS=yes'
      echo 'EXTRALIBS_EXIM+=%{l_ldflags} -lwrap'
%endif

      #   support for various appendfile transports
%if "%{with_maildir}" == "yes"
      echo 'SUPPORT_MAILDIR=yes'
%endif
%if "%{with_mailstore}" == "yes"
      echo 'SUPPORT_MAILSTORE=yes'
%endif
%if "%{with_mbx}" == "yes"
      echo 'SUPPORT_MBX=yes'
%endif

      #   experimental extensions
%if "%{with_spf}" == "yes"
      echo 'EXPERIMENTAL_SPF=yes'
      echo 'CFLAGS+=-DSPF'
      echo 'EXTRALIBS_EXIM+=-lspf2 -pthread'
%endif
    ) >Local/Makefile

    case "%{l_platform -t}" in
        *-freebsd* )
            %{l_shtool} subst \
                -e 's;^\.if \([^ ]*\) == \([^ ]*\) *$;ifeq (\1,\2);' \
                -e 's;^\.ifdef;ifdef;' \
                -e 's;^\.elifdef;elifdef;' \
                -e 's;^\.else;else;' \
                -e 's;^\.endif.*$;endif;' \
                OS/Makefile-FreeBSD
                ;;
    esac

    %{l_make} %{l_mflags} makefile
    %{l_make} %{l_mflags}

%install

    #   create directories
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/bin \
        $RPM_BUILD_ROOT%{l_prefix}/etc/exim \
        $RPM_BUILD_ROOT%{l_prefix}/man/man8 \
        $RPM_BUILD_ROOT%{l_prefix}/sbin \
        $RPM_BUILD_ROOT%{l_prefix}/var/exim/log \
        $RPM_BUILD_ROOT%{l_prefix}/var/exim/run

    #   install default configuration which must be done before the
    #   installation of exim
    %{l_shtool} install -c -m 640 %{l_value -s -a} \
        %{SOURCE exim.conf} \
        %{SOURCE aliases} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/exim/

    #   install exim
    %{l_make} install DESTDIR="$RPM_BUILD_ROOT" INSTALL_ARG="-no_chown"

    #   install man pages
    %{l_shtool} install -c -m 644 \
        doc/exim.8 $RPM_BUILD_ROOT%{l_prefix}/man/man8/

    #   provide backward compatibility
    for i in mailq newaliases rmail rsmtp runq sendmail; do
        ln -s exim   $RPM_BUILD_ROOT%{l_prefix}/sbin/$i
        ln -s exim.8 $RPM_BUILD_ROOT%{l_prefix}/man/man8/$i.8
    done
    for i in mailq newaliases rmail; do
        ln -s ../sbin/exim $RPM_BUILD_ROOT%{l_prefix}/bin/$i
    done

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

    #   strip installation
    rm -f $RPM_BUILD_ROOT%{l_prefix}/sbin/exicyclog
    strip $RPM_BUILD_ROOT%{l_prefix}/sbin/* >/dev/null 2>&1 || true

    #   determine installation files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std} \
        '%dir    %attr(0750,%{l_susr},%{l_rgrp}) %{l_prefix}/etc/exim' \
        '%config %attr(0640,%{l_susr},%{l_rgrp}) %{l_prefix}/etc/exim/*' \
        '        %attr(4711,%{l_susr},%{l_sgrp}) %{l_prefix}/sbin/exim' \
        '%dir    %attr(0755,%{l_susr},%{l_sgrp}) %{l_prefix}/var/exim' \
        '%dir    %attr(0770,%{l_susr},%{l_rgrp}) %{l_prefix}/var/exim/log' \
        '%dir    %attr(0755,%{l_susr},%{l_sgrp}) %{l_prefix}/var/exim/run'

%files -f files

%clean

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

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

