##
##  fetchmail.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:         fetchmail
Summary:      Batch Client for POP and IMAP Protocols
URL:          http://www.fetchmail.info/
Vendor:       Eric S. Raymond
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        BASE
Group:        Mail
License:      GPL
Version:      6.5.6
Release:      20251005

#   package options
%option       with_conf  no

#   list of sources
Source0:      http://sourceforge.net/projects/fetchmail/files/branch_6.5/fetchmail-%{version}.tar.xz
Source1:      rc.fetchmail
Source2:      fetchmail.master
Source3:      fetchmail.users
Patch0:       fetchmail.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, make, flex, bison, config
PreReq:       OpenPKG, openpkg >= 20160101, sed
BuildPreReq:  openssl
PreReq:       openssl
%if "%{with_conf}" == "yes"
PreReq:       python
%endif

%description
    Fetchmail is a full-featured, robust, well-documented remote-mail
    retrieval and forwarding utility intended to be used over on-demand
    TCP/IP links (such as SLIP or PPP connections). It supports every
    remote-mail protocol now in use on the Internet: POP2, POP3, RPOP,
    APOP, KPOP, all flavors of IMAP, and ESMTP ETRN. It can even support
    IPv6 and IPSEC.

%track
    prog fetchmail = {
        version   = %{version}
        url       = http://sourceforge.net/projects/fetchmail/files/branch_6.4/
        regex     = fetchmail-(\d+\.\d+\.\d+)\.tar\.(gz|bz2|xz)
    }

%prep
    %setup -q
    %patch -p0

%build
    #   configure program
    CC="%{l_cc}" \
    CFLAGS="%{l_cflags -O} %{l_cppflags openssl3 .}" \
    CPPFLAGS="%{l_cppflags openssl3 .}" \
    LDFLAGS="%{l_ldflags openssl3 .}" \
    GREP="grep" \
    ./configure \
        --prefix=%{l_prefix} \
        --mandir=%{l_prefix}/man \
        --with-ssl=%{l_prefix} \
        --without-kerberos \
        --without-kerberos5 \
        --without-hesiod \
        --disable-nls

    #   build program
    %{l_make} %{l_mflags}

%install
    #   install program
    %{l_make} %{l_mflags} install DESTDIR=$RPM_BUILD_ROOT

    #   strip down installation
    strip $RPM_BUILD_ROOT%{l_prefix}/bin/* >/dev/null 2>&1 || true
%if "%{with_conf}" != "yes"
    rm -f  $RPM_BUILD_ROOT%{l_prefix}/bin/fetchmailconf
    rm -f  $RPM_BUILD_ROOT%{l_prefix}/man/man1/fetchmailconf.1
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/lib/python
%endif

    #   create global service run-time directory
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/var/fetchmail

    #   install global service configuration files
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/fetchmail
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE fetchmail.master} %{SOURCE fetchmail.users} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/fetchmail/

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

    #   determine installation files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std} \
        '%config %{l_prefix}/etc/fetchmail/fetchmail.master' \
        '%config %{l_prefix}/etc/fetchmail/fetchmail.users'

%files -f files

%clean

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

%preun
    #   before erase, stop service
    [ $1 -eq 0 ] || exit 0
    %{l_rc} fetchmail stop 2>/dev/null
    exit 0

