File emby-server.spec of Package emby-server

%global name emby-server
%global release 0
%global data_dir /var/lib/emby-server
%global install_dir /usr/lib/emby-server
%global user_group media
%global admin_group emby
%global user emby
#We don't need debug info packages (allows to build correctly on RPM distros with rpm > 4.13)
%global debug_package %{nil}

Name:           %{name}
Version:        3.1.0
Release:        %{release}
Summary:        Emby is a home media server built on top of other popular open source technologies such as Service Stack, jQuery, jQuery mobile, and Mono.
Vendor:         Emby
Group:          Applications/Multimedia
License:        GPL
URL:            http://emby.media/
Source0:        %{name}-%{version}.tar.gz
Source1:	redhat.default.emby-server
Source2: 	redhat.emby
Source3:	redhat.emby-server
Source4:	redhat.emby-server.conf
Source5:	redhat.emby-server.service
Source6:	redhat.emby-server.sh
Source7:	redhat.restart.sh
BuildRequires:	mono-xbuild >= 4.4.2.11
BuildRequires:	mono-devel >= 4.4.2.11
BuildRequires:  referenceassemblies-pcl
%if 0%{?fedora_version} >= 23
BuildRequires:	mono-winfxcore >= 4.4.2.11
%endif
BuildRequires:	embymagick
BuildRequires:  embysqlite
%if 0%{?suse_version} >= 1315
BuildRequires:	pkgconfig(systemd)
BuildRequires:  shadow
BuildRequires:  dummy-release
BuildRequires:  lsb-release
%endif
Requires:	embysqlite
Requires:	mono-core >= 4.4.2.11
Requires:	mono-wcf >= 4.4.2.11
Requires:       mono-locale-extras >= 4.4.2.11
Requires:	embymagick
%if 0%{?suse_version} >= 1315
Requires:       at
%else
Requires:	redhat-lsb-core
%endif
AutoReqProv:	no
Obsoletes:      MediaBrowserServer , MediaBrowserServer-dev, emby-server-core
Conflicts:		emby-server-beta, emby-server-dev

%description
Emby (formely known as Media Browser) is a home media server built on top of other popular open source technologies such as Service Stack, jQuery, jQuery mobile, and Mono.
It features a REST-based api with built-in documention to facilitate client development. We also have client libraries for our api to enable rapid development.

%prep
%setup -c -n %{name}-%{version}-build -q
mkdir -p ./etc/default ./etc/init.d ./etc/sudoers.d ./usr/lib/emby-server ./usr/lib/systemd/system
cp %{_sourcedir}/redhat.default.emby-server ./etc/default/emby-server
cp %{_sourcedir}/redhat.emby-server.conf ./etc/emby-server.conf
%if 0%{?centos_version} == 600
	cp %{_sourcedir}/redhat.emby-server ./etc/init.d/emby-server
	chmod 0755 ./etc/init.d/emby-server
%else
	cp %{_sourcedir}/redhat.emby-server.service ./usr/lib/systemd/system/emby-server.service
	chmod 0444 ./usr/lib/systemd/system/emby-server.service
%endif
%if 0%{?suse_version} >= 1315
	cp %{_sourcedir}/opensuse.emby ./etc/sudoers.d/emby
    cp %{_sourcedir}/opensuse.emby-server.sh ./usr/lib/emby-server/emby-server.sh
	cp %{_sourcedir}/opensuse.restart.sh ./usr/lib/emby-server/restart.sh
	rm -f ./usr/lib/systemd/system/emby-server.service
	cp %{_sourcedir}/opensuse.emby-server.service ./usr/lib/systemd/system/emby-server.service
	chmod 0444 ./usr/lib/systemd/system/emby-server.service
%else
	cp %{_sourcedir}/redhat.emby ./etc/sudoers.d/emby
    cp %{_sourcedir}/redhat.emby-server.sh ./usr/lib/emby-server/emby-server.sh
	cp %{_sourcedir}/redhat.restart.sh ./usr/lib/emby-server/restart.sh
%endif
chmod 0755 ./usr/lib/emby-server/emby-server.sh
chmod 0755 ./usr/lib/emby-server/restart.sh

%build
cd %{name}-%{version}
mkdir -p ..%{install_dir}/bin
xbuild /p:Configuration="Release Mono" /p:Platform="Any CPU" /t:clean MediaBrowser.sln
xbuild /p:Configuration="Release Mono" /p:Platform="Any CPU" /t:build MediaBrowser.sln
mv MediaBrowser.Server.Mono/bin/Release/* ..%{install_dir}/bin
cd ..
rm -rf %{name}-%{version}
cd .%{install_dir}/bin
rm -rf ./*.dylib
mono --aot=full -O=all MediaBrowser.Server.Mono.exe

%pre
getent group %{user_group} >/dev/null || groupadd -r %{user_group}
getent group %{admin_group} >/dev/null || groupadd -r %{admin_group}
getent passwd %{user} >/dev/null || useradd -r -g %{admin_group} -d %{data_dir} -s /sbin/nologin -c "Account under which Emby runs" %{user}
usermod -aG %{user_group} %{user}
%if 0%{?suse_version} >= 1315
	%service_add_pre emby-server.service
%endif

%install
mkdir -p %{buildroot}%{install_dir}
mkdir -p %{buildroot}%{data_dir}
cp -vR * %{buildroot}

%post
cd %{install_dir}/bin
imageWand="libMagickWand-6.Q8.so.2"
imageCore="libMagickCore-6.Q8.so.2"
sqlitelib="libembysqlite3.so.0"
echo -e "<configuration>\n\t<dllmap dll=\"CORE_RL_Wand_.dll\" target=\"$imageWand\" os=\"linux\"/>\n\t<dllmap dll=\"CORE_RL_magick_.dll\" target=\"$imageCore\" os=\"linux\"/>\n</configuration>" > ImageMagickSharp.dll.config
echo -e "<configuration>\n\t<dllmap dll=\"sqlite3\" target=\"$sqlitelib\" os=\"linux\"/>\n</configuration>" > System.Data.SQLite.dll.config
echo -e "<configuration>\n\t<dllmap dll=\"sqlite3\" target=\"$sqlitelib\" os=\"linux\"/>\n</configuration>" > SQLitePCLRaw.provider.sqlite3.dll.config
# create emby-server symlink for execution
if [ ! -e /usr/bin/emby-server ]; then
    ln -sf /usr/lib/emby-server/emby-server.sh /usr/bin/emby-server
fi
# Data migration script for users upgrading from older MediaBrowser/Emby packages to the new rebranded package
if [ -d "/var/opt/MediaBrowser" ] && [ ! -f "/var/opt/MediaBrowser/.already_imported" ]; then
	mv /var/lib/emby-server /var/lib/emby-server.default
	mkdir /var/lib/emby-server
	cp -a /var/opt/MediaBrowser/MediaBrowserServer/* /var/lib/emby-server
	chown emby.emby /var/lib/emby-server -R
	cp -a /var/lib/emby-server/data/library.db /var/lib/emby-server/data/librarydb.bak
	rm /var/lib/emby-server/data/library.db-shm
	rm /var/lib/emby-server/data/library.db-wal
	sqlite3 /var/lib/emby-server/data/library.db "UPDATE TypedBaseItems SET data = CAST(REPLACE(CAST(data AS TEXT), '/var/opt/MediaBrowser/MediaBrowserServer', '/var/lib/emby-server') AS BLOB)"
	touch /var/opt/MediaBrowser/.already_imported
elif [ -d "/var/opt/Emby" ] && [ ! -f "/var/opt/Emby/.already_imported" ]; then
	mv /var/lib/emby-server /var/lib/emby-server.default
	mkdir /var/lib/emby-server
	cp -a /var/opt/Emby/server/* /var/lib/emby-server/
	chown emby.emby /var/lib/emby-server -R
	cp -a /var/lib/emby-server/data/library.db /var/lib/emby-server/data/librarydb.bak
    rm /var/lib/emby-server/data/library.db-shm
    rm /var/lib/emby-server/data/library.db-wal
	sqlite3 /var/lib/emby-server/data/library.db "UPDATE TypedBaseItems SET data = CAST(REPLACE(CAST(data AS TEXT), '/var/opt/Emby/server', '/var/lib/emby-server') AS BLOB)"
	touch /var/opt/Emby/.already_imported
fi
%if 0%{?suse_version} >= 1315
	%service_add_post emby-server.service
%else
	DISTRIBUTOR=$(lsb_release -i | cut -f 2)
	RELEASE=$(lsb_release -r | cut -f 2 | cut -d . -f 1)
	if [ "$DISTRIBUTOR" == "Fedora" ]; then
		systemctl daemon-reload > /dev/null
	elif [ "$DISTRIBUTOR" == "CentOS" ]; then
		if [ "$RELEASE" == "6" ]; then
			chkconfig --add emby-server > /dev/null
		elif [ "$RELEASE" == "7" ]; then
			systemctl daemon-reload > /dev/null
		fi
	fi
%endif

%preun
%if 0%{?suse_version} >= 1315
	%service_del_preun emby-server.service
%else
	DISTRIBUTOR=$(lsb_release -i | cut -f 2)
	RELEASE=$(lsb_release -r | cut -f 2 | cut -d . -f 1)
	if [ "$DISTRIBUTOR" == "Fedora" ] && [ -f "/usr/lib/systemd/system/emby-server.service" ]; then
		systemctl stop emby-server > /dev/null
	elif [ "$DISTRIBUTOR" == "CentOS" ]; then
		if [ "$RELEASE" == "6" ] && [ -f "/etc/init.d/emby-server" ]; then
			service emby-server stop > /dev/null
		elif [ "$RELEASE" == "7" ] && [ -f "/usr/lib/systemd/system/emby-server.service" ]; then
			systemctl stop emby-server > /dev/null
		fi
	fi
%endif

%postun
%if 0%{?suse_version} >= 1315
	%service_del_postun emby-server.service
%endif

%files
%{install_dir}
%attr(775,%{user},%{admin_group}) %{data_dir}
%{_sysconfdir}/default/emby-server
%config(noreplace) %{_sysconfdir}/emby-server.conf
%if 0%{?centos_version} == 600
	%{_sysconfdir}/init.d/emby-server
%else
	/usr/lib/systemd/system/emby-server.service
%endif
%if 0%{?suse_version} >= 1315
	%{_sysconfdir}/sudoers.d
%else
	%{_sysconfdir}/sudoers.d/emby
%endif


%changelog