##
##  opensips.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 options
%define       V_opensips  1.6.4
%define       V_rtpproxy  1.2.1

#   package information
Name:         opensips
Summary:      Open SIP Server
URL:          http://www.opensips.org/
Vendor:       FhG FOKUS et al.
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        PLUS
Group:        VoIP
License:      GPL
Version:      %{V_opensips}
Release:      20101221

#   package options
%option       with_fsl  yes
%option       with_ssl  no

#   list of sources
Source0:      http://www.opensips.org/pub/opensips/%{version}/src/opensips-%{V_opensips}-tls_src.tar.gz
Source1:      http://b2bua.org/chrome/site/rtpproxy-%{V_rtpproxy}.tar.gz
Source2:      rc.opensips
Source3:      fsl.opensips
Source4:      opensips.cfg
Source5:      opensips.schema-dbtext.txt
Patch0:       opensips.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, make, gcc, flex, bison, sed
PreReq:       OpenPKG, openpkg >= 20160101
BuildPreReq:  pcre
PreReq:       pcre
%if "%{with_fsl}" == "yes"
BuildPreReq:  fsl
PreReq:       fsl
%endif
%if "%{with_ssl}" == "yes"
BuildPreReq:  openssl
PreReq:       openssl
%endif

%description
    OpenSIPS is (beside Kamailio) a successor to OpenSER, which in
    turn was spawned from FhG FOKUS' SIP Express Router (SER). It
    provides SIP (RFC3621) registrar, proxy and routing functionality.
    A C shell like scripting language provides full control over the
    server's behaviour. It's modular architecture allows only required
    functionality to be loaded.

%track
    prog opensips = {
        version   = %{V_opensips}
        url       = http://www.opensips.org/pub/opensips/
        regex     = (\d+\.\d+\.\d+)/
        url       = http://www.opensips.org/pub/opensips/__NEWVER__/src/
        regex     = opensips-(__VER__)-tls_src\.tar\.gz
    }
    prog opensips:rtpproxy = {
        version   = %{V_rtpproxy}
        url       = http://www.rtpproxy.org/
        regex     = rtpproxy-(\d+(?:\.\d+)+)\.tar\.gz
    }

%prep
    %setup -q -n opensips-%{version}-tls
    %setup -q -n opensips-%{version}-tls -T -D -a 1
    %patch -p0

%build
    #   build dependencies
    %{l_make} %{l_mflags} \
        CC="%{l_cc} %{l_cflags} %{l_cppflags}" \
%if "%{with_ssl}" == "yes"
        TLS=1 \
%endif
        prefix=%{l_prefix} \
        dep >/dev/null 2>&1 || true

    #   build program
    %{l_make} %{l_mflags} \
        CC="%{l_cc} %{l_cflags} %{l_cppflags}" \
%if "%{with_ssl}" == "yes"
        TLS=1 \
%endif
        prefix=%{l_prefix}

    #   build modules
    %{l_make} %{l_mflags} \
        CC="%{l_cc} %{l_cflags} %{l_cppflags}" \
%if "%{with_ssl}" == "yes"
        TLS=1 \
%endif
        prefix=%{l_prefix} \
        modules \
        skip_modules="mysql jabber"

    #   build rtpproxy extension
    ( cd rtpproxy-%{V_rtpproxy}
      export CC="%{l_cc}"
      export CFLAGS="%{l_cflags -O}"
      export LIBS=""
      case "%{l_platform -t}" in
          *-sunos* ) LIBS="$LIBS -lsocket -lnsl -lrt" ;;
      esac
      ./configure
      %{l_make} %{l_mflags}
    ) || exit $?

%install

    #   install program
    %{l_make} %{l_mflags} \
        INSTALL="%{l_shtool} install%{l_nil} -c" \
        basedir=$RPM_BUILD_ROOT \
        prefix=%{l_prefix} \
        cfg-prefix=$RPM_BUILD_ROOT \
        cfg-target=%{l_prefix}/etc/opensips/ \
        doc-dir=share/opensips/doc/ \
        man-dir=man/ \
        install

    #   install modules
    %{l_make} %{l_mflags} \
        INSTALL="%{l_shtool} install%{l_nil} -c" \
        basedir=$RPM_BUILD_ROOT \
        prefix=%{l_prefix} \
        cfg-prefix=$RPM_BUILD_ROOT \
        cfg-target=%{l_prefix}/etc/opensips/ \
        doc-dir=share/opensips/doc/ \
        man-dir=man/ \
        install-modules \
        skip_modules="mysql jabber"

    #   install rtpproxy extension
    %{l_shtool} install -c -s -m 755 \
        rtpproxy-%{V_rtpproxy}/rtpproxy $RPM_BUILD_ROOT%{l_prefix}/sbin/opensips_rtpproxy

    #   strip down installation
    strip $RPM_BUILD_ROOT%{l_prefix}/sbin/* >/dev/null 2>&1 || true
    rm -f $RPM_BUILD_ROOT%{l_prefix}/sbin/opensips_mysql.sh

    #   post-adjust installation
    %{l_shtool} subst \
        -e 's;/etc/opensips/opensipsctlrc;%{l_prefix}/etc/opensips/opensipsctlrc;g' \
        $RPM_BUILD_ROOT%{l_prefix}/sbin/opensipsctl
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/opensips
    %{l_shtool} install -c -m 644 \
        -e 's;^# \(PID_FILE\)=.*;\1=%{l_prefix}/var/opensips/opensips.pid;' \
        -e 's;^# \(OSIPS_FIFO\)=.*;\1=%{l_prefix}/var/opensips/opensips.fifo;' \
        scripts/opensipsctlrc \
        $RPM_BUILD_ROOT%{l_prefix}/etc/opensips/opensipsctlrc

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

    #   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.opensips} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/fsl/

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

    #   create database directory and install database schema definitions
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/var/opensips
    %{l_shtool} install -c -m 644 \
        %{SOURCE opensips.schema-dbtext.txt} \
        $RPM_BUILD_ROOT%{l_prefix}/share/opensips/

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

%files -f files

%clean

%post
    #   on install, setup database
    if [ ! -d $RPM_INSTALL_PREFIX/var/opensips/db ]; then
        mkdir $RPM_INSTALL_PREFIX/var/opensips/db
        chown %{l_rusr}:%{l_rgrp} $RPM_INSTALL_PREFIX/var/opensips/db
        chmod 755 $RPM_INSTALL_PREFIX/var/opensips/db
        schema="$RPM_INSTALL_PREFIX/share/opensips/opensips.schema-dbtext.txt"
        for table in `sed -e '/^#.*/d' -e '/^$/d' -e 's;\([^:]*\):.*;\1;' <$schema | sort -u`; do
            grep "^${table}:" $schema | sed -e "s;^${table}: *;;" \
                >$RPM_INSTALL_PREFIX/var/opensips/db/$table
        done
        chown %{l_rusr}:%{l_rgrp} $RPM_INSTALL_PREFIX/var/opensips/db/* 2>/dev/null || true
        chmod 644 $RPM_INSTALL_PREFIX/var/opensips/db/* 2>/dev/null || true
    fi

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

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

