##
##  stun.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_dist  0.97
%define       V_opkg  0.97

#   package information
Name:         stun
Summary:      STUN Client & Server
URL:          http://www.vovida.org/applications/downloads/stun/
Vendor:       Vovida
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        VoIP
License:      Open Source
Version:      %{V_opkg}
Release:      20150104

#   list of sources
Source0:      http://download.sourceforge.net/stun/stund-%{V_dist}.tgz
Source1:      rc.stun
Patch0:       stun.patch

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

%description
    The STUN (Simple Traversal of UDP through NATs (Network Address
    Translation)) server is an implementation of the STUN protocol that
    enables STUN functionality in SIP-based systems. The STUN server
    tar ball also include a client API to enable STUN functionality in
    SIP endpoints. In addition there is a command line UNIX client and
    a graphical windows client that check what type of NAT the user is
    using. STUN is an application-layer protocol that can determine the
    public IP and nature of a NAT device that sits between the STUN
    client and STUN server. The current version of the code supports
    most of RFC 3489 except the ability to get OTPs from the server.

%track
    prog stun = {
        version   = %{V_dist}
        url       = http://sourceforge.net/projects/stun/files/
        regex     = stund-(.+?)\.tgz
    }

%prep
    %setup -q -n stund
    %patch -p0

%build
    %{l_make} %{l_mflags} \
        CXX="%{l_cxx}" \
        CXXFLAGS="%{l_cxxflags -O}" \
        LDFLAGS=""

%install
    %{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}/var/stun
    %{l_shtool} install -c -s -m 755 \
        client $RPM_BUILD_ROOT%{l_prefix}/bin/stun
    %{l_shtool} install -c -s -m 755 \
        server $RPM_BUILD_ROOT%{l_prefix}/sbin/stund
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE rc.stun} $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d/
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT %{l_files_std}

%files -f files

%clean

%post
    if [ $1 -eq 1 ]; then
        #   display final hints on initial installation
        ( echo "Before starting the STUN daemon, please set the configuration"
          echo "variables \"stun_bind_addr{1,2}\" in $RPM_INSTALL_PREFIX/etc/rc.conf"
          echo "to two *different* IP addresses bound to your host. STUN requires this."
        ) | %{l_rpmtool} msg -b -t notice
    fi

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

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

