#!/usr/bin/bash

spotify=/usr/lib64/spotify-client/spotify
version=1.2.31.1205.g4d59ad7c_amd64.deb

if [ "$1" = '-v' ]; then
    echo Version: $version
    exit 0
fi

# board=http://community.spotify.com/t5/Desktop-Linux
# $board/ANNOUNCE-Spotify-0-8-3-for-GNU-Linux/td-p/60659/page/6
rm -f \$HOME/.cache/spotify/offline.bnk

# 0.8.8: Various reports requiring ~/cache/spotify to be wiped.
[ -f $HOME/.cache/spotify-$version ] || {
    rm -rf  $HOME/.cache/spotify
    touch  $HOME/.cache/spotify-$version
}

# $board/Illegal-Instruction-Client-Crash-Read-Here/td-p/194448
cat /proc/cpuinfo | grep -q 'sse2' || {
    zenity --error --text "Cannot run on this CPU (no sse2 support)"
    exit 1
}

# board= https://community.spotify.com/t5/Help-Desktop-Linux-Mac-and
# $board/quot-Failed-loading-skin-quot-error-on-Ubuntu-GNOME-13-04/m-p/1013124#M107324
unset DESKTOP_SESSION
unset GNOME_DESKTOP_SESSION_ID

# Check that we have write access to /dev/snd/*
snd_sample=$( find /dev/snd -type c | head -1 )
[ -w "$snd_sample" ] || {
    txt="You havn't write access to $snd_sample, problems ahead."
    zenity --warning  --text "$txt"
}

# LC_NUMERIC=en_US.utf8 is required since the Discover panel is otherwise not working properly
# as recorded at http://community.spotify.com/t5/Help-Desktop-Linux-Mac-and/Discover-page-and-radio-page-not-working-on-Linux-on-my-desktop/m-p/503548#M57375
LD_LIBRARY_PATH=$( dirname $spotify ) LC_NUMERIC=en_US.utf8 $spotify $@
