##
##  unbound.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:         unbound
Summary:      DNS Resolver
URL:          http://unbound.net/
Vendor:       NLnet Labs
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Networking
License:      BSD
Version:      1.24.0
Release:      20250920

#   list of sources
Source0:      http://unbound.net/downloads/unbound-%{version}.tar.gz
Source1:      unbound.txt
Source2:      rc.unbound

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, flex, gcc
PreReq:       OpenPKG, openpkg >= 20160101
BuildPreReq:  openssl, expat
PreReq:       openssl, expat

%description
    Unbound is a validating, recursive, and caching DNS resolver.
    Unbound is designed as a set of modular components, so that also
    DNSSEC (secure DNS) validation and stub-resolvers (that do not
    run as a server, but are linked into an application) are easily
    possible.

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

%prep
    %setup -q

%build
    #   configure program
    CC="%{l_cc}" \
    CFLAGS="%{l_cflags -O}" \
    CPPFLAGS="%{l_cppflags}" \
    LDFLAGS="%{l_ldflags}" \
    ./configure \
        --prefix=%{l_prefix} \
        --mandir=%{l_prefix}/man \
        --with-ssl=%{l_prefix} \
        --with-libexpat=%{l_prefix} \
        --disable-flto \
        --disable-shared

    #   build program
    %{l_make} %{l_mflags -O}

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

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

    #   create run-time directories
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/var/unbound/log \
        $RPM_BUILD_ROOT%{l_prefix}/var/unbound/run

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

    #   install default configuration
    for name in `grep "^<file" %{SOURCE unbound.txt} | sed -e 's;^.*name=";;' -e 's;".*$;;'`; do
        name_escaped=`echo "$name" | sed -e 's;/;\\\\/;g'`
        (echo ""; cat %{SOURCE unbound.txt}; echo "") |\
            sed -e "1,/^<file name=\"$name_escaped\">/d" -e "/<\/file>/,\$d" >tmp.txt
        %{l_shtool} install -c -m 644 %{l_value -s -a} \
            tmp.txt $RPM_BUILD_ROOT%{l_prefix}/etc/unbound/$name
    done

    #   determine installation files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std} \
        '%config %{l_prefix}/etc/unbound/*' \
        '%attr(-,%{l_nusr},%{l_ngrp}) %{l_prefix}/var/unbound/*'

%files -f files

%clean

%post
    if [ $1 -eq 1 ]; then
        #   on initial installation, create keys
        $RPM_INSTALL_PREFIX/sbin/unbound-control-setup >/dev/null 2>&1
    elif [ $1 -eq 2 ]; then
        #   on upgrade, restart server
        eval `%{l_rc} unbound status 2>/dev/null`
        [ ".$unbound_active" = .yes ] && %{l_rc} unbound restart
    fi
    exit 0

%preun
    if [ $1 -eq 0 ]; then
        #   on deinstallation, stoo server and cleanup files
        %{l_rc} unbound stop 2>/dev/null
        rm -f $RPM_INSTALL_PREFIX/etc/unbound/*.pem >/dev/null 2>&1 || true
        rm -f $RPM_INSTALL_PREFIX/etc/unbound/*.key >/dev/null 2>&1 || true
        rm -f $RPM_INSTALL_PREFIX/var/unbound/run/* >/dev/null 2>&1 || true
        rm -f $RPM_INSTALL_PREFIX/var/unbound/log/* >/dev/null 2>&1 || true
    fi
    exit 0

