##
##  mutt.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:         mutt
Summary:      Mail User Agent
URL:          http://www.mutt.org/
Vendor:       Michael Elkins
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        BASE
Group:        Mail
License:      BSD
Version:      2.2.15
Release:      20251003

#   build options
%option       with_pop       yes
%option       with_imap      yes
%option       with_smtp      yes
%option       with_sasl      yes
%option       with_ssl       yes
%option       with_ncurses   yes
%option       with_hcache    no
%option       with_sidebar   no

#   list of sources
Source0:      ftp://ftp.mutt.org/pub/mutt/mutt-%{version}.tar.gz
Source1:      mailcap
Source2:      muttrc
Patch1:       mutt.patch.sidebar
Patch2:       mutt.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, make, perl, pkgconfig
PreReq:       OpenPKG, openpkg >= 20160101
BuildPreReq:  libidn, libiconv
PreReq:       libidn, libiconv
%if "%{with_ssl}" == "yes"
BuildPreReq:  openssl
PreReq:       openssl
%endif
%if "%{with_sasl}" == "yes"
BuildPreReq:  sasl
PreReq:       sasl
%endif
%if "%{with_hcache}" == "yes"
BuildPreReq:  qdbm
PreReq:       qdbm
%endif
%if "%{with_ncurses}" == "yes"
BuildPreReq:  ncurses
PreReq:       ncurses
%else
BuildPreReq:  slang
PreReq:       slang
%endif

%description
    Mutt is a small but very powerful text based program for reading
    electronic mail under Unix operating systems, including support for
    color terminals, MIME, and a threaded sorting mode.

%track
    prog mutt = {
        version   = %{version}
        url       = http://www.mutt.org/download.html
        regex     = mutt-(__VER__)\.tar\.gz
    }

%prep
    %setup -q
%if "%{with_sidebar}" == "yes"
    %patch -p1 -P 1
%endif
    %patch -p0 -P 2
    %{l_shtool} subst \
        -e 's;\(Mutt/%%s\);\1 %{l_openpkg_release -F "OpenPKG/%%t"};' \
        sendlib.c

%build
    #   configure for particular platform
    options=''
    export CC="%{l_cc}"
    export CFLAGS="%{l_cflags -O}"
%if "%{with_ncurses}" == "yes"
    export CPPFLAGS="%{l_cppflags ncurses libidn .}"
%else
    export CPPFLAGS="%{l_cppflags libidn .}"
%endif
    export LDFLAGS="-Limap %{l_ldflags}"
    export LIBS=""
    export INSTALL="%{l_shtool} install%{l_nil} -c"
    case "%{l_platform -t}" in
        *-freebsd* ) options="--enable-flock --disable-fcntl" ;;
        *-linux*   ) LIBS="$LIBS -ldl" ;;
    esac
%if "%{with_sasl}" == "yes"
    CFLAGS="$CFLAGS `pkg-config sasl --cflags`"
    LDFLAGS="$LDFLAGS `pkg-config sasl --libs-only-L`"
    LIBS="$LIBS `pkg-config sasl --libs-only-l`"
%endif
    ./configure \
        --prefix=%{l_prefix} \
        --mandir=%{l_prefix}/man \
        --sysconfdir=%{l_prefix}/etc/mutt \
        --with-docdir=%{l_prefix}/share/mutt \
%if "%{with_ncurses}" == "yes"
        --with-curses=%{l_prefix} \
%else
        --with-slang=%{l_prefix} \
%endif
%if "%{with_pop}" == "yes"
        --enable-pop \
%endif
%if "%{with_imap}" == "yes"
        --enable-imap \
%endif
%if "%{with_smtp}" == "yes"
        --enable-smtp \
%endif
%if "%{with_sasl}" == "yes"
        --with-sasl=%{l_prefix} \
%endif
%if "%{with_ssl}" == "yes"
        --with-ssl=%{l_prefix} \
%endif
%if "%{with_hcache}" == "yes"
        --enable-hcache \
        --with-qdbm=%{l_prefix} \
%endif
        --enable-pgp \
        --enable-smime \
        --enable-iconv \
        --disable-warnings \
        --disable-nls \
        $options

    #   perform standard build procedure
    %{l_make} %{l_mflags}

%install

    #   perform standard installation procedure
    %{l_shtool} subst \
        -e "s;chmod 2755;true;" \
        -e "s;chgrp mail;true;" \
        Makefile
    %{l_make} %{l_mflags} install DESTDIR=$RPM_BUILD_ROOT

    #   strip down installation tree
    rm -f  $RPM_BUILD_ROOT%{l_prefix}/share/mutt/[A-Za-d]*
    rm -f  $RPM_BUILD_ROOT%{l_prefix}/share/mutt/patch-notes.txt
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/share/mutt/html
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/share/mutt/samples
    rm -f  $RPM_BUILD_ROOT%{l_prefix}/bin/flea
    rm -f  $RPM_BUILD_ROOT%{l_prefix}/man/man1/flea.1
    rm -f  $RPM_BUILD_ROOT%{l_prefix}/bin/muttbug
    rm -f  $RPM_BUILD_ROOT%{l_prefix}/man/man1/muttbug.1
    strip $RPM_BUILD_ROOT%{l_prefix}/bin/* >/dev/null 2>&1 || true

    #   install additional files
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE muttrc} $RPM_BUILD_ROOT%{l_prefix}/etc/mutt/Muttrc
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE mailcap} $RPM_BUILD_ROOT%{l_prefix}/etc/mutt/mailcap

    #   determine installation files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std} \
        '%config %{l_prefix}/etc/mutt/*'

%files -f files

%clean

