##
##  bind.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_opkg  9.18.27
%define       V_dist  9.18.27

#   package information
Name:         bind
Summary:      Berkeley Internet Name Domain (BIND)
URL:          https://www.isc.org/software/bind
Vendor:       Internet Software Foundation
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        BASE
Group:        DNS
License:      ISC
Version:      %{V_opkg}
Release:      20240516

#   package options
%option       with_dnssec    no
%option       with_pgsql     no
%option       with_mysql     no
%option       with_odbc      no
%option       with_bdb       no
%option       with_fs        no
%option       with_ldap      no
%option       with_readline  no
%option       with_http      no

#   list of sources
Source0:      ftp://ftp.isc.org/isc/bind9/%{V_dist}/bind-%{V_dist}.tar.xz
Source1:      bind.txt
Source2:      rc.bind
Patch0:       bind.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, make, pkgconfig
PreReq:       OpenPKG, openpkg >= 20160101
BuildPreReq:  libuv, jemalloc
PreReq:       libuv, jemalloc
%if "%{with_dnssec}" == "yes"
BuildPreReq:  openssl, python, python-parse
PreReq:       openssl, python, python-parse
%endif
%if "%{with_pgsql}" == "yes"
BuildPreReq:  postgresql, openssl
PreReq:       postgresql, openssl
%endif
%if "%{with_mysql}" == "yes"
BuildPreReq:  mysql
PreReq:       mysql
%endif
%if "%{with_odbc}" == "yes"
BuildPreReq:  ODBC
PreReq:       ODBC
%endif
%if "%{with_bdb}" == "yes"
BuildPreReq:  db
PreReq:       db
%endif
%if "%{with_ldap}" == "yes"
BuildPreReq:  openldap, openssl
PreReq:       openldap, openssl
%endif
%if "%{with_readline}" == "yes"
BuildPreReq:  readline
PreReq:       readline
%endif
%if "%{with_http}" == "yes"
BuildPreReq:  nghttp2
PreReq:       nghttp2
%endif
Conflicts:    powerdns

%description
    Berkeley Internet Name Domain (BIND) is an implementation of
    the Domain Name System (DNS) protocols and provides an openly
    redistributable reference implementation of the major components
    of the Domain Name System, including a Domain Name System server
    (named)

%track
    prog bind = {
        version   = %{V_dist}
        url       = ftp://ftp.isc.org/isc/bind9/
        regex     = (9.[12][02468]\.\d+(?:-P\d+)?)[^br]
    }

%prep
    %setup -q -n bind-%{V_dist}
    %patch -p0

%build
    #   configure program
    export CC="%{l_cc}"
    export CFLAGS="%{l_cflags -O} %{l_cppflags} -Wl,-u,isc__initialize,-u,isc__shutdown"
    export LDFLAGS="%{l_ldflags}"
    export LIBS=""
%if "%{with_pgsql}" == "yes"
    CFLAGS="$CFLAGS %{l_cppflags postgresql .}"
%endif
%if "%{with_dnssec}" == "yes"
    LIBS="$LIBS `pkg-config openssl --libs`"
%endif
%if "%{with_readline}" == "yes"
    LIBS="$LIBS `pkg-config readline --libs`"
%endif
    GREP="grep" \
    ./configure \
        --prefix=%{l_prefix} \
        --mandir=%{l_prefix}/man \
        --sysconfdir=%{l_prefix}/etc/bind \
        --localstatedir=%{l_prefix}/var/bind \
%if "%{with_dnssec}" == "yes"
        --with-openssl=%{l_prefix} \
%endif
%if "%{with_pgsql}" == "yes"
        --with-dlz-postgres=%{l_prefix} \
%endif
%if "%{with_mysql}" == "yes"
        --with-dlz-mysql=%{l_prefix} \
%endif
%if "%{with_odbc}" == "yes"
        --with-dlz-odbc=%{l_prefix} \
%endif
%if "%{with_bdb}" == "yes"
        --with-dlz-bdb=%{l_prefix} \
%endif
%if "%{with_fs}" == "yes"
        --with-dlz-filesystem \
%endif
%if "%{with_ldap}" == "yes"
        --with-dlz-ldap=%{l_prefix} \
%endif
%if "%{with_readline}" == "yes"
        --with-readline \
%else
        --without-readline \
%endif
%if "%{with_http}" == "yes"
        --enable-doh \
        --with-libnghttp2 \
%else
        --disable-doh \
%endif
        --enable-pthread-rwlock \
        --disable-linux-caps \
        --with-jemalloc \
        --without-libxml2 \
        --without-gssapi \
        --disable-geoip \
        --enable-static \
        --disable-shared

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

%install
    #   perform standard install procedure
    %{l_make} %{l_mflags} install DESTDIR=$RPM_BUILD_ROOT

    #   strip down installation
    strip $RPM_BUILD_ROOT%{l_prefix}/sbin/* >/dev/null 2>&1 || true
    strip $RPM_BUILD_ROOT%{l_prefix}/bin/* >/dev/null 2>&1 || true
    rm -f $RPM_BUILD_ROOT%{l_prefix}/bin/isc-config.sh
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/include
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/lib
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/man/man3
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/var/bind/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.bind} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d/

    #   install default configuration
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/bind \
        $RPM_BUILD_ROOT%{l_prefix}/etc/bind/named.db
    for name in `grep "^<file" %{SOURCE bind.txt} | sed -e 's;^.*name=";;' -e 's;".*$;;'`; do
        name_escaped=`echo "$name" | sed -e 's;/;\\\\/;g'`
        (echo ""; cat %{SOURCE bind.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/bind/$name
    done

    #   create an empty var directory
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/var/bind/log \
        $RPM_BUILD_ROOT%{l_prefix}/var/bind/run

    #   determine the installed files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std} \
        '%config %attr(640,%{l_musr},%{l_rgrp}) %{l_prefix}/etc/bind/*.conf' \
        '%config %attr(775,%{l_musr},%{l_rgrp}) %{l_prefix}/etc/bind/named.db' \
        '%config %attr(644,%{l_musr},%{l_rgrp}) %{l_prefix}/etc/bind/named.db/*' \
        '%dir %attr(775,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/bind' \
        '%dir %attr(775,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/bind/*'

%files -f files

%clean

%post
    if [ ! -f $RPM_INSTALL_PREFIX/etc/bind/rndc.key ]; then
        #   generate local rndc(8) key
        ( echo "Generating RSA key for RNDC operation in $RPM_INSTALL_PREFIX/etc/bind/rndc.key."
          echo "Please be patient, this takes a non-deterministic amount of time."
        ) | %{l_rpmtool} msg -b -t notice
        $RPM_INSTALL_PREFIX/sbin/rndc-confgen -a
        chown %{l_musr}:%{l_rgrp} $RPM_INSTALL_PREFIX/etc/bind/rndc.key
        chmod 640 $RPM_INSTALL_PREFIX/etc/bind/rndc.key
    fi

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

%preun
    #   before erase, stop service and remove log files
    [ $1 -eq 0 ] || exit 0
    %{l_rc} bind stop 2>/dev/null
    rm -f $RPM_INSTALL_PREFIX/etc/bind/rndc.key
    rm -rf $RPM_INSTALL_PREFIX/var/bind/*/*
    exit 0

