##
##  quagga.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:         quagga
Summary:      Internet Routing Daemon
URL:          http://www.quagga.net/
Vendor:       Kunihiro Ishiguro
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        BASE
Group:        Routing
License:      GPL
Version:      1.2.4
Release:      20180220

#   package options
%option       with_ipv6  yes

#   list of sources
Source0:      http://download-mirror.savannah.gnu.org/releases/quagga/quagga-%{version}.tar.gz
Source1:      rc.quagga
Source2:      bgpd.conf
Source3:      ospfd.conf
Source4:      ripd.conf
Source5:      zebra.conf
Source6:      vtysh.conf
Patch0:       quagga.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, gcc, perl, make, gawk
PreReq:       OpenPKG, openpkg >= 20160101
BuildPreReq:  readline
PreReq:       readline
Conflicts:    zebra

%description
    Quagga (a fork of GNU Zebra) is free software (distributed under GNU
    Generic Public License) that manages TCP/IP based routing protocols.
    It supports BGP-4 protocol as described in RFC1771 (A Border Gateway
    Protocol 4) as well as RIPv1, RIPv2 and OSPFv2. Unlike traditional,
    Gated based, monolithic architectures and even the so-called "new
    modular architectures" that remove the burden of processing routing
    functions from the CPU and utilize special ASIC chips instead, Zebra
    software offers true modularity.

%track
    prog quagga = {
        version   = %{version}
        url       = http://download.savannah.gnu.org/releases/quagga/
        regex     = quagga-(__VER__)\.tar\.gz
    }

%prep
    %setup -q
    %patch -p0

%build
    #   configure package
    autoreconf
    ISISD=""
    case "%{l_platform -t}" in
        *-netbsd* ) ISISD="--disable-isisd" ;;
        *-sunos*  ) ISISD="--disable-isisd" ;;
    esac
    CC="%{l_cc}" \
    CFLAGS="%{l_cflags -O}" \
    CPPFLAGS="%{l_cppflags}" \
    LDFLAGS="%{l_ldflags}" \
    LIBS="-lm" \
    MAKEINFO="true" \
    ./configure \
        --prefix=%{l_prefix} \
        --mandir=%{l_prefix}/man \
        --sysconfdir=%{l_prefix}/etc/quagga \
        --localstatedir=%{l_prefix}/var/quagga \
        --enable-vtysh \
%if "%{with_ipv6}" == "yes"
        --enable-ospf6d \
%else
        --disable-ospf6d \
%endif
        --disable-ripngd \
        --without-libpam \
        --disable-shared \
        $ISISD

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

%install

    #   install package
    %{l_make} %{l_mflags} install AM_MAKEFLAGS="DESTDIR=$RPM_BUILD_ROOT"

    #   strip down package
    rm -f $RPM_BUILD_ROOT%{l_prefix}/man/man8/ospf6d.8
    rm -f $RPM_BUILD_ROOT%{l_prefix}/man/man8/ripngd.8
    rm -f $RPM_BUILD_ROOT%{l_prefix}/info/dir
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/include
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/lib
    strip $RPM_BUILD_ROOT%{l_prefix}/sbin/* >/dev/null 2>&1 || true

    #   replace default configuration
    rm -f $RPM_BUILD_ROOT%{l_prefix}/etc/quagga/*
    %{l_shtool} install -c -m 600 %{l_value -s -a} \
        %{SOURCE zebra.conf} %{SOURCE vtysh.conf} \
        %{SOURCE ripd.conf} %{SOURCE ospfd.conf} %{SOURCE bgpd.conf} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/quagga/
    %{l_shtool} install -c -m 600 /dev/null \
        $RPM_BUILD_ROOT%{l_prefix}/etc/quagga/zebra.conf.integrate

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

    #   make sure pid and log directory exists
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/var/quagga

    #   determine package file list
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std} \
        '%config %{l_prefix}/etc/quagga/*'

%files -f files

%clean

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

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

