# Description: tools and libraries for working with Musepack audio streams.
# URL: http://www.musepack.net/
# Maintainer: John McQuah, jmcquah at disroot dot org
# Depends on: 

name=musepack
version=1.3.0
release=1
source=(https://files.musepack.net/source/${name}_src_r475.tar.gz)

build() {
	cd ${name}*

	# disable mpcenc (broken when built against recent glibc)
	# and any targets whose dependencies are not present
	sed -e '/add_subdirectory(mpcenc)/d' \
		-e '/add_subdirectory(mpcpsy)/d' \
		-i CMakeLists.txt
	[ -e /usr/lib/libcuefile.so ] || \
		sed '/add_subdirectory(mpcchap)/d' -i CMakeLists.txt
	[ -e /usr/lib/libreplaygain.so ] || \
		sed '/add_subdirectory(mpcgain)/d' -i CMakeLists.txt
	
	# fix the headers
	sed -i 's/^const /extern const /' libmpcdec/requant.h
	sed -e '/^# ifdef USE_TERMIOS/i\#include <sys/time.h>\ \ ' \
		-i mpcenc/keyboard.c
	for INC in $(grep -lrE "^#include <cuetools/" .); do
		sed -i 's|^#include <cuetools|#include <cuefile|' $INC
	done

	mkdir build && cd build
	cmake -G Ninja -Wno-dev	\
		-DCMAKE_INSTALL_PREFIX="/usr" \
		-DCMAKE_BUILD_TYPE=Release \
		-DCMAKE_C_FLAGS="$CFLAGS" ..

	ninja -j ${JOBS:-1}
	DESTDIR=$PKG ninja install
	rm -rf $PKG/usr/include/mpc/.svn

	install -d "$PKG/usr/lib"
	install -m 0755 libmpcdec/libmpcdec.so "$PKG/usr/lib/libmpcdec.so"
}
