##
##  maradns.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_major 2.0
%define       V_minor 17

#   package information
Name:         maradns
Summary:      Authoritative & Caching DNS Server
URL:          http://www.maradns.org/
Vendor:       Sam Trenholme
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        DNS
License:      MIT-style
Version:      %{V_major}.%{V_minor}
Release:      20190121

#   package options
%option       with_authonly  no

#   list of sources
Source0:      http://www.maradns.org/download/%{V_major}/%{version}/maradns-%{version}.tar.bz2
Source1:      rc.maradns
Source2:      mararc

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

%description
    MaraDNS is a secure, full-featured and easy to use Domain Name
    Service (DNS) server supporting both the authoritative and recursive
    resolving and caching operations.

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

%prep
    %setup -q

%build
    #   configure program
%if "%{with_authonly}" == "yes"
    ./configure --authonly || true
%else
    ./configure || true
%endif

    #   build program
    %{l_make} %{l_mflags}

%install
    #   create installation hierarchy
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/bin \
        $RPM_BUILD_ROOT%{l_prefix}/sbin \
        $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d \
        $RPM_BUILD_ROOT%{l_prefix}/etc/maradns \
        $RPM_BUILD_ROOT%{l_prefix}/var/maradns \
        $RPM_BUILD_ROOT%{l_prefix}/man/man1 \
        $RPM_BUILD_ROOT%{l_prefix}/man/man5 \
        $RPM_BUILD_ROOT%{l_prefix}/man/man8

    #   install program
    %{l_shtool} subst -e 's;RPM_BUILD_ROOT;PREFIX;g' build/install.sh
    export PREFIX=$RPM_BUILD_ROOT%{l_prefix}
    export LANGUAGE=en
    %{l_make} %{l_mflags} install

    #   strip down installation
    strip $RPM_BUILD_ROOT%{l_prefix}/bin/* >/dev/null 2>&1 || true
    strip $RPM_BUILD_ROOT%{l_prefix}/sbin/* >/dev/null 2>&1 || true
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/doc
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/etc/maradns/logger
    rm -f $RPM_BUILD_ROOT%{l_prefix}/etc/mararc

    #   install default configuration
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE mararc} $RPM_BUILD_ROOT%{l_prefix}/etc/maradns/
    sed -e 's;net;com;' -e 's;NET;COM;' \
        <$RPM_BUILD_ROOT%{l_prefix}/etc/maradns/db.example.net \
        >$RPM_BUILD_ROOT%{l_prefix}/etc/maradns/db.example.com

    #   install run-command script
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE rc.maradns} $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/maradns/*' \
        '%attr(755,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/maradns'

%files -f files

%clean

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

%preun
    #   before erase, stop service and remove log files
    [ $1 -eq 0 ] || exit 0
    %{l_rc} maradns stop 2>/dev/null
    rm -f $RPM_INSTALL_PREFIX/var/maradns/* >/dev/null 2>&1 || true
    exit 0

