##
##  janus.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:         janus
Summary:      Janus WebRTC Gateway
URL:          https://janus.conf.meetecho.com/
Vendor:       Meetecho
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Networking
License:      GPL
Version:      1.3.2
Release:      20250710

#   list of sources
Source0:      https://github.com/meetecho/janus-gateway/archive/v%{version}.tar.gz
Source1:      janus.txt
Source2:      rc.janus
Source3:      janus-tls.sh
Patch0:       janus.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, automake, autoconf, libtool, pkgconfig, gengetopt
PreReq:       OpenPKG, openpkg >= 20160101, cfssl, mosquitto
BuildPreReq:  glib, jansson, openssl3, zlib, libnice, libconfig, libsrtp, gengetopt, libmicrohttpd, libwebsockets
PreReq:       glib, jansson, openssl3, zlib, libnice, libconfig, libsrtp, gengetopt, libmicrohttpd, libwebsockets
BuildPreReq:  opus, vorbis-libs, curl, paho, usrsctp, sofia-sip, lua, duktape
PreReq:       opus, vorbis-libs, curl, paho, usrsctp, sofia-sip, lua, duktape

%description
    Janus is a general purpose WebRTC server/gateway.

%track
    prog janus = {
        version   = %{version}
        url       = https://github.com/meetecho/janus-gateway/tags
        regex     = tags/v(\d+\.\d+\.\d+)
    }

%prep
    %setup -q -n janus-gateway-%{version}
    %patch -p0

%build
    #   generate configuration scripts
    sh autogen.sh

    #   configure program
    CC="%{l_cc}" \
    CXX="%{l_cxx}" \
    CFLAGS="%{l_cflags -O}" \
    CXXFLAGS="%{l_cxxflags -O}" \
    CPPFLAGS="%{l_cppflags openssl3 .}" \
    LDFLAGS="%{l_ldflags openssl3 .}" \
    LIBS="-lnice -lgnutls -lcurl -lssl -lcrypto -liconv" \
    %{l_bash} ./configure \
        --prefix=%{l_prefix} \
        --mandir=%{l_prefix}/man \
        --enable-plugin-duktape \
        --enable-plugin-lua \
        --disable-rabbitmq

    #   build program
    %{l_make} %{l_mflags}

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

    #   strip down installation
    rm -f $RPM_BUILD_ROOT%{l_prefix}/info/dir
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/share/janus
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/share/doc
    strip $RPM_BUILD_ROOT%{l_prefix}/bin/* >/dev/null 2>&1 || true

    #   provide custom default configuration
    rm -f $RPM_BUILD_ROOT%{l_prefix}/etc/janus/*
    for name in `grep "^<file" %{SOURCE janus.txt} | sed -e 's;^.*name=";;' -e 's;".*$;;'`; do
        name_escaped=`echo "$name" | sed -e 's;/;\\\\/;g'`
        (echo ""; cat %{SOURCE janus.txt}; echo "") |\
            sed -e "1,/^<file name=\"$name_escaped\">/d" -e "/<\/file>/,\$d" >tmp.txt
        %{l_shtool} install -c -m 644 %{l_value -s -a} \
            tmp.txt $RPM_BUILD_ROOT%{l_prefix}/etc/janus/$name
    done

    #   install utility program
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        -e 's;@l_bash@;%{l_bash};g' \
        %{SOURCE janus-tls.sh} $RPM_BUILD_ROOT%{l_prefix}/bin/janus-tls

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

    #   create runtime directory
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/var/janus/data/voicemail \
        $RPM_BUILD_ROOT%{l_prefix}/var/janus/data/recording \
        $RPM_BUILD_ROOT%{l_prefix}/var/janus/log \
        $RPM_BUILD_ROOT%{l_prefix}/var/janus/run

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

%files -f files

%clean

%post
    if [ $1 -eq 1 ]; then
        #   on initial installation, generate TLS certificate/key pair
        echo "Generating TLS Certificates/Keys" | %{l_rpmtool} msg -b -t notice
        $RPM_INSTALL_PREFIX/bin/janus-tls localhost 127.0.0.1
        ( echo "Janus was configured with a standard TLS certificate/key pair."
          echo "for \"localhost\" and \"127.0.0.1\". For production use, you usually let"
          echo "Janus listen on an external IP address. For this the TLS"
          echo "certificate/key pair has to be regenerated with for instance:"
          echo "    \$ $RPM_INSTALL_PREFIX/bin/janus-tls www.example.com 192.168.0.1"
        ) | %{l_rpmtool} msg -b -t notice
    elif [ $1 -eq 2 ]; then
        #   after upgrade, restart service
        eval `%{l_rc} janus status 2>/dev/null`
        [ ".$janus_active" = .yes ] && %{l_rc} janus restart
    fi
    exit 0

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

