##
##  asterisk.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_opkg                     1.6.2.13
%define       V_asterisk                 1.6.2.13
%define       V_asterisk_addons          1.6.2.2
%define       V_asterisk_sounds_core_en  1.4.20
%define       V_asterisk_sounds_core_de  current
%define       V_asterisk_sounds_extra_en 1.4.11
%define       V_chan_capi                1.1.5

#   package information
Name:         asterisk
Summary:      Private Branch Exchange (PBX)
URL:          http://www.asterisk.org/
Vendor:       Mark Spencer et al.
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        BASE
Group:        VoIP
License:      GPL
Version:      %{V_opkg}
Release:      20101019

#   package options
%option       with_dahdi   no
%option       with_capi    no
%option       with_ogg     no
%option       with_mp3     no
%option       with_fax     no
%option       with_avcodec no
%option       with_ldap    no
%option       with_radius  no
%option       with_sqlite  no
%option       with_lua     no
%option       with_jabber  no
%option       with_imap    no
%option       with_odbc    no

#   list of sources
Source0:      http://downloads.asterisk.org/pub/telephony/asterisk/releases/asterisk-%{V_asterisk}.tar.gz
Source1:      http://downloads.asterisk.org/pub/telephony/asterisk/releases/asterisk-addons-%{V_asterisk_addons}.tar.gz
Source2:      http://downloads.asterisk.org/pub/telephony/sounds/releases/asterisk-core-sounds-en-alaw-%{V_asterisk_sounds_core_en}.tar.gz
Source3:      http://downloads.asterisk.org/pub/telephony/sounds/releases/asterisk-extra-sounds-en-alaw-%{V_asterisk_sounds_extra_en}.tar.gz
Source4:      http://www.amooma.de/asterisk/sprachbausteine/asterisk-core-sounds-de-gsm-%{V_asterisk_sounds_core_de}.tar.gz
Source5:      ftp://ftp.chan-capi.org/chan-capi/chan_capi-%{V_chan_capi}.tar.gz
Source6:      asterisk.txt
Source7:      rc.asterisk
Patch0:       asterisk.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, bison, gcc, make, grep
PreReq:       OpenPKG, openpkg >= 20160101, MTA
BuildPreReq:  zlib, curl, openssl, ncurses, speex, popt, gmime22
PreReq:       zlib, curl, openssl, ncurses, speex, popt, gmime22
%if "%{with_ogg}" == "yes"
BuildPreReq:  vorbis-libs
PreReq:       vorbis-libs
%endif
%if "%{with_mp3}" == "yes"
PreReq:       mpg123
%endif
%if "%{with_imap}" == "yes"
BuildPreReq:  imap
PreReq:       imap
%endif
%if "%{with_sqlite}" == "yes"
BuildPreReq:  sqlite
PreReq:       sqlite
%endif
%if "%{with_lua}" == "yes"
BuildPreReq:  lua
PreReq:       lua
%endif
%if "%{with_fax}" == "yes"
BuildPreReq:  spandsp, tiff, jpeg, zlib
PreReq:       spandsp, tiff, jpeg, zlib
%endif
%if "%{with_avcodec}" == "yes"
BuildPreReq:  ffmpeg
PreReq:       ffmpeg
%endif
%if "%{with_ldap}" == "yes"
BuildPreReq:  openldap
PreReq:       openldap
%endif
%if "%{with_radius}" == "yes"
BuildPreReq:  radiusclient
PreReq:       radiusclient
%endif
%if "%{with_jabber}" == "yes"
BuildPreReq:  iksemel
PreReq:       iksemel
%endif
%if "%{with_odbc}" == "yes"
BuildPreReq:  unixodbc, sqlite, sqlite::with_odbc = yes
PreReq:       unixodbc, sqlite, sqlite::with_odbc = yes
%endif

%description
    Asterisk is a complete Private Branch Exchange (PBX) in software.
    It provides all of the features you would expect from a PBX and
    more. Asterisk does Voice over IP (VoIP) in many protocols. Asterisk
    provides Voicemail services with Directory, Call Conferencing,
    Interactive Voice Response and Call Queuing. It has support for
    three-way calling, caller ID services, ADSI, SIP, etc. Asterisk
    needs no additional hardware for VoIP.

%track
    prog asterisk = {
        version   = %{V_asterisk}
        url       = http://downloads.asterisk.org/pub/telephony/asterisk/releases/
        regex     = asterisk-(\d+\.\d+(\.\d+)+)\.tar\.gz
    }
    prog asterisk:addons = {
        version   = %{V_asterisk_addons}
        url       = http://downloads.asterisk.org/pub/telephony/asterisk/releases/
        regex     = asterisk-addons-(\d+\.\d+(\.\d+)+)\.tar\.gz
    }
    prog asterisk:sounds_core_en = {
        version   = %{V_asterisk_sounds_core_en}
        url       = http://downloads.asterisk.org/pub/telephony/sounds/releases/
        regex     = asterisk-core-sounds-en-alaw-(\d+\.\d+(\.\d+)+)\.tar\.gz
    }
    prog asterisk:sounds_extra_en = {
        version   = %{V_asterisk_sounds_extra_en}
        url       = http://downloads.asterisk.org/pub/telephony/sounds/releases/
        regex     = asterisk-extra-sounds-en-alaw-(\d+\.\d+(\.\d+)+)\.tar\.gz
    }
    prog asterisk:sounds_core_de = {
        version   = %{V_asterisk_sounds_core_de}
        url       = http://www.amooma.de/asterisk/sprachbausteine/
        regex     = asterisk-core-sounds-de-gsm-(__VER__)\.tar\.gz
    }
    prog asterisk:chan_capi = {
        version   = %{V_chan_capi}
        url       = ftp://ftp.chan-capi.org/chan-capi/
        regex     = chan_capi-(__VER__)\.tar\.gz
    }

%prep
    %setup -q -n asterisk-%{V_asterisk}
    %setup -q -n asterisk-%{V_asterisk} -D -T -a 1
    %setup -q -n asterisk-%{V_asterisk} -D -T -a 5
    %patch -p0
    exit 0
    cp %{SOURCE2} %{SOURCE3} sounds/
    %{l_shtool} subst \
        -e 's;\(CORE_SOUNDS_VERSION\):=.*$;\1:=%{V_asterisk_sounds_core_en};' \
        -e 's;\(EXTRA_SOUNDS_VERSION\):=.*$;\1:=%{V_asterisk_sounds_extra_en};' \
        sounds/Makefile
    %{l_shtool} subst \
        -e 's;-lradiusclient-ng;-lradiusclient;g' \
        -e 's;radiusclient-ng\.h;radiusclient.h;g' \
        configure cdr/cdr_radius.c

%build
    #   sanity check
%if "%{with_dahdi}" == "yes"
    dahdi_prefix=""
    case "%{l_platform -t}" in
        *-linux* )
            if [ ! -f /usr/include/linux/dahdi.h ]; then
                echo "option \"with_dahdi\" requires DAHDI Linux drivers installed" 1>&2; exit 1
            fi
            dahdi_prefix=/usr
            ;;
        *-freebsd* )
            if [ ! -f /usr/local/include/dahdi/dahdi.h ]; then
                echo "option \"with_dahdi\" requires DAHDI FreeBSD drivers installed" 1>&2; exit 1
            fi
            dahdi_prefix=/usr/local
            ;;
        * ) echo "option \"with_dahdi\" supported under Linux and FreeBSD only" 1>&2; exit 1 ;;
    esac
%endif

    #   re-configure music-on-hold to find mpg123
%if "%{with_mp3}" == "yes"
    mpg123="%{l_prefix}/bin/mpg123"
%else
    mpg123="`which false`"
%endif
    %{l_shtool} subst \
        -e "s;/usr/local/bin/mpg123;$mpg123;" \
        -e "s;/usr/bin/mpg123;$mpg123;" \
        res/res_musiconhold.c apps/app_mp3.c

    #   build program
    %{l_shtool} subst \
        -e 's;-llua5\.1;-llua;' \
        -e 's;lua5\.1/lua\.h;lua/lua.h;' \
        -e 's;ffmpeg/avcodec\.h;libavcodec/avcodec.h;' \
        configure
    ( echo "ac_cv_prog_CONFIG_GMIME=%{l_prefix}/bin/gmime-config"
    ) >config.cache
    cflags="%{l_cflags -O}"
    cppflags="%{l_cppflags ncurses lua .}"
    ldflags="%{l_ldflags}"
    libs=""
    cppflags="$cppflags `pkg-config gmime-2.0 --cflags-only-I`"
    ldflags="$ldflags `pkg-config gmime-2.0 --libs-only-L`"
    libs="$libs `pkg-config gmime-2.0 --libs-only-l`"
%if "%{with_ogg}" == "yes"
    libs="$libs -logg -lm"
%endif
%if "%{with_avcodec}" == "yes"
    libs="$libs -lavcodec -lavutil"
%endif
%if "%{with_ldap}" == "yes"
    libs="$libs -lldap -llber -lssl -lcrypto"
%endif
%if "%{with_dahdi}" == "yes"
    cppflags="$cppflags -I$dahdi_prefix/include"
    ldflags="$ldflags -L$dahdi_prefix/lib"
%endif
    CC="%{l_cc}" \
    CFLAGS="$cflags" \
    CPPFLAGS="$cppflags" \
    LDFLAGS="$ldflags" \
    LIBS="$libs" \
    ./configure \
        --cache-file=./config.cache \
        --prefix=%{l_prefix} \
        --mandir=%{l_prefix}/man \
        --sysconfdir=%{l_prefix}/etc \
        --localstatedir=%{l_prefix}/var/asterisk \
        --with-curl=%{l_prefix} \
        --with-ncurses \
        --with-gsm="internal" \
        --with-popt=%{l_prefix} \
%if "%{with_sqlite}" == "yes"
        --with-sqlite3=%{l_prefix} \
%else
        --without-sqlite3 \
%endif
        --with-speex=%{l_prefix} \
        --with-speexdsp=%{l_prefix} \
        --with-ssl=%{l_prefix} \
        --with-gmime=%{l_prefix} \
        --with-z=%{l_prefix} \
%if "%{with_ogg}" == "yes"
        --with-ogg=%{l_prefix} \
        --with-vorbis=%{l_prefix} \
%else
        --without-ogg \
        --without-vorbis \
%endif
%if "%{with_jabber}" == "yes"
        --with-iksemel=%{l_prefix} \
%else
        --without-iksemel \
%endif
%if "%{with_lua}" == "yes"
        --with-lua=%{l_prefix} \
%else
        --without-lua \
%endif
%if "%{with_dahdi}" == "yes"
        --with-dahdi=$dahdi_prefix \
        --with-tonezone=$dahdi_prefix \
%else
        --without-dahdi \
        --without-tonezone \
%endif
%if "%{with_avcodec}" == "yes"
        --with-avcodec=%{l_prefix} \
%else
        --without-avcodec \
%endif
%if "%{with_ldap}" == "yes"
        --with-ldap=%{l_prefix} \
%else
        --without-ldap \
%endif
%if "%{with_radius}" == "yes"
        --with-radius=%{l_prefix} \
%else
        --without-radius \
%endif
%if "%{with_imap}" == "yes"
        --with-imap=%{l_prefix} \
%else
        --without-imap \
%endif
%if "%{with_odbc}" == "yes"
        --with-odbc=%{l_prefix} \
%else
        --without-odbc \
%endif
        --without-netsnmp \
        --without-isdnnet \
        --without-misdn \
        --without-kde \
        --without-nbs \
        --without-newt \
        --without-asound \
        --without-oss \
        --without-qt \
        --without-sqlite \
        --without-postgres \
        --without-tds \
        --without-osptk \
        --without-pri \
        --without-suppserv \
        --without-vpb \
        --without-sdl \
        --without-x11 \
        --without-gtk \
        --without-gtk2
    %{l_make} %{l_mflags} \
        CC="%{l_cc} $cflags -I. -I./include -I../include -I../../include $cppflags"

    #   build addon modules
    ( cd asterisk-addons-*
      ASTERISK_DIR="`pwd`/.." \
      CC="%{l_cc}" \
      CFLAGS="%{l_cflags -O}" \
      CPPFLAGS="%{l_cppflags ncurses tiff .}" \
      LDFLAGS="%{l_ldflags}" \
%if "%{with_fax}" == "yes"
      LIBS="-ltiff -ljpeg -lz -lm" \
%else
      LIBS="-lm" \
%endif
      ./configure \
          --prefix=%{l_prefix} \
          --mandir=%{l_prefix}/man \
          --sysconfdir=%{l_prefix}/etc \
          --localstatedir=%{l_prefix}/var/asterisk \
          --with-ncurses \
          --with-asterisk \
          --without-mysqlclient \
%if "%{with_fax}" == "yes"
          --with-spandsp=%{l_prefix}
%else
          --without-spandsp
%endif
      %{l_make} %{l_mflags} \
          CC="%{l_cc} %{l_cflags -O} -I. %{l_cppflags ncurses tiff .}"
    ) || exit $?
%if "%{with_capi}" == "yes"
    ( cd chan_capi-*
      ln -s ../include/asterisk .
      ln -s ../include/asterisk.h .
      %{l_make} %{l_mflags} \
          INSTALL_PREFIX=%{l_prefix} \
          ASTERISK_HEADER_DIR="`pwd`"
    ) || exit $?
%endif

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

    #   install addon modules
    ( cd asterisk-addons-*
      %{l_make} %{l_mflags} install \
          DESTDIR=$RPM_BUILD_ROOT \
          INSTALL_PREFIX=%{l_prefix}
    ) || exit $?
%if "%{with_capi}" == "yes"
    ( cd chan_capi-*
      %{l_make} %{l_mflags} install \
          INSTALL_PREFIX=$RPM_BUILD_ROOT%{l_prefix} \
          INSTALL="%{l_shtool} install -c" \
          MODULES_DIR='$(INSTALL_PREFIX)/lib/asterisk/modules'
    ) || exit $?
%endif

    #   install addon sounds
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/share/asterisk/sounds/de
    ( cd $RPM_BUILD_ROOT%{l_prefix}/share/asterisk/sounds/de
      %{l_gzip} -d -c %{SOURCE4} | %{l_tar} xf -
    ) || exit $?

    #   strip down installation
    rm -f $RPM_BUILD_ROOT%{l_prefix}/sbin/autosupport >/dev/null 2>&1 || true
    rm -f $RPM_BUILD_ROOT%{l_prefix}/sbin/safe_asterisk >/dev/null 2>&1 || true
    rm -f $RPM_BUILD_ROOT%{l_prefix}/man/man8/autosupport.8 >/dev/null 2>&1 || true
    rm -f $RPM_BUILD_ROOT%{l_prefix}/man/man8/safe_asterisk.8 >/dev/null 2>&1 || true
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/share/asterisk/firmware >/dev/null 2>&1 || true
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/share/asterisk/images >/dev/null 2>&1 || true
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/share/asterisk/sounds/.asterisk* >/dev/null 2>&1 || true
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/share/asterisk/sounds/en/CREDIT* >/dev/null 2>&1 || true
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/share/asterisk/sounds/en/CHANGES* >/dev/null 2>&1 || true
    rmdir $RPM_BUILD_ROOT%{l_prefix}/share/asterisk/sounds/es >/dev/null 2>&1 || true
    rmdir $RPM_BUILD_ROOT%{l_prefix}/share/asterisk/sounds/fr >/dev/null 2>&1 || true

    #   create additional directories
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/var/asterisk/db

    #   post-adjust installation
    strip $RPM_BUILD_ROOT%{l_prefix}/sbin/* >/dev/null 2>&1 || true
    mv  $RPM_BUILD_ROOT%{l_prefix}/share/asterisk/agi-bin \
        $RPM_BUILD_ROOT%{l_prefix}/lib/asterisk/agi-bin
    for bin in $RPM_BUILD_ROOT%{l_prefix}/lib/asterisk/agi-bin/*.*; do
        mv $bin `echo $bin | sed -e 's;\.[^.]*$;;'`
    done

    #   install default configuration
    for name in `grep "^<file" %{SOURCE asterisk.txt} | sed -e 's;^.*name=";;' -e 's;".*$;;'`; do
%if "%{with_dahdi}" == "no"
        [ ".$name" = ".chan_dahdi.conf" ] && continue
%endif
%if "%{with_capi}" == "no"
        [ ".$name" = ".capi.conf" ] && continue
%endif
%if "%{with_sqlite}" == "no"
        [ ".$name" = ".cdr_sqlite3_custom.conf" ] && continue
%endif
%if "%{with_odbc}" == "no"
        [ ".$name" = ".res_odbc.conf" ] && continue
        [ ".$name" = ".func_odbc.conf" ] && continue
%endif
        (echo ""; cat %{SOURCE asterisk.txt}; echo "") |\
            sed -e "1,/^<file name=\"$name\">/d" -e "/<\/file>/,\$d" >$name
        %{l_shtool} install -c -m 644 %{l_value -s -a} \
            $name $RPM_BUILD_ROOT%{l_prefix}/etc/asterisk/
    done

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

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

%files -f files

%clean

%post
%if "%{with_dahdi}" == "yes"
    if [ $1 -eq 1 ]; then
        case "%{l_platform -t}" in
            *-freebsd* )
                ( echo "To allow Asterisk to use the DAHDI /dev/dahdi/pseudo device(s)"
                  echo "you have add the following entry to /etc/devfs.rules:"
                  echo ""
                  echo "[openpkg_asterisk=10]"
                  echo "add path 'dahdi/pseudo*' mode 0664 user %{l_rusr} group %{l_rgrp}"
                  echo ""
                ) | %{l_rpmtool} msg -b -t notice
                ;;
        esac
    fi
%endif
%if "%{with_odbc}" == "yes"
    #   after install, optionally link into ODBC and create database
    if $RPM_INSTALL_PREFIX/bin/odbcinst -q -s -n "asterisk-sqlite" >/dev/null 2>&1; then
        :
    else
        ( echo "[asterisk-sqlite]"
          echo "Description = Asterisk SQLite Database"
          echo "Driver      = SQLite3"
          echo "Database    = $RPM_INSTALL_PREFIX/var/asterisk/db/asterisk.sqlite"
          echo "Timeout     = 2000"
        ) | $RPM_INSTALL_PREFIX/bin/odbcinst -i -s -l -n "asterisk-sqlite" -r >/dev/null 2>&1 || true
    fi
    if [ ! -f $RPM_INSTALL_PREFIX/var/asterisk/run/asterisk.db ]; then
        ( umask 007
          echo "CREATE TABLE map (key TEXT, val TEXT);" | $RPM_INSTALL_PREFIX/bin/isql "asterisk-sqlite" -b
          chown %{l_rusr}:%{l_rgrp} $RPM_INSTALL_PREFIX/var/asterisk/db/asterisk.sqlite >/dev/null 2>&1 || true
        ) || exit $?
    fi
%endif
    #   after upgrade, restart service
    [ $1 -eq 2 ] || exit 0
    eval `%{l_rc} asterisk status 2>/dev/null`
    [ ".$asterisk_active" = .yes ] && %{l_rc} asterisk restart
    exit 0

%preun
    #   before erase, stop service and remove log files
    [ $1 -eq 0 ] || exit 0
    %{l_rc} asterisk stop 2>/dev/null
    rm -f $RPM_INSTALL_PREFIX/var/asterisk/log/*       >/dev/null 2>&1 || true
    rm -f $RPM_INSTALL_PREFIX/var/asterisk/log/cdr-*/* >/dev/null 2>&1 || true
    rm -f $RPM_INSTALL_PREFIX/var/asterisk/run/*       >/dev/null 2>&1 || true
    rm -f $RPM_INSTALL_PREFIX/var/asterisk/spool/*/*   >/dev/null 2>&1 || true
    rm -f $RPM_INSTALL_PREFIX/var/asterisk/db/*        >/dev/null 2>&1 || true
%if "%{with_odbc}" == "yes"
    #   before erase, optionally unlink from ODBC and destroy database
    $RPM_INSTALL_PREFIX/bin/odbcinst -u -s -l -n "asterisk-sqlite" >/dev/null 2>&1 || true
%endif
    exit 0

