#!/bin/bash
#
# Copyright (c) 2013 Spotify AB

# This script makes symlinks to some missing libs.
# It's sort of a hack, but it seems to work ok.
#
# Spotify looks for libs next to the executable,
# or in the Data folder. But SpotifyHelper only
# looks in Data, so put the symlinks there!

# Find out architecture
MACHINE_TYPE=$(uname -m)
WANTED_LIB_TYPE="ELF 32-bit"
if [ "${MACHINE_TYPE}" = "x86_64" ]; then
	WANTED_LIB_TYPE="ELF 64-bit"
fi

# ldconfig is in /sbin which isn't in the path on some systems
export PATH=$PATH:/sbin

# Symlink libudev.so.0 => libudev.so.1
LIBUDEV0=$(ldconfig -p | grep libudev.so.0)
if [ -z "${LIBUDEV0}" ]; then
	for LIBUDEV1 in $(ldconfig -p | grep libudev.so.1 | cut -d " " -f 4); do
		MATCH=$(file -L ${LIBUDEV1} | grep "${WANTED_LIB_TYPE}")
		if [ ! -z "${MATCH}" ]; then
			ln -sf ${LIBUDEV1} Data/libudev.so.0
		fi
	done
fi
