# Contributor: Ɓukasz Jendrysik <scadu@yandex.com>
# Contributor: Michael Mason <ms13sp@gmail.com>
# Contributor: Leonardo Arena <rnalrd@gmail.com>
# Maintainer: Nathan Angelacos <nangel@alpinelinux.org>
setvar pkgname = 'kamailio'

# If building from a git snapshot, specify the gitcommit
# run "abuild snapshot", then abuild as normal
# If building a proper release, leave gitcommit blank
#_gitcommit=e27802533cee
setvar _giturl = ""git://github.com/$pkgname/$pkgname.git""
setvar _gittag = 'HEAD'


setvar pkgver = '5.0.2'
setvar pkgrel = '2'
test -z ${_gitcommit} && setvar _suffix = ""_src"" || setvar _suffix = ""-${_gitcommit}""

setvar pkgdesc = ""Open Source SIP Server""
setvar url = ""http://www.kamailio.org/""
setvar arch = ""all""
setvar pkgusers = ""kamailio""
setvar pkggroups = ""kamailio""
setvar arch = ""all""
setvar license = ""GPL2+""
setvar depends = """"
setvar makedepends = ""bison flex expat-dev postgresql-dev pcre-dev mariadb-dev
	libxml2-dev curl-dev unixodbc-dev confuse-dev ncurses-dev sqlite-dev
	lua-dev openldap-dev libressl-dev net-snmp-dev libuuid libev-dev
	jansson-dev json-c-dev libevent-dev linux-headers libmemcached-dev
	rabbitmq-c-dev hiredis-dev libmaxminddb-dev libunistring-dev""
setvar install = ""$pkgname.pre-install $pkgname.pre-upgrade""

# See Makefile.groups for the list of recommended modules for
# each package.  What follows is a simplified version of Makefile.groups
#
# Alpine specific changes:
#
# _mod_list_debugger is not defined in the orginal
#
# Move avp from _mod_list_extras to _mod_list_basic, so that we can create
# a kamailio-extras package
#
# Remove db_text from _mod_list_db because we create a separate dbtext package
#
# - basic used modules, with no extra dependency (widespread usage)
setvar _mod_list_basic = ""avp async auth benchmark blst cfg_rpc cfgt cfgutils corex counters \
		ctl debugger diversion enum exec gzcompress ipops kex mangler \
		maxfwd mediaproxy mqueue \
		nat_traversal nathelper path pike pv ratelimit rr rtimer \
		rtpproxy sanity sdpops siputils sl smsops statsc statistics \
		textops textopsx tm tmx topoh topos xlog rtpengine stun sipt""

# - extra used modules, with no extra dependency
setvar _mod_list_extras = ""auth_diameter call_control cnxcc dmq domainpolicy log_custom \
		msrp pdb qos sca seas sms sst timer tmrec tsilo uac_redirect \
		xhttp xhttp_rpc xprint nosip dmq_usrloc statsd rtjson tcpops \
		auth_xkeys""

# - common modules depending on database
setvar _mod_list_db = ""acc alias_db auth_db avpops cfg_db db_flatstore \
		db_cluster dialog dispatcher domain drouting group \
		htable imc matrix mohqueue msilo mtree p_usrloc pdt permissions \
		pipelimit prefix_route registrar sipcapture siptrace speeddial \
		sqlops uac uri_db userblacklist usrloc""

# - common modules depending on database, using UID db schema
setvar _mod_list_dbuid = ""db2_ops uid_auth_db uid_avp_db uid_domain uid_gflags \
		uid_uri_db""

# - modules for devel purposes
setvar _mod_list_devel = ""malloc_test print print_lib""

# - modules depending on pcre3 library
setvar _mod_list_pcre = ""dialplan lcr regex""

# - modules depending on radius client library
setvar _mod_list_radius = ""acc_radius auth_radius misc_radius peering""

# - modules depending on ldap client library
setvar _mod_list_ldap = ""db2_ldap h350 ldap""

# - modules depending on mysql client library
setvar _mod_list_mysql = ""db_mysql""

# - modules depending on postgres client library
setvar _mod_list_postgres = ""db_postgres""

# - modules depending on unixodbc library
setvar _mod_list_unixodbc = ""db_unixodbc""

# - modules depending on mysql cassandra library
setvar _mod_list_cassandra = ""db_cassandra ndb_cassandra""

# - modules depending on xml2 library
setvar _mod_list_cpl = ""cplc""

# - modules depending on xml2 library
setvar _mod_list_xml = ""xhttp_pi xmlrpc xmlops""

# - modules depending on net-snmp library
setvar _mod_list_snmpstats = ""snmpstats""

# - modules depending on expat library
setvar _mod_list_xmpp = ""xmpp""

# - modules depending on confuse library
setvar _mod_list_carrierroute = ""carrierroute""

# - modules depending on bdb (db4) library
setvar _mod_list_berkeley = ""db_berkeley""

# - modules depending on curl library
setvar _mod_list_utils = ""utils http_client""

# - async http_async_client
setvar _mod_list_http_async = ""http_async_client""

# - modules depending on purple library
setvar _mod_list_purple = ""purple""

# - modules depending on memcache library
setvar _mod_list_memcached = ""memcached""

# - modules depending on openssl library
setvar _mod_list_tls = ""auth_identity crypto tls""

# - modules depending on openssl library
setvar _mod_list_outbound = ""outbound""

# - modules depending on unistring library
setvar _mod_list_websocket = ""websocket""

# - modules depending on libval-threads libcrypto libsres libpthread
setvar _mod_list_dnssec = ""dnssec""

# - modules depending on libsctp
setvar _mod_list_sctp = ""sctp""

# - modules depending on openssl library
setvar _mod_list_authephemeral = ""auth_ephemeral""

# - modules related to SIMPLE presence extensions
setvar _mod_list_presence = ""presence presence_conference presence_dialoginfo \
			presence_mwi presence_profile presence_reginfo \
			presence_xml \
			pua pua_bla pua_dialoginfo pua_reginfo \
			pua_rpc pua_usrloc pua_xmpp \
			rls xcap_client xcap_server""

# - modules depending on lua library
setvar _mod_list_lua = ""app_lua""

# - modules depending on perl library
setvar _mod_list_perl = ""app_perl db_perlvdb""

# - modules depending on python library
setvar _mod_list_python = ""app_python""

# - modules depending on rabbitmq
setvar _mod_list_rabbitmq = ""rabbitmq""

# - modules depending on geoip2 library
setvar _mod_list_geoip2 = ""geoip2""

# - modules depending on sqlite library
setvar _mod_list_sqlite = ""db_sqlite""

# - modules depending on oracle library
setvar _mod_list_oracle = ""db_oracle""

# - modules depending on json library
setvar _mod_list_json = ""json jsonrpcc jsonrpcs""

# - modules depending on redis library
setvar _mod_list_redis = ""ndb_redis""

# - modules depending on mono library
setvar _mod_list_mono = ""app_mono""

# - modules related to IMS extensions
setvar _mod_list_ims = ""cdp cdp_avp ims_dialog ims_auth ims_isc ims_icscf ims_qos \
		ims_registrar_pcscf ims_registrar_scscf ims_usrloc_pcscf \
		ims_usrloc_scscf ims_charging""

# - modules depending on osp toolkit library
setvar _mod_list_osp = ""osp""

# - modules depending on java library
setvar _mod_list_java = ""app_java""

# - modules depending on iptables library
setvar _mod_list_iptrtpproxy = ""iptrtpproxy""

# - modules depending on gzip library
setvar _mod_list_gzcompress = ""gzcompress""

# - modules depending on uuid library
setvar _mod_list_uuid = ""uuid""

# - modules depending on ev library
setvar _mod_list_ev = ""evapi""

# - modules depending on kazoo/rabbitmq
setvar _mod_list_kazoo = ""kazoo""

# - modules depending on mongodb
setvar _mod_list_mongodb = ""db_mongodb ndb_mongodb""

# - modules depending on jansson library
setvar _mod_list_jansson = ""jansson janssonrpcc""

# - modules depending on libm
setvar _mod_list_jsdt = ""app_jsdt""


# Alpine Specific
setvar _mod_list_debugger = ""$_mod_list_devel benchmark debugger""

# Standard modules in main pkg
setvar _module_group_kstandard = ""$_mod_list_basic $_mod_list_dbuid $_mod_list_pcre""

# END Makefile.groups #############################################################

setvar subpackages = ""$pkgname-doc $pkgname-dbg""
setvar _modules = "$_module_group_kstandard"
for _i in db postgres sqlite dbtext mysql \
	cpl xml unixodbc snmpstats xmpp carrierroute \
	ldap utils tls presence lua ims outbound debugger \
	extras json websocket authephemeral \
	uuid ev memcached redis geoip2 jansson \
	jsdt http_async kazoo {

   setvar subpackages = ""$subpackages $pkgname-$_i""
   eval "_modules=\"\$_modules \$_mod_list_$_i\""
}

setvar source = ""http://www.kamailio.org/pub/kamailio/$pkgver/src/${pkgname}-${pkgver}${_suffix}.tar.gz
	0001-kamdbctl.base.patch
	0002-remove-spurious-execinfo.patch
	0003-src_modules_tls_tls_init_c.patch
	0004-src_core_tcp_read_c.patch
	kamailio.cfg
	kamailio.initd
	""

proc snapshot {
        mkdir -p $srcdir
        cd ${SRCDEST:-$srcdir}
        if ! test -d $pkgname.git {
                git clone --bare  $_giturl || return 1
                cd $pkgname.git
        } else {
                cd $pkgname.git
                git fetch || return 1
        }
        git archive --prefix=$pkgname-$pkgver/ -o "$SRCDEST"/${pkgname}-${pkgver}${_suffix}.tar.gz $_gittag
}



setvar _builddir = ""$srcdir"/$pkgname-$pkgver"
proc prepare {
	cd $_builddir
	default_prepare

	cd utils/kamctl/
	sed -i -e 's:/var/run/kamailio.pid:/var/run/kamailio/kamailio.pid:g' \
		kamctl.base kamctlrc || return 1
	# we actually dont need bash
	sed -i -e 's:/bin/bash:/bin/sh:' kamctl kamdbctl \
		|| return 1
}

proc build {
	cd $_builddir
	make FLAVOUR=kamailio STUN=1 \
		PREFIX=/usr \
		CC_EXTRA_OPTS="$CFLAGS -D_GNU_SOURCE" \
		cfg_target=/etc/kamailio/ \
		run_prefix="" \
		include_modules="$_modules" \
		LIBDIR=lib \
		DESTDIR="$pkgdir" \
		cfg_prefix="$pkgdir" \
		cfg
	make EMBEDDED_UTF8_DECODE=1 STUN=1 \
		all || return 1
}

proc package {
	cd $_builddir
	make -j1 install || return 1

	# move default config to -doc package and use our own default config

	mv "$pkgdir"/etc/kamailio/kamailio.cfg \
		"$pkgdir"/usr/share/doc/kamailio/
	install -m644 -D "$srcdir"/kamailio.cfg \
		"$pkgdir"/etc/kamailio/kamailio.cfg

	install -m755 -D "$srcdir"/$pkgname.initd "$pkgdir"/etc/init.d/$pkgname
	install -d -o kamailio "$pkgdir"/var/run/kamailio
}

proc _mv_mod {
	local moddir=usr/lib/kamailio/modules i=
	mkdir -p "$subpkgdir"/$moddir
	for i in $@ {
		mv "$pkgdir"/$moddir/$i.so "$subpkgdir"/$moddir/ || return 1
	}
}

proc _generic_pkg {
	setvar pkgdesc = ""Kamailio $1""
	setvar depends = "$pkgname"
	_mv_mod $2
}


proc _db_driver {
	setvar pkgdesc = ""Database drivers for $1""
	setvar depends = ""$pkgname $pkgname-db""
	_mv_mod db_$2 || return 1
	mkdir -p "$subpkgdir"/usr/share/kamailio \
		"$subpkgdir"/usr/lib/kamailio/kamctl
	mv "$pkgdir"/usr/share/kamailio/$3 \
		"$subpkgdir"/usr/share/kamailio/ || return 1
	mv "$pkgdir"/usr/lib/kamailio/kamctl/*.$4 \
		"$subpkgdir"/usr/lib/kamailio/kamctl/ || return 1
}


proc db {
	_generic_pkg "modules using a database backend" $_mod_list_db
}

proc postgres {
	_db_driver Postgres postgres postgres pgsql
}

proc mysql {
	_db_driver Mysql mysql mysql mysql
}

proc sqlite {
	_db_driver SQlite sqlite db_sqlite sqlite
}

proc dbtext {
	_db_driver DBText text dbtext dbtext || return 1
	# kamdbctl.dbtext is a python script - but we will not make python
	# a dependency because kamdbctl is not normaly used
	setvar depends = "$depends"
	mkdir -p "$subpkgdir"/usr/lib/kamailio/kamctl || return 1
	mv "$pkgdir"/usr/lib/kamailio/kamctl/dbtextdb \
		"$subpkgdir"/usr/lib/kamailio/kamctl/ || return 1
}

proc cpl {
	_generic_pkg "CPL (Call Processing Language) interpreter" \
		$_mod_list_cpl
}

proc xml {
	_generic_pkg "XML related modules: $_mod_list_xml" \
		$_mod_list_xml || return 1
	mkdir -p "$subpkgdir"/usr/share/kamailio
	mv "$pkgdir"/usr/share/kamailio/xhttp_pi \
		"$subpkgdir"/usr/share/kamailio || return 1
	mkdir -p "$subpkgdir"/etc/kamailio/
	mv "$pkgdir"/etc/kamailio/pi_framework.xml \
		"$subpkgdir"/etc/kamailio || return 1
}

proc unixodbc {
	_generic_pkg "Database drivers for unixodbc" \
		$_mod_list_unixodbc
}

proc snmpstats {
	_generic_pkg "SNMP statistics support" \
		$_mod_list_snmpstats || return 1
	mkdir -p "$subpkgdir"/usr/share/snmp
	mv "$_builddir"/src/modules/snmpstats/mibs \
		"$subpkgdir"/usr/share/snmp/ || return 1
}

proc xmpp {
	_generic_pkg "XMPP (Jabber) gateway" \
		$_mod_list_xmpp
}

proc carrierroute {
	_generic_pkg "carrier grade routing functions" \
		$_mod_list_carrierroute
}

proc ldap {
	_generic_pkg "LDAP search functions" \
		$_mod_list_ldap
}

proc utils {
	_generic_pkg "miscelaneous utility functions" \
		$_mod_list_utils
}

proc tls {
	_generic_pkg "TLS support" \
		$_mod_list_tls
}

proc presence {
	_generic_pkg "SIP Notify (Presence) support" \
		$_mod_list_presence
}

proc lua {
	_generic_pkg "Lua script support" \
		$_mod_list_lua
}

proc ims {
	_generic_pkg "IP Multimedia Subsystem (IMS) support" \
		$_mod_list_ims
}

proc outbound {
	_generic_pkg "RFC 5626 section 5 Outbound support" \
		$_mod_list_outbound
}

proc debugger {
	_generic_pkg "debugging modules" \
		$_mod_list_debugger
}

proc extras {
	_generic_pkg "extra modules" \
		$_mod_list_extras
}

proc websocket {
	_generic_pkg "websocket support" \
		$_mod_list_websocket
}

proc authephemeral {
	_generic_pkg "auth ephemeral (webrtc)" \
		$_mod_list_authephemeral
}

proc json {
	_generic_pkg "Operations on json strings" \
		$_mod_list_json
}

proc jansson {
	_generic_pkg "Operations on json strings using jansson library" \
		$_mod_list_jansson
}

proc geoip2 {
	_generic_pkg "MaxMind GeoIP lookup support" \
		$_mod_list_geoip2
}

proc ev {
	_generic_pkg "Network event broadcast API" \
		$_mod_list_ev
}

proc uuid {
	_generic_pkg "UUID generator using libuuid" \
		$_mod_list_uuid
}

proc memcached {
	_generic_pkg "memcached support" \
		$_mod_list_memcached
}

proc rabbitmq {
	_generic_pkg "RabbitMQ client for configuration file" \
		$_mod_list_rabbitmq
}

proc jsdt {
	_generic_pkg "Support for executing JavaScript" \
		$_mod_list_jsdt
}


proc redis {
	_generic_pkg "Redis NoSQL support" \
		$_mod_list_redis
}

proc http_async {
	_generic_pkg "Asynchronous HTTP queries support" \
		$_mod_list_http_async
}

proc kazoo {
	_generic_pkg "Kazoo VoIP platform support" \
		$_mod_list_kazoo
}


setvar sha512sums = ""d2bd320edb9612006277979f96079c8a01b8fef3774af5847aab78529dbe61880be6a4e596af1ce6c67fa34142a0f200e7728ff3a725691f081fccd51cd11f16  kamailio-5.0.2_src.tar.gz
6badfb611c02ffcb4c2e9937731162ea1a4b737f042ed52120e2f96ebb80b5b7d240b5612c9ca565e693eec9b8c52c1ee5db04dfc47d204501021f984b4b11db  0001-kamdbctl.base.patch
e60e8d41e55008f0473e4d645af3b43f2bc097e341bb3bb81daec8bc47abcd6d037cbb56082675c24ffb1c50b2eb93e3c4fc50b0dc2df9cdfe4f28ad7b9fbf9e  0002-remove-spurious-execinfo.patch
84b8645e551a41737863085dad6c2c865e60bbe505bfdf750dec530cdd0501025a69e60a48b54971f0d0daaf5f52d1cc0aa8d275395f5c110a3563bb269bb6df  0003-src_modules_tls_tls_init_c.patch
af8362201957eae6b66baf7368c9ca884024209a396d77c5c52180c9aabe13772e9c6513e59721d39503e5bb7a8c1030f5c10301ea9055bddafb7f01ee2f3076  0004-src_core_tcp_read_c.patch
c1abf69b48847dc8c7ab0d11ef9adb531aa4635f9d44db6933981edc5a47df374664fb24867b19aa64abbcc9777bf1cd0360d9aea54e27b081065928c61e0f0b  kamailio.cfg
cd6e3b677d803cd78561ad14d9b2589fd35ad0096f48047fdcb4ddc7d9103871357efba3b350946844cb53dbb081210746421fc420c22ac845b90251168a628e  kamailio.initd""