# 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"
	(
                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 || exit 1
                ar x "spotify-client_${SVERSION}.deb"
		[ -f data.tar.gz ] && tar xzf data.tar.gz 2>/dev/null
		[ -f data.tar.xz ] && tar xJf data.tar.xz 2>/dev/null
        ) || {
                echo Error while extracting;
                rm -r "$_BUILDDIR";
                exit 1;
        }
	mkdir -p /usr/share/licenses/spotify
	mv "${_BUILDDIR}/usr/share/doc/spotify-client/copyright" /usr/share/licenses/spotify/

	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

