##
##  dovecot.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_major       2.4
%define       V_minor       0
%define       V_pigeonhole  0.5.21.1

#   package information
Name:         dovecot
Summary:      IMAP4 & POP3 Server
URL:          http://www.dovecot.org/
Vendor:       Timo Sirainen et al.
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        PLUS
Group:        Mail
License:      MIT+LGPL
Version:      %{V_major}.%{V_minor}
Release:      20250124

#   package options
%option       with_fsl          yes
%option       with_sieve        yes
%option       with_pam          no
%option       with_ldap         no
%option       with_mysql        no
%option       with_pgsql        no
%option       with_sqlite       no
%option       with_sodium       no

#   list of sources
Source0:      https://dovecot.org/releases/%{V_major}/dovecot-%{V_major}.%{V_minor}.tar.gz
Source1:      https://pigeonhole.dovecot.org/releases/%{V_major}/dovecot-pigeonhole-%{V_major}.%{V_minor}.tar.gz
Source2:      fsl.dovecot
Source3:      rc.dovecot
Source4:      dovecot.conf
Patch0:       dovecot.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101
PreReq:       OpenPKG, openpkg >= 20160101, MTA, x509
BuildPreReq:  libiconv, openssl, zlib, bzip2
PreReq:       libiconv, openssl, zlib, bzip2
%if "%{with_fsl}" == "yes"
BuildPreReq:  fsl
PreReq:       fsl
%endif
%if "%{with_ldap}" == "yes"
BuildPreReq:  openldap
PreReq:       openldap
%endif
%if "%{with_sieve}" == "yes"
BuildPreReq:  gettext
PreReq:       gettext
%endif
%if "%{with_pam}" == "yes"
BuildPreReq:  PAM
PreReq:       PAM
%endif
%if "%{with_mysql}" == "yes"
BuildPreReq:  mysql, zlib
PreReq:       mysql, zlib
%endif
%if "%{with_pgsql}" == "yes"
BuildPreReq:  postgresql
PreReq:       postgresql
%endif
%if "%{with_sqlite}" == "yes"
BuildPreReq:  sqlite
PreReq:       sqlite
%endif
%if "%{with_sodium}" == "yes"
BuildPreReq:  libsodium
PreReq:       libsodium
%endif

%description
    Dovecot is an Open Source IMAP and POP3 server, written with
    security primarily in mind. Dovecot is an excellent choice for both
    small and large installations. It's fast, simple to set up, requires
    no special administration and it uses very little memory.

%track
    prog dovecot = {
        version   = %{V_major}.%{V_minor}
        url       = https://dovecot.org/download.html
        regex     = dovecot-(\d+\.\d+(?:\.\d+)*)\.tar\.gz
    }
    prog dovecot:sieve = {
        version   = %{V_pigeonhole}
        url       = https://pigeonhole.dovecot.org/download.html
        regex     = dovecot-%{V_major}-pigeonhole-(__VER__)\.tar\.gz
    }

%prep
    %setup -q -n dovecot-%{version}
%if "%{with_sieve}" == "yes"
    %setup -q -n dovecot-%{version} -T -D -a 1
%endif
    %patch -p0

%build
    #   build Dovecot
    %{l_shtool} subst \
        -e 's;/usr/sbin/sendmail;%{l_prefix}/sbin/sendmail;g' \
        src/lib-smtp/smtp-submit-settings.c
    cppflags="%{l_cppflags}"
    ldflags="%{l_ldflags} %{l_fsl_ldflags}"
    libs="%{l_fsl_libs}"
    sql_drivers=""
%if "%{with_ldap}" == "yes"
    libs="$libs -llber -lssl -lcrypto"
%endif
%if "%{with_mysql}" == "yes"
    cppflags="$cppflags %{l_cppflags mysql}"
    ldflags="$ldflags %{l_ldflags mysql}"
%endif
%if "%{with_pgsql}" == "yes"
    libs="$libs -lssl -lcrypto -lcrypt"
%endif
%if "%{with_sqlite}" == "yes"
    libs="$libs -lm"
%endif
    sql_drivers=`echo "$sql_drivers" | sed 's;^,;;'`
    CC="%{l_cc}" \
    CFLAGS="%{l_cflags -O}" \
    CPPFLAGS="$cppflags" \
    LDFLAGS="$ldflags" \
    LIBS="$libs" \
    ./configure \
        --prefix=%{l_prefix} \
        --mandir=%{l_prefix}/man \
        --sysconfdir=%{l_prefix}/etc \
        --with-ssldir=%{l_prefix}/etc/dovecot/ssl \
        --datadir=%{l_prefix}/share/dovecot \
        --docdir=%{l_prefix}/share/dovecot/doc \
        --with-rundir=%{l_prefix}/var/dovecot/run \
        --with-libiconv-prefix=%{l_prefix} \
        --with-ssl=openssl \
        --with-docs \
%if "%{with_ldap}" == "yes"
        --with-ldap \
%else
        --without-ldap \
%endif
%if "%{with_mysql}" == "yes" || "%{with_pgsql}" == "yes" || "%{with_sqlite}" == "yes"
        --with-sql \
%if "%{with_mysql}" == "yes"
        --with-mysql \
%else
        --without-mysql \
%endif
%if "%{with_pgsql}" == "yes"
        --with-pgsql \
%else
        --without-pgsql \
%endif
%if "%{with_sqlite}" == "yes"
        --with-sqlite \
%else
        --without-sqlite \
%endif
%else
        --without-sql \
        --without-lua \
        --without-mysql \
        --without-pgsql \
        --without-sqlite \
%endif
%if "%{with_pam}" == "yes"
        --with-pam \
%else
        --without-pam \
%endif
%if "%{with_sodium}" == "yes"
        --with-sodium \
%else
        --without-sodium \
%endif
        --without-gssapi \
        --without-lucene \
        --without-vpopmail \
        --without-lzma \
        --without-lz4 \
        --without-icu \
        --enable-shared \
        --disable-static
    %{l_make} %{l_mflags -O}

%if "%{with_sieve}" == "yes"
    #   build optional Dovecot LDA sieve plugin
    ( cd dovecot-*-pigeonhole-*
      CC="%{l_cc}" \
      CFLAGS="%{l_cflags -O}" \
      CPPFLAGS="%{l_cppflags}" \
      LDFLAGS="%{l_ldflags}" \
      ./configure \
          --prefix=%{l_prefix} \
          --mandir=%{l_prefix}/man \
          --with-dovecot=`pwd`/.. \
          --with-managesieve
      %{l_make} %{l_mflags -O}
    ) || exit $?
%endif

%install
    #   install Dovecot
    %{l_make} %{l_mflags} install AM_MAKEFLAGS="DESTDIR=$RPM_BUILD_ROOT"

%if "%{with_sieve}" == "yes"
    #   install optional Dovecot LDA sieve plugin
    ( cd dovecot-*-pigeonhole-*
      %{l_make} %{l_mflags} install AM_MAKEFLAGS="DESTDIR=$RPM_BUILD_ROOT"
    ) || exit $?
%endif

    #   create additional dirctories
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/dovecot/ssl \
        $RPM_BUILD_ROOT%{l_prefix}/var/dovecot/log \
        $RPM_BUILD_ROOT%{l_prefix}/var/dovecot/run/login

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

    #   strip installation
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/etc/dovecot/dovecot
    rm -f  $RPM_BUILD_ROOT%{l_prefix}/etc/dovecot/README
    rm -f  $RPM_BUILD_ROOT%{l_prefix}/etc/dovecot/dovecot*example.conf
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/share/dovecot
    rm -f  $RPM_BUILD_ROOT%{l_prefix}/lib/dovecot/{,*}/*.a  >/dev/null 2>&1 || true
    rm -f  $RPM_BUILD_ROOT%{l_prefix}/lib/dovecot/{,*}/*.la >/dev/null 2>&1 || true
    strip \
        $RPM_BUILD_ROOT%{l_prefix}/sbin/* \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/dovecot/* \
        >/dev/null 2>&1 || true

    #   install default server configuration
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/dovecot
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE dovecot.conf} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/dovecot/

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

    #   generate file list
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT %{l_files_std} \
        '%not %dir %{l_prefix}/etc/fsl' \
        '%config %{l_prefix}/etc/fsl/fsl.dovecot' \
        '%config %attr(0600,%{l_musr},%{l_mgrp}) %{l_prefix}/etc/dovecot/*' \
        '%config %attr(0644,%{l_musr},%{l_rgrp}) %{l_prefix}/etc/dovecot/dovecot.conf' \
        '%dir %attr(0700,%{l_susr},%{l_mgrp}) %{l_prefix}/etc/dovecot/ssl' \
        '%dir %attr(0755,%{l_susr},%{l_mgrp}) %{l_prefix}/var/dovecot' \
        '%dir %attr(0770,%{l_susr},%{l_rgrp}) %{l_prefix}/var/dovecot/log' \
        '%dir %attr(0755,%{l_susr},%{l_mgrp}) %{l_prefix}/var/dovecot/run' \
        '%dir %attr(0750,%{l_susr},%{l_mgrp}) %{l_prefix}/var/dovecot/run/login'

%files -f files

%clean

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

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

