# Contributor: Michael Mason <ms13sp@gmail.com>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=pjproject
pkgver=2.5.5
pkgrel=3
pkgdesc="CLI Softphone"
url="http://www.pjsip.org/pjsua.htm"
arch="all"
license="GPL"
depends=
depends_dev="libressl-dev alsa-lib-dev gsm-dev speex-dev speexdsp-dev
	portaudio-dev libsrtp-dev libsamplerate-dev"
makedepends="$depends_dev"
install=
subpackages="$pkgname-dev pjsua"
source="http://www.pjsip.org/release/$pkgver/$pkgname-$pkgver.tar.bz2
	fix-bb-cp-uniqueness.patch
	pjproject_no_third_party.patch
	"

_builddir="$srcdir/$pkgname-$pkgver"
prepare() {
	cd "$_builddir"
	for i in $source; do
		case $i in
		*.patch) msg $i; patch -p1 -i "$srcdir"/$i || return 1;;
		esac
	done
	update_config_sub || return 1

	# Remove target name from lib names
	sed -i -e 's/-$(TARGET_NAME)//g' \
		-e 's/= $(TARGET_NAME).a/= .a/g' \
		-e 's/-$(LIB_SUFFIX)/$(LIB_SUFFIX)/g' \
		$(find . -name '*.mak*' -o -name Makefile) || return 1

	# Fix hardcoded prefix and flags
	sed -i \
		-e 's/poll@/poll@\nexport PREFIX := @prefix@\n/g' \
		-e 's!prefix = /usr/local!prefix = $(PREFIX)!' \
		-e '/PJLIB_CFLAGS/ s/(_CFLAGS)/(_CFLAGS) -fPIC/g' \
		-e '/PJLIB_UTIL_CFLAGS/ s/(_CFLAGS)/(_CFLAGS) -fPIC/g' \
		Makefile \
		build.mak.in || return 1

	# Use libsamplerate instead of bundled resample
	sed -i -e "s/resample//" third_party/build/Makefile
	sed -i -e "s#../../third_party/libsamplerate/src/samplerate.h#samplerate.h#" pjmedia/src/pjmedia/resample_libsamplerate.c
}

build() {
	cd "$_builddir"
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--sysconfdir=/etc \
		--mandir=/usr/share/man \
		--infodir=/usr/share/info \
		--enable-shared \
		--enable-libsamplerate \
		--with-external-speex \
		--with-external-gsm \
		--with-external-srtp \
		--with-external-pa \
		|| return 1

	# TODO: make it build with only shared libs from system
	# rm -r third_party
	make dep || return 1
	make -j1 all || return 1
}

package() {
	cd "$_builddir"
	install -d "$pkgdir"/usr/lib
	make -j1 DESTDIR="$pkgdir" prefix=/usr install
}

pjsua() {
	pkgdesc="pjsua is an open source command line SIP user agent (softphone)"
	cd "$_builddir"/pjsip-apps/bin/
	install -D -m755 pjsua "$subpkgdir"/usr/bin/pjsua
}

md5sums="183f7144b9aa238884243c0fc52ece36  pjproject-2.5.5.tar.bz2
1f152ad611b5fb0e44676e2701b53a15  fix-bb-cp-uniqueness.patch
e835a442abf0fdc6614d5ba1cb961c11  pjproject_no_third_party.patch"
sha256sums="ab39207b761d3485199cd881410afeb2d171dff7c2bf75e8caae91c6dca508f3  pjproject-2.5.5.tar.bz2
cae0255ddb8b6b6e07f6da5b448ec669b0727dc6d6c795e0d1cd8ce9ae504d40  fix-bb-cp-uniqueness.patch
f95cb0e03efd5995b2ea5e130831edbf831c01fd5bbcf10b4dcbd77133d18a12  pjproject_no_third_party.patch"
sha512sums="7204326ed7d06241f7de02111641de792274fb126bb5c06ba6d7e2fbb6c6475b84fd4e1e63d44863207cbd79e75b654588567125476ce9d66bd1397708827a09  pjproject-2.5.5.tar.bz2
cc4077c9999d4eb28cfc34080ea2cdfdc905726565afdca96d26979d734a1d70522c5e6307ec33c0d4f5f1a5b26699787cbd31fecb73b860b6b417eda50f37a1  fix-bb-cp-uniqueness.patch
fe53b4e8a654dc4025a4bf6350f405953609abc9e7ed967882cb9eb9e6a659e348a0c3c7496c8093562a0dcc4a40459896d8ee1da7275dd089244338111bccbb  pjproject_no_third_party.patch"
