##
##  dbmail.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 component versions
%define       V_major     2.2
%define       V_minor     18

#   package information
Name:         dbmail
Summary:      Fast Scalable RDBMS Mail Server
URL:          http://www.dbmail.org/
Vendor:       IC&S
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Mail
License:      GPL
Version:      %{V_major}.%{V_minor}
Release:      20120713

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

#   checking for option conflicts
%if "%{with_sqlite}" == "no" && "%{with_mysql}" == "no" && "%{with_pgsql}" == "no"
    %{error:one of the build-time options 'with_sqlite', 'with_mysql' or 'with_pgsql' have to be enabled}
%endif

#   list of sources
Source0:      http://www.dbmail.org/download/%{V_major}/dbmail-%{V_major}.%{V_minor}.tar.gz
Source1:      fsl.dbmail
Source2:      rc.dbmail
Source3:      dbmail-setup.sh
Source4:      dbmail-folders.pl
Patch0:       dbmail.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, make, gcc, pkgconfig
PreReq:       OpenPKG, openpkg >= 20160101, MTA, perl
BuildPreReq:  libiconv, gettext, gc, glib, gmime22
PreReq:       libiconv, gettext, gc, glib, gmime22
%if "%{with_fsl}" == "yes"
BuildPreReq:  fsl
PreReq:       fsl
%endif
%if "%{with_sieve}" == "yes"
BuildPreReq:  libsieve >= 2.2
PreReq:       libsieve >= 2.2
%endif
%if "%{with_sqlite}" == "yes"
BuildPreReq:  sqlite >= 3.4.1
PreReq:       sqlite >= 3.4.1
%endif
%if "%{with_mysql}" == "yes"
BuildPreReq:  mysql, mysql::with_innobase = yes, zlib
PreReq:       mysql, mysql::with_innobase = yes, zlib
%endif
%if "%{with_pgsql}" == "yes"
BuildPreReq:  postgresql >= 8.2.4-20070906, openssl
PreReq:       postgresql >= 8.2.4-20070906, openssl
%endif
%if "%{with_ldap}" == "yes"
BuildPreReq:  openldap, openssl
PreReq:       openldap, openssl
%endif

%description
    DBMail is a collection of programs that enables Email to be
    stored in and retrieved from a RDBMS. Currently SQLite, MySQL
    and PostgreSQL can be used as RDBMS. Advantages of DBMail are
    in the areas of scalability, manageability, speed, security and
    flexibility.

%track
    prog dbmail = {
        version   = %{V_major}.%{V_minor}
        url       = http://www.dbmail.org/index.php?page=download
        regex     = dbmail-(\d+\.\d*[02468](\.\d+)+)\.tar\.gz
    }

%prep
    %setup -q -n dbmail-%{V_major}.%{V_minor}
    %patch -p0

%build
    export CC="%{l_cc}"
    export CFLAGS="%{l_cflags -O}"
    export CPPFLAGS="%{l_cppflags gmime-2.0 .}"
    export LDFLAGS="%{l_ldflags} %{l_fsl_ldflags}"
    export LIBS="%{l_fsl_libs}"
%if "%{with_pgsql}" == "yes"
    LIBS="$LIBS -lssl -lcrypto"
%endif
%if "%{with_ldap}" == "yes"
    LIBS="$LIBS -llber -lssl -lcrypto"
%endif
    ./configure \
        --prefix=%{l_prefix} \
        --mandir=%{l_prefix}/man \
        --sysconfdir=%{l_prefix}/etc/dbmail \
        --localstatedir=%{l_prefix}/var/dbmail/run \
        --with-logdir=%{l_prefix}/var/dbmail/log \
        --with-gc=%{l_prefix} \
%if "%{with_sieve}" == "yes"
        --with-sieve=%{l_prefix}/include \
%endif
%if "%{with_sqlite}" == "yes"
        --with-sqlite \
%else
        --without-sqlite \
%endif
%if "%{with_mysql}" == "yes"
        --with-mysql \
%else
        --without-mysql \
%endif
%if "%{with_pgsql}" == "yes"
        --with-pgsql \
%else
        --without-pgsql \
%endif
%if "%{with_ldap}" == "yes"
        --with-auth-ldap=%{l_prefix}/include \
%else
        --without-auth-ldap \
%endif
        --disable-shared \
        --enable-static
    %{l_make} %{l_mflags -O}

%install

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

    #   create additional directories
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/dbmail \
        $RPM_BUILD_ROOT%{l_prefix}/var/dbmail/db \
        $RPM_BUILD_ROOT%{l_prefix}/var/dbmail/log \
        $RPM_BUILD_ROOT%{l_prefix}/var/dbmail/run

    #   strip down installation
    strip $RPM_BUILD_ROOT%{l_prefix}/sbin/* >/dev/null 2>&1 || true
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/lib/dbmail >/dev/null 2>&1 || true

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

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

    #   install database setup stuff
%if "%{with_sqlite}" == "yes"
    db_type='sqlite'
    db_script='sql/sqlite/create_tables.sqlite'
%endif
%if "%{with_mysql}" == "yes"
    db_type='mysql'
    db_script='sql/mysql/create_tables.mysql'
%endif
%if "%{with_pgsql}" == "yes"
    db_type='pgsql'
    db_script='sql/postgresql/create_tables.pgsql'
%endif
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/share/dbmail
    %{l_shtool} install -c -m 644 \
        $db_script $RPM_BUILD_ROOT%{l_prefix}/share/dbmail/dbmail-setup.sql
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        -e "s;@l_bash@;%{l_bash};g" \
        -e "s;@db_type@;$db_type;g" \
        %{SOURCE dbmail-setup.sh} $RPM_BUILD_ROOT%{l_prefix}/sbin/dbmail-setup

    #   install folder creation utility
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE dbmail-folders.pl} $RPM_BUILD_ROOT%{l_prefix}/sbin/dbmail-folders

    #   install default configuration file
    %{l_shtool} install%{l_nil} -c -m 600 %{l_value -s -a} \
        -e 's;[ 	]*$;;' \
        -e "s;^\\(driver[^=]*=\\).*$;\\1 $db_type;" \
%if "%{with_ldap}" == "yes"
        -e 's;^\(authdriver[^=]*=\).*$;\1 ldap;' \
%else
        -e 's;^\(authdriver[^=]*=\).*$;\1 sql;' \
%endif
%if "%{with_sqlite}" == "yes"
        -e 's;^\(user[^=]*=\).*$;\1 %{l_rusr};' \
        -e 's;^\(pass[^=]*=\).*$;\1 %{l_rgrp};' \
        -e 's;^\(db[^=]*=\).*$;\1 %{l_prefix}/var/dbmail/db/dbmail.db;' \
%else
        -e 's;^\(user[^=]*=\).*$;\1 dbmail;' \
        -e 's;^\(pass[^=]*=\).*$;\1 dbmail;' \
        -e 's;^\(db[^=]*=\).*$;\1 dbmail;' \
%endif
        -e 's;^\(sendmail[^=]*=\).*$;\1 %{l_prefix}/sbin/sendmail;' \
        -e 's;^\(EFFECTIVE_USER[^=]*=\).*$;\1 %{l_rusr};' \
        -e 's;^\(EFFECTIVE_GROUP[^=]*=\).*$;\1 %{l_rgrp};' \
        -e 's;^\(BINDIP[^=]*=\).*$;\1 127.0.0.1;' \
        -e 's;^\(RESOLVE_IP[^=]*=\).*$;\1 no;' \
        -e 's;^\(logfile[^=]*=\).*$;\1 %{l_prefix}/var/dbmail/log/dbmail-output.log;' \
        -e 's;^\(errorlog[^=]*=\).*$;\1 %{l_prefix}/var/dbmail/log/dbmail-output.log;' \
        -e 's;^\(pid_directory[^=]*=\).*$;\1 %{l_prefix}/var/dbmail/run;' \
        -e 's;^\(state_directory[^=]*=\).*$;\1 %{l_prefix}/var/dbmail/run;' \
        -e 's;dc=mydomain;dc=example;g' \
        dbmail.conf $RPM_BUILD_ROOT%{l_prefix}/etc/dbmail/

    #   install additional setup documentation
    %{l_shtool} install%{l_nil} -c -m 644 \
%if "%{with_sieve}" == "yes"
        README.sieve \
%endif
        README.{aliases,exim,postfix,sieve,smtp,usermap} \
        $RPM_BUILD_ROOT%{l_prefix}/share/dbmail/

    #   determine installation files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT %{l_files_std} \
        '%config %attr(0640,%{l_musr},%{l_rgrp}) %{l_prefix}/etc/dbmail/*' \
        '%config %{l_prefix}/etc/fsl/fsl.dbmail' \
        '%doc %{l_prefix}/share/dbmail/README.*' \
        '%dir %attr(0770,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/dbmail/*'

%files -f files

%clean

%post
    if [ $1 -eq 1 ]; then
%if "%{with_sqlite}" == "yes"
        ( echo "Establishing initial DBMail SQLite database"
        ) | %{l_rpmtool} msg -b -t notice
        $RPM_INSTALL_PREFIX/sbin/dbmail-setup install
%endif
        #   display final hints on initial installation
        ( echo "First, please adjust the configuration file"
          echo "    $RPM_INSTALL_PREFIX/etc/dbmail/dbmail.conf"
          echo "especially in terms of database configuration because"
          echo "those settings are relevant to the dbmail-setup utility."
          echo "To complete this installation of DBMail,"
%if "%{with_sqlite}" != "yes"
%if "%{with_mysql}" == "yes"
          echo "after starting your MySQL RDBMS with"
          echo "  \$ $RPM_INSTALL_PREFIX/bin/openpkg rc mysql start"
%endif
%if "%{with_pgsql}" == "yes"
          echo "after starting your PostgreSQL RDBMS with"
          echo "  \$ $RPM_INSTALL_PREFIX/bin/openpkg rc postgresql start"
%endif
          echo "please establish the initial DBMail database with:"
          echo "  \$ $RPM_INSTALL_PREFIX/sbin/dbmail-setup install"
%endif
          echo "Then hook DBMail into your particular MTA manually."
          echo "For instance for the Postfix MTA this is achieved via:"
          echo "  # transport:"
          echo "  <domain> lmtp:127.0.0.1:24"
          echo "Finally, add each of your users to DBMail with:"
          echo "  \$ $RPM_INSTALL_PREFIX/sbin/dbmail-users \\%{l_nil}"
          echo "    -a <username> -w <password> -s <user>@<domain>"
        ) | %{l_rpmtool} msg -b -t notice
    elif [ $1 -eq 2 ]; then
        #   after upgrade, restart service
        [ $1 -eq 2 ] || exit 0
        eval `%{l_rc} dbmail status 2>/dev/null`
        [ ".$dbmail_active" = .yes ] && %{l_rc} dbmail restart
    fi
    exit 0

%preun
    if [ $1 -eq 0 ]; then
        #   before erase, stop service and remove log files
        [ $1 -eq 0 ] || exit 0
        %{l_rc} dbmail stop 2>/dev/null
        $RPM_INSTALL_PREFIX/sbin/dbmail-setup uninstall >/dev/null 2>&1 || true
        rm -f $RPM_INSTALL_PREFIX/var/dbmail/db/*  >/dev/null 2>&1 || true
        rm -f $RPM_INSTALL_PREFIX/var/dbmail/run/* >/dev/null 2>&1 || true
        rm -f $RPM_INSTALL_PREFIX/var/dbmail/log/* >/dev/null 2>&1 || true
    fi
    exit 0

