# Contributor: Carlo Landmeter <clandmeter@gmail.com>
# Maintainer: Carlo Landmeter <clandmeter@gmail.com>
pkgname=emby
pkgver=3.0.5911
pkgrel=2
pkgdesc="Bring together your videos, music, photos, and live television"
url="http://emby.media"
arch="x86_64 x86"
license="GPL-2.0"
depends="ffmpeg mono imagemagick sqlite"
pkgusers=$pkgname
pkggroups=$pkgname
depends_dev=""
makedepends="$depends_dev"
install="$pkgname.pre-install"
subpackages="$pkgname-migrate"
source="Emby-$pkgver.tar.gz::https://github.com/MediaBrowser/Emby/archive/$pkgver.tar.gz
	emby-server
	$pkgname.initd
	$pkgname.confd
	emby-migrate"

_builddir="$srcdir"/Emby-$pkgver

prepare() {
	local i
	local libMagicWand="libMagickWand-7.Q16HDRI.so.0"
	cd "$_builddir"
	for i in $source; do
		case $i in
		*.patch) msg $i; patch -p1 -i "$srcdir"/$i || return 1;;
		esac
	done
	mkdir -p "$_builddir"/build
	if [ -e "/usr/lib/$libMagicWand" ]; then
		sed -i s/libMagickWand-6.Q8.so/$libMagicWand/g \
			"$_builddir"/MediaBrowser.Server.Mono/ImageMagickSharp.dll.config \
			|| return 1
	else
		die "libMagickWand not found!"
	fi
	if [ -e "/usr/lib/libsqlite3.so.0" ]; then
		sed -i s/libsqlite3.so/libsqlite3.so.0/g \
			"$_builddir"/MediaBrowser.Server.Mono/System.Data.SQLite.dll.config \
			|| return 1
	else
		die "libsqlite3 not found!"
	fi
}

build() {
	cd "$_builddir"
	xbuild \
		/p:Configuration='Release Mono' \
		/p:Platform='Any CPU' \
		/p:OutputPath="$_builddir"/build \
		/t:build MediaBrowser.Mono.sln || return 1
}

package() {
	cd "$_builddir"
	mkdir -p "$pkgdir"/usr/lib/emby || return 1
	install -d -o $pkgusers -g $pkggroups \
		 "$pkgdir"/var/lib/emby || return 1
	cp -r "$_builddir"/build/* \
		"$pkgdir"/usr/lib/emby/
	install -Dm755 "$srcdir"/emby-server \
		"$pkgdir"/usr/bin/emby-server || return 1
	install -Dm755 "$srcdir"/$pkgname.initd \
		"$pkgdir"/etc/init.d/emby || return 1
	install -Dm644 "$srcdir"/$pkgname.confd \
		"$pkgdir"/etc/conf.d/emby || return 1
}

migrate() {
	pkgdesc="$pkgname database migration script"
	depends="sqlite"
	install -Dm755 "$srcdir"/emby-migrate \
		"$subpkgdir"/usr/bin/emby-migrate || return 1
}

md5sums="34ff145682f3dad664899e5f9605417e  Emby-3.0.5911.tar.gz
1fb8f122e9659da42be092fd054debf3  emby-server
1dd4899fb086d8a9549ed8259d130024  emby.initd
780b7fddf63030437fd398eb679bfb42  emby.confd
e176391105b6c4793ada2b9745cf82c7  emby-migrate"
sha256sums="1a4b4ad69473d2f8120079ecf045ba350c9867604c72978763100e7a1d8d57e9  Emby-3.0.5911.tar.gz
e287a2d6b88c0d6b17b29c0be7463e489eab4ea5153d52e3e00e47119384e130  emby-server
82e30752bfe43ac66adaca88651b839d396897cc85c3a45fa3c3c2d473cd779c  emby.initd
eadf0c8c2cee2ccc6761aa731cd58dd70086f1dc207acdd71ac1be9fa9f94ed4  emby.confd
0e3f6b7fe700a3bbdf97bdae8655453b34b1bd08fa8ae339e0fd130fe8670b0b  emby-migrate"
sha512sums="dcb8f6bf1890d7f8824a9791700416c298467e1f4be6c8b8edc1b3cd91d4415ba8fde1b155d8c184e4c0caf95538cc4354cf04e522b078fcea643336168eb5d0  Emby-3.0.5911.tar.gz
f161fbf2d83d91e5436578f1998410dba991c8d2de4fb8852ec106b13ce3e7119fec7432998dfffa2c488425e2a5eacaf34b0c4e6fc93ad370c1ba94b3eaa2e4  emby-server
2a520ee646b0489bbacd79679e75828194cbcd41845948816d1ae4d129f7cd582561f7357fa0c6d7f3f1970cc7900ae34338d54a4e54d5b21284c020a911e284  emby.initd
ebdb39d3e17277ecc51709fc6862265295892d6aa1b5f1b9da537a80552b81c845863cd058ab468f7cf20c22f8f56954b7cce4d149f8fa233ea19ae7aa0bc001  emby.confd
d4275622b26228c352ee9150441995c2062f6afdda46ccca176ad13a17d5ce432f6cf80b7c7d3029763d3e6fb79cf9a7a07bdbf4fef1dfaee2811c56ec0ec7b9  emby-migrate"
