# Makefile is generated from Makefile.in by configure
# Don't edit Makefile, your changes will be lost.

SHELL        := /bin/bash

help_msg      = \
Plain 'make' doesn't do anything. Targets: \n\
   - download:   Download all sources \n\
   - install:    Install, possibly using DESTDIR to define location. \n\
   - register:   Notify system about new desktop icons etc. \n\
   - uninstall:  Remove everything installed.

PATH          = /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
dist          = Unknown
arch          = amd64
archlib       = ^[ ]*/.*64.*/.+
binary        = spotify
repo          = http://repository.spotify.com/pool/non-free/s/spotify-client
version       = 1.2.26.1187.g36b715a1_amd64.deb
archive       = /builds/charliewolf-rpms/spotify-client/spotify-client_1.2.26.1187.g36b715a1_amd64.deb
so_perms      = 644
prefix        = /usr
libdir        = /usr/lib64
datadir       = /usr/share
bindir        = /usr/bin
mandir        = /usr/share/man
app_libdir    = $(libdir)/spotify-client
app_datadir   = $(datadir)/spotify-client

DOCS          =


SOURCES       = spotify.bash spotify.1 spotify.desktop $(archive)
ifeq ($(dist),Fedora)
SOURCES      += $(wildcard fedora/$(arch)/*.so.*)
else ifeq ($(dist),Ubuntu)
SOURCES      += $(wildcard ubuntu/$(arch)/*.so.*)
DOCS         += $(wildcard ubuntu/$(arch)/*)
else ifeq ($(dist),Mageia)
SOURCES      += $(wildcard mageia2/$(arch)/*.so.*)
DOCS         += $(wildcard mageia2/doc/*)
else ifeq ($(dist),RHEL)
SOURCES      += $(wildcard rhel/$(arch)/*)
DOCS         += $(wildcard rhel/doc/*)
endif

lib_to_data  := $(shell python3 -c 'import os.path; 		\
					print(os.path.relpath("$(app_datadir)",	\
					"$(app_libdir)"))')
all: help

lib_to_data:
	echo lib_to_data: $(lib_to_data)

help:
	@echo -e "\n$(help_msg)\n"

download: $(archive)
	@# Download all platform sources

download-all: download
	@# Download all architectures.
	tarball=$(archive); \
	[[ $$tarball = *386* ]] && \
	    other=$${tarball/i386/amd64} || other=$${tarball/amd64/i386}; \
	wget -Nq $(repo)/$$other

check_sources:
	@test -f $(archive) || {                                    \
	    echo "$(archive) not available (try 'make download')";  \
	    exit 1;                                                 \
	}

$(archive):
	wget -Nq -O $@ $(repo)/$@

unpack: check_sources
	@# Unpack into libdir (arched files) and datadir(noarch).
	ar x $(archive)
	tar xzf data.tar.gz
	@rm -rf $(DESTDIR)$(app_libdir) $(DESTDIR)/$(app_datadir)
	@mkdir -p $(DESTDIR)$(app_libdir)/Data || :
	@mkdir -p $(DESTDIR)$(app_datadir) || :
	cp -ar ./usr/share/spotify/*  $(DESTDIR)$(app_datadir)
	mv $(DESTDIR)$(app_datadir)/libcef.so $(DESTDIR)$(app_libdir)
	cp ./usr/bin/spotify $(DESTDIR)$(app_libdir)
	tar xzf control.tar.gz
	mv control md5sums $(DESTDIR)$(app_datadir)
	@rm -f prerm postinst

set_so_perms: unpack
	@# Set so files perms to 755 (e. g, fedora) or 644 (e. g. debian).
	chmod $(so_perms) \
	    $$( find $(DESTDIR)$(app_libdir) -name '*.so*' -writable)

bundled:  check_sources unpack
	[ -n "$(DOCS)" ] && cp -a $(DOCS) $(DESTDIR)$(app_datadir) || :
ifeq ($(dist),Fedora)
	cp -a fedora/$(arch)/*.so.* $(DESTDIR)$(app_libdir)
else ifeq ($(dist),Ubuntu)
	cp -a ubuntu/$(arch)/libssl.so.0.9.8 $(DESTDIR)$(app_libdir)
	cp -a ubuntu/$(arch)/libcrypto.so.0.9.8 $(DESTDIR)$(app_libdir)
else ifeq ($(dist),Mageia)
	cp -a mageia2/$(arch)/libQtDBus.so.4.8.1 $(DESTDIR)$(app_libdir)
else ifeq ($(dist),RHEL)
	cp -ra rhel/$(arch)/* $(DESTDIR)$(app_libdir)
endif

applinks: unpack
	@# Add links to datadir in libdir (where things are searched for).
	cd $(DESTDIR)$(app_libdir); \
	    for f in $(DESTDIR)$(app_datadir)/*; do \
	        ln -sf $(lib_to_data)/$$(basename $$f) . ; \
	    done

appdata:
	mkdir -p $(DESTDIR)$(datadir)/appdata  || echo '(ignored)'
	cp appdata.xml $(DESTDIR)$(datadir)/appdata/spotify.xml

syslinks: unpack bundled
	@# Match unresolved references found using ldd with system libs
	@# in linker cache found using ldconfig -p. Create links and
	@# report. If distro is RHEL handle linkage to libssl/libcrypto
	@# separately against openssl098e-compatibility libs.
	@cd $(DESTDIR)$(app_libdir);                                     \
	hostlink_libssl='libssl.so.1.*0.*';                              \
	hostlink_libcrypto='libcrypto.so.1.*0.*';                        \
	if [[ "$(dist)" == "RHEL" ]]; then                               \
	  hostlink_libssl='libssl.so.6';                                 \
	  hostlink_libcrypto='libcrypto.so.6';                           \
	fi;                                                              \
	for dep in $$( ldd  spotify | awk '/found$$/ {print $$1}' ); do  \
	    case $$dep in                                                \
	        libssl*)     hostlink="$$hostlink_libssl" ;;             \
	        libcrypto*)  hostlink="$$hostlink_libcrypto" ;;          \
	        libudev*)    hostlink='libudev.so.1.*.*' ;;              \
	        *)           hostlink="$${dep%%.*}" ;;                   \
	    esac;                                                        \
	    hostlink=$$( ldconfig -p | grep $$hostlink | sed 's/.*=>//'  \
	                 | egrep -m1 '$(archlib)' || :);                 \
	    if test "$$hostlink" != ''; then                             \
	        ln -sf $$hostlink $$dep;                                 \
	        echo INFO: "resolving $$dep as \"$$hostlink\"";          \
	    else                                                         \
	        echo "WARNING; cant resolve spotify dependency: $$dep" ; \
	    fi                                                           \
	done || :

desktop:  unpack check_sources appdata
	@# Install icons, desktop file etc.
	@icondir="$(DESTDIR)$(datadir)/icons/hicolor";                   \
	src_icons="$(DESTDIR)$(app_datadir)/icons";                      \
	echo "Installing icons in $$icondir";                            \
	for size in 24 32 48 64 128 256; do                              \
	    install -m 644 -pD  $$src_icons/spotify-linux-$$size.png     \
	        $$icondir/$${size}x$${size}/apps/spotify-client.png;     \
	done
	[ -d $(DESTDIR)$(datadir)/applications ] ||                      \
	    mkdir  $(DESTDIR)$(datadir)/applications
	desktop-file-install --dir $(DESTDIR)$(datadir)/applications     \
	    spotify.desktop
	[ "$(dist)" = 'Ubuntu' ] && chmod 755                            \
	    $(DESTDIR)$(datadir)/applications/spotify.desktop  || :

addons: check_sources
	@#  Manpage and wrapper script
	install -m 755 -pD spotify.bash $(DESTDIR)$(bindir)/$(binary)
	install -m 644 -pD spotify.1 $(DESTDIR)$(mandir)/man1/$(binary).1

install: applinks syslinks unpack addons bundled desktop set_so_perms
	cp Makefile $(DESTDIR)$(app_datadir)
	cp README.md  $(DESTDIR)$(app_datadir)


check-deps:
	@deps=$$( ldd $(DESTDIR)$(app_libdir)/spotify-bin        \
	              | grep 'not.*found' );                     \
	if [ -z "$$deps" ]; then                                 \
	    echo "OK";                                           \
	    exit 0;                                              \
	else                                                     \
	    echo "Error: unresolved dependencies"; echo $$deps;  \
	    exit 1;                                              \
	fi

register:
	@# Register icons and check desktop file. Not done by default, makes
	@# no sense for a temporary install e. g., when packaging.
	-gtk-update-icon-cache --ignore-theme-index \
	    $(DESTDIR)$(datadir)/icons/hicolor
	-desktop-file-validate \
	    $(DESTDIR)$(datadir)/applications/spotify.desktop
	-update-desktop-database  $(DESTDIR)$(datadir)/applications

uninstall:
	rm -rf $(DESTDIR)$(app_datadir)
	rm -rf $(DESTDIR)$(app_libdir)
	rm -f  $(DESTDIR)$(bindir)/spotify
	rm -f  $(DESTDIR)$(datadir)/icons/hicolor/*/apps/spotify-client.png
	rm -f  $(DESTDIR)$(mandir)/man1/spotify.1
	rm -f  $(DESTDIR)$(datadir)/applications/spotify.desktop

clean:
	rm -rf $(archive) spotify.bash opt usr
	rm -rf control.tar.gz data.tar.gz *.deb debian-binary github
	rm -f spotify.desktop Makefile
