##
##  dhcp-agent.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.
##

#   FIXME: mlelstv: fails to configure interfaces on FreeBSD

#   package information
Name:         dhcp-agent
Summary:      DHCP Client Suite
URL:          http://dhcp-agent.sourceforge.net/
Vendor:       Thamer Alharbash
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        DHCP
License:      MIT-style
Version:      0.41
Release:      20080101

#   package options
%option       with_fsl  yes

#   list of sources
Source0:      http://download.sourceforge.net/dhcp-agent/dhcp-agent-%{version}.tar.gz
Source1:      rc.dhcp-agent
Source2:      fsl.dhcp-agent
Patch0:       dhcp-agent.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, gcc
PreReq:       OpenPKG, openpkg >= 20160101
BuildPreReq:  libdnet, libpcap, guile
PreReq:       libdnet, libpcap, guile
%if "%{with_fsl}" == "yes"
BuildPreReq:  fsl
PreReq:       fsl
%endif

%description
    dhcp-agent is a portable UNIX Dynamic Host Configuration suite. It
    provides a DHCP client which supports basic network setup out of the
    box and a DHCP sniffer for diagnostic purposes.

%track
    prog dhcp-agent = {
        version   = %{version}
        url       = http://dhcp-agent.sourceforge.net/
        regex     = dhcp-agent-(__VER__)\.tar\.gz
    }

%prep
    %setup -q
    %patch -p0

    #   add some missing $(DESTDIR) occurrences
    %{l_shtool} subst \
        -e 's;mkdir -p .*\(${dhcplocalstate_clientdir}\);%{l_shtool} mkdir -p $(DESTDIR)\1;' \
        -e 's; \(${dhcplocalstate_serverdir}\); $(DESTDIR)\1;' \
        src/Makefile.in

    #   fix inconsistently renamed API
    %{l_shtool} subst \
        -e 's;FATAL_ERROR;FATAL_MESSAGE;g' \
        src/dhcp-daemon.c

%build
    #   configure package
    defs=""
    case "%{l_platform -t}" in
        *-linux2.2 ) defs="-DNEED_IN_ADDR_T" ;;
    esac
    CC="%{l_cc}" \
    CFLAGS="%{l_cflags -O} $defs" \
    LDFLAGS="-L. %{l_fsl_ldflags}" \
    ./configure \
        --prefix=%{l_prefix} \
        --with-libdnet-prefix=%{l_prefix} \
        --with-libpcap-prefix=%{l_prefix} \
        --disable-shared
    %{l_shtool} subst \
        -e 's;^LIBS.*=.*;LIBS=%{l_fsl_libs};' \
        src/Makefile

    #   build package
    %{l_make} %{l_mflags}

%install

    #   install package
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/var/dhcp-agent
    %{l_make} %{l_mflags} install AM_MAKEFLAGS="DESTDIR=$RPM_BUILD_ROOT"

    #   drop underdocumented underimplemented server
    rm -rf  $RPM_BUILD_ROOT%{l_prefix}/etc/dhcp-agent/dhcp-server
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/var/dhcp-agent/dhcp-server
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/sbin/dhcp-server

    #   remove fsl dependency from libtool files
    %{l_shtool} subst \
        -e 's;-lfsl *;;' \
        $RPM_BUILD_ROOT%{l_prefix}/lib/*.la

    #   post-adjust installation
    rm -f $RPM_BUILD_ROOT%{l_prefix}/sbin/dhclient-server
    strip $RPM_BUILD_ROOT%{l_prefix}/sbin/* >/dev/null 2>&1 || true

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

    #   install OSSP fsl configuration
    %{l_shtool} mkdir -f -p -m 755 $RPM_BUILD_ROOT%{l_prefix}/etc/fsl
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE fsl.dhcp-agent} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/fsl/

    #   determine installation files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std} \
        '%config %{l_prefix}/etc/fsl/fsl.dhcp-agent'

%files -f files

%clean

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

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

