# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
# Contributor: Michael Mason <ms13sp@gmail.com>
# Contributor: Cameron Banta <cbanta@gmail.com>
pkgname=freeswitch
pkgver=1.8.7
pkgrel=1
pkgdesc="A communications platform written in C from the ground up"
url="http://www.freeswitch.org"
arch="all"
license="MPL-1.1"
makedepends="
	bash
	bsd-compat-headers
	coreutils
	curl-dev
	db-dev
	flac-dev
	flite-dev
	gdbm-dev
	gnutls-dev
	ilbc-dev
	lame-dev
	ldns-dev
	libedit-dev
	libexecinfo-dev
	libjpeg-turbo-dev
	libogg-dev
	libpri-dev
	openssl-dev
	libshout-dev
	libsndfile-dev
	libvorbis-dev
	linux-headers
	lua5.3-dev
	mpg123-dev
	ncurses-dev
	net-snmp-dev
	opus-dev
	pcre-dev
	perl-dev
	portaudio-dev
	postgresql-dev
	sngtc_client-dev
	speex-dev
	speexdsp-dev
	sqlite-dev
	tiff-dev
	unixodbc-dev
	util-linux-dev
	xmlrpc-c-dev
	yasm
	zlib-dev
	autoconf automake libtool
	"
install="$pkgname.pre-install $pkgname.pre-upgrade"
FREESWITCH_USER=freeswitch
FREESWITCH_GROUP=freeswitch
pkgusers="$FREESWITCH_USER"
pkggroups="$FREESWITCH_GROUP"
subpackages="$pkgname-static $pkgname-dev $pkgname-flite $pkgname-timezones::noarch
	$pkgname-sample-config:conf:noarch $pkgname-freetdm $pkgname-sangoma
	$pkgname-snmp $pkgname-pgsql $pkgname-perl $pkgname-perlesl
	$pkgname-openrc $pkgname-dbg"

source="https://files.freeswitch.org/freeswitch-releases/$pkgname-$pkgver.tar.xz
	0001-sofia-sip-byte-order.patch
	0002-FS-verto-bswap_64.patch
	configure-lua53.patch
	include-lua53.patch
	getlib.patch
	libvpx-fix-arm-float-abi.patch
	disable-Werror.patch
	modules.conf
	freeswitch.confd
	freeswitch.initd
	"

builddir="$srcdir/$pkgname-$pkgver"

prepare() {
	default_prepare
	autoreconf -vif
	update_config_sub
}

build() {
	cd "$builddir"
	cp -f "$srcdir/modules.conf" modules.conf
	CFLAGS="-Wno-unused-but-set-variable" ./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--enable-fhs \
		--localstatedir=/var \
		--sysconfdir=/etc \
		--with-scriptdir=/etc/freeswitch/scripts \
		--mandir=/usr/share/man \
		--infodir=/usr/share/info \
		--with-devrandom=/dev/urandom \
		--with-libpri \
		--disable-debug \
		--enable-core-pgsql-support \
		--enable-system-lua \
		--enable-system-xmlrpc-c

	# build freetdm with -lexecinfo
	make -C libs/freetdm LIBS="-lexecinfo"
	# first build libfreeswitch (in parallel)
	make src/include/switch_version.h src/include/switch_swigable_cpp.h
	make libfreeswitch.la
	# finally we build the rest
	make -j1 all

	# build perlesl module
	cd "$builddir"/libs/esl
	make -j1 perlmod

}
package(){
	cd "$builddir"
	make -j1 DESTDIR="$pkgdir" install samples-conf samples-htdocs

	install -m755 -D "$srcdir"/$pkgname.initd \
		"$pkgdir"/etc/init.d/$pkgname
	install -m644 -D "$srcdir"/$pkgname.confd \
		"$pkgdir"/etc/conf.d/$pkgname
	chown -R $FREESWITCH_USER:$FREESWITCH_GROUP "$pkgdir"/var/*/freeswitch

	# install perlesl module
	cd "$builddir"/libs/esl
	make -j1 DESTDIR="$pkgdir" perlmod-install
}


_mv_mod() {
	local moddir=usr/lib/freeswitch/mod i=
	mkdir -p "$subpkgdir"/$moddir
	for i in $@; do
		mv "$pkgdir"/$moddir/$i.so "$subpkgdir"/$moddir/
	done
}


flite() {
        pkgdesc="Freeswitch Text To Speech Module"
        install=
        _mv_mod mod_flite
##
## The mod_say_xx modules can be used with out flite (for numbers, etc using
## sound files). So they shouldn't be in the flite package -cB
##
#	_mv_mod mod_say_de mod_say_en mod_say_es mod_say_fr \
#		mod_say_it mod_say_nl mod_say_zh mod_say_hu mod_say_ru \
#		mod_say_th mod_say_he
}

freetdm() {
	pkgdesc="Freeswitch FreeTDM Module"
	install=
	_mv_mod mod_freetdm ftmod_analog ftmod_analog_em ftmod_libpri \
		ftmod_skel ftmod_zt
	mv "$pkgdir"/usr/lib/libfreetdm.so* "$subpkgdir"/usr/lib/
}

sangoma() {
	pkgdesc="Freeswitch Sangoma Media Transcode Codec Module"
	install=
	_mv_mod mod_sangoma_codec
}

timezones() {
	pkgdesc="Freeswitch timezone configuration"
	install=
	replaces="freeswitch-sample-config"
	mkdir -p "$subpkgdir"/etc/freeswitch/autoload_configs
	mv "$pkgdir"/etc/freeswitch/autoload_configs/timezones.conf.xml \
		"$subpkgdir"/etc/freeswitch/autoload_configs
}

snmp() {
	pkgdesc="Freeswitch SNMP module"
	install=
	_mv_mod mod_snmp
}

pgsql() {
	pkgdesc="Freeswitch PostgreSQL Module"
	install=
	_mv_mod mod_cdr_pg_csv
}

perl() {
	pkgdesc="Freeswitch Perl module"
	install=
	_mv_mod mod_perl
}

perlesl() {
	pkgdesc="Freeswitch Perl ESL module"
	install=
	mkdir -p "$subpkgdir"/usr/lib/perl5
	mv "$pkgdir"/usr/lib/perl5/* "$subpkgdir"/usr/lib/perl5
}

conf() {
	pkgdesc="Freeswitch sample configureation"
	depends="freeswitch-timezones"
	install=
	mkdir -p "$subpkgdir"/etc/freeswitch
	# move all configs except freeswitch.xml
	for i in "$pkgdir"/etc/freeswitch/*; do
		[ "$i" = "$pkgdir"/etc/freeswitch/freeswitch.xml ] && continue
		mv "$i" "$subpkgdir"/etc/freeswitch/
	done
	mkdir -p "$pkgdir"/etc/freeswitch/scripts
}

sha512sums="d395368989763ef35fe4f700692febfc18e548a2a7ed13ffde87ed5330b4f5b39ef322b4aaed69c0c84192b42b2fe11c7f778826902851d139cec1f702a98b56  freeswitch-1.8.7.tar.xz
8a7ca31cc80524b02edc83af891a32af64dd7834ac14b1389112f2ce7fe06fe602d24509a299898f25e807dd0b88544aecb990bf4bd37ee1c7023ae58dacd28a  0001-sofia-sip-byte-order.patch
5f93150e1acd632df98bc3bed5613fb1e45180ae4096dcfee5c060da213c8355339260eaf5758cd77c785f6d84cf0661650a872ec574b586ab19803d4f6955f8  0002-FS-verto-bswap_64.patch
222e3510790de29e29428c70325eb1d4e7852eb4315ee546f137293868bd71acf89e7c85c5ca9d0eb708090c9eef91878a8e12c1b9d5cc99a68fecbd42ad962f  configure-lua53.patch
c1960f3c7acf729468b819f7f15d66a251c3967b2784bce18c7755b333156e687c55fdc1aceda588ceb1bf9638e2fd4a31a169b00b15c5040cf1001c3c73913b  include-lua53.patch
4ceb48f64d2bc26a02cc0846276506241bfd30c156422b0a1d608fd172c099feb5c121a763652e9a45046dcdd0ba0eb71eab240e0c6ce2ad63ff781719e135a4  getlib.patch
98a53f289c3647cafd05b6f847da0504a838a6e06545c133c10fb058c2d941e6bccf2820f44649fc25f94a4a5b7c1280eaee91098e8110f96ed3342c1cb4346c  libvpx-fix-arm-float-abi.patch
16e9208fca7f0a1985dd01ec38ddabb6a79da83ad6ab004bb66de528ac45f02fadaddc50a9f228e1aa38fae213fa5af0ca858b7af629b67a7d654339f813a68c  disable-Werror.patch
6deecb303e765d44bdd970d5a7079c9dd9ce17d12056b68cd978a182a86b477b4a3c19d2f5a18f36199ca2e0594a3b84de521ab821e2f8c89f7449a3d7f21279  modules.conf
a585f6411185a26206137a1ad97a06fd6c73e80c5439e9be45eabfa70e7a83120169ba882971fcd328436c8e0242cbd664170b80754ea2846021689baf1f1595  freeswitch.confd
643d0a2e43f5d3bf3b99fcb6f6422302cb4b74a95eccf844eafb100b15aa9856b4ff41f112d6637255c2e9e2bec9fedc9a9215dfff214dfb83b52eae16b71dca  freeswitch.initd"
