##
##  coturn.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:         coturn
Summary:      TURN/STUN Server
URL:          https://github.com/coturn/coturn/
Vendor:       Oleg Moskalenko
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Networking
License:      BSD
Version:      4.7.0
Release:      20250531

#   list of sources
Source0:      https://github.com/coturn/coturn/archive/%{version}.tar.gz
Source1:      turnserver.conf
Source2:      rc.coturn
Patch0:       coturn.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20140101, gcc, make
PreReq:       OpenPKG, openpkg >= 20140101, x509
BuildPreReq:  openssl-threads, sqlite, libevent
PreReq:       openssl-threads, sqlite, libevent

%description
    CoTURN is a combined TURN, STUN and ICE server providing a network
    traffic NAT traversal server and gateway.

%track
    prog coturn = {
        version   = %{version}
        url       = https://github.com/coturn/coturn/releases
        regex     = tree/(\d+\.\d+\.\d+)
    }

%prep
    %setup -q
    %patch -p0

%build
    #   configure program
    CC="%{l_cc}" \
    CFLAGS="%{l_cflags -O}" \
    CPPFLAGS="%{l_cppflags openssl-threads .}" \
    LDFLAGS="%{l_ldflags openssl-threads .} -lssl -lcrypto" \
    ./configure \
        --prefix=%{l_prefix} \
        --disable-shared \
        --disable-nls

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

%install
    #   install program
    %{l_make} %{l_mflags} install DESTDIR=$RPM_BUILD_ROOT PREFIX=%{l_prefix}

    #   strip down installation
    strip $RPM_BUILD_ROOT%{l_prefix}/bin/* >/dev/null 2>&1 || true
    rm -f $RPM_BUILD_ROOT%{l_prefix}/etc/turnserver.conf.default
    rm -f $RPM_BUILD_ROOT%{l_prefix}/info/dir
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/share/doc
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/share/examples
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/var/db

    #   install default configuration
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/coturn
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE turnserver.conf} $RPM_BUILD_ROOT%{l_prefix}/etc/coturn/

    #   install run-command script
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE rc.coturn} $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d/

    #   create run-time directories
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/var/coturn/log \
        $RPM_BUILD_ROOT%{l_prefix}/var/coturn/run \
        $RPM_BUILD_ROOT%{l_prefix}/var/coturn/db

    #   determine installation files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std} \
        '%config %{l_prefix}/etc/coturn/*' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/coturn/*'

%files -f files

%clean

%post
    if [ $1 -eq 1 ]; then
        #   after initial installation, create database
        $RPM_INSTALL_PREFIX/bin/sqlite $RPM_INSTALL_PREFIX/var/coturn/db/turnserver.db \
            <$RPM_INSTALL_PREFIX/share/turnserver/schema.sql
        chmod 600 $RPM_INSTALL_PREFIX/var/coturn/db/turnserver.db
        chown %{l_rusr}:%{l_rgrp} $RPM_INSTALL_PREFIX/var/coturn/db/turnserver.db
    elif [ $1 -eq 2 ]; then
        #   after upgrade, restart service
        eval `%{l_rc} coturn status 2>/dev/null`
        [ ".$coturn_active" = .yes ] && %{l_rc} coturn restart
    fi
    exit 0

%preun
    if [ $1 -eq 0 ]; then
        #   before erase, stop service and remove log files
        %{l_rc} coturn stop 2>/dev/null
        rm -rf $RPM_INSTALL_PREFIX/var/coturn/log/*  >/dev/null 2>&1 || true
        rm -rf $RPM_INSTALL_PREFIX/var/coturn/run/*  >/dev/null 2>&1 || true
        rm -rf $RPM_INSTALL_PREFIX/var/coturn/db/*   >/dev/null 2>&1 || true
    fi
    exit 0

