# 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/${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-client/spotify-client_${SVERSION}.deb"
		echo "${SCHECKSUM}  spotify-client_${SVERSION}.deb" >checksum
		sha256sum -c checksum
		ar x "spotify-client_${SVERSION}.deb"
		tar -xf data.tar.gz
	)
	if [ $? -ne 0 ] ; then
		echo "Failed downloading spotify client"
		rm -r "$_BUILDDIR";
		exit 1;
	fi

	[ -d "/usr/share/spotify/spotify-client" ] && rm -rf /usr/share/spotify/spotify-client
	mv -f "${_BUILDDIR}/usr/share/spotify" /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
	linklib "libssl" "1.0.0"
	linklib "libcrypto" "1.0.0"
	rm -r "$_BUILDDIR"
fi
