# INSTALL for 'spotify'
# Fetching proprietary binaries at install-time

_BUILDDIR="/tmp/spotify.build"
_LIBS=$(ldconfig -vNX -n /usr/lib 2>/dev/null)

linklib() {
	_LIB=$(echo "$_LIBS" | grep -m 1 "${1}\.so" | sed 's/\s*\([^ ]*\).*$/\1/')
	ln -sf "/usr/lib/${_LIB}" "/usr/share/spotify/libs/${1}.so.${2}"
}

if test "$ACTION" = "post"; then
	. /usr/share/spotify/pkgdata
	mkdir -p "$_BUILDDIR"
	(
		set -e
		cd "$_BUILDDIR"
		xbps-uhelper fetch "http://repository.spotify.com/pool/non-free/s/spotify/spotify-client_${SVERSION}.deb"
		echo "${SCHECKSUM}  spotify-client_${SVERSION}.deb" >checksum
		sha256sum -c checksum
		ar x "spotify-client_${SVERSION}.deb"
		tar -xf data.tar.*
	)
	if [ $? -ne 0 ] ; then
		echo "Failed downloading spotify client"
		rm -r "$_BUILDDIR";
		exit 1;
	fi
	mkdir -p /usr/share/licenses/spotify
	mv "${_BUILDDIR}/usr/share/doc/spotify-client/copyright" /usr/share/licenses/spotify/

	[ -d "/usr/share/spotify/spotify-client" ] && rm -rf /usr/share/spotify/spotify-client
	mv -f "${_BUILDDIR}/opt/spotify/spotify-client" /usr/share/spotify/spotify-client
	for _s in 16 22 24 32 48 64 128 256 512; do
		mkdir -p "/usr/share/icons/hicolor/${_s}x${_s}/apps"
		ln -sf "/usr/share/spotify/spotify-client/Icons/spotify-linux-${_s}.png" "/usr/share/icons/hicolor/${_s}x${_s}/apps/spotify-client.png"
	done
	mkdir -p /usr/share/applications
	ln -sf /usr/share/spotify/spotify-client/spotify.desktop /usr/share/applications/spotify.desktop

	if test "$ARCH" = "x86_64"; then
		linklib "libssl" "1.0.0"
		linklib "libcrypto" "1.0.0"
		linklib "libudev" "0"
	else
		linklib "libssl" "0.9.8"
		linklib "libcrypto" "0.9.8"
		linklib "libnss3" "1d"
		linklib "libnssutil3" "1d"
		linklib "libsmime3" "1d"
		linklib "libplc4" "0d"
		linklib "libnspr4" "0d"
		linklib "libudev" "0"
	fi

	rm -r "$_BUILDDIR"
fi

