#!/bin/bash
#
# Configure spotify setup
#


function on_prefix_change()
{
    bindir=$prefix/bin
    libdir=$prefix/lib
    datadir=$prefix/share
    mandir=$datadir/man
}

binary='spotify'
prefix='/usr/local'
on_prefix_change

function usage()
{
cat << EOF
Usage: configure [options]

--prefix=path  Set common prefix, defaults to $prefix.
--datadir=path Directory for non-arch data, defaults to $datadir.
--libdir=path  Directory for arch data, defaults to $libdir.
--bindir=path  Directory for binary, defaults to $bindir.
--mandir=path  Directory for manual pages, defaults to $mandir.
--user         Configure for personal install, nust be first option.
--package=path Downloaded spotify .deb package, by default determined
               by examining the spotify download dir. Implies local
               operation, nothing is downloaded from spotify

EOF
exit 0
}


while [ "$1" != "" ]; do
    case $1 in
        --datadir=*)
            datadir=${1##*=}
            ;;
        --prefix=*)
            prefix=${1##*=}
            on_prefix_change
            ;;
        --libdir=*)
            libdir=${1##*=}
            ;;
        --mandir=*)
            mandir=${1##*=}
            ;;
        --bindir=*)
            bindir=${1##*=}
            ;;
        --package=*)
            package=${1##*=}
            [ -r "$package" ] || {
                echo "Cannot open package file" >&2
                exit 2
            }
            ;;
        --user)
            prefix=$HOME/.local
            on_prefix_change
            bindir=$HOME/bin
            binary='my-spotify'
            ;;
        *) usage
            ;;
    esac
    shift
done

echo "Checking build and support dependencies"

sts="$(ldconfig --version  &>/dev/null && \
           ldconfig --version  | head -1 | sed 's/$/ (ok)/' || \
               echo 'ERROR: Not found')"
if [[ "$sts" == ERROR* ]]; then
    export PATH="$PATH:/sbin:/usr/sbin"
    if ldconfig --version &> /dev/null; then
        echo  "INFO: ldconfig(8) not found default PATH. "
        echo  "INFO: Trying adding '/sbin:/usr/sbin'"
        sts="$(ldconfig --version | head -1 | sed 's/$/ (ok)/' || \
                 echo 'ERROR: Not found')"
    else
        echo "Cannot find ldconfig(8), please install it. Giving up." >&2
        exit 2
    fi
fi
echo "    ldconfig:... $sts"

echo -n "    python: "
sts="$(python --version 2>&1) (ok)" || sts="ERROR: Not found"
echo $sts

if [  -z "$package" ]; then
    echo -n "    wget: "
    sts="$(wget --version | head -1) (ok)" || sts="ERROR: Not found"
    echo $sts
fi

echo -n "    zenity: ..."
sts="$(which zenity &>/dev/null && echo '   (ok)' || \
         echo 'WARNING: Not found (make will run, but not spotify)')"
echo $sts

echo -n "    ar: "
sts="$(ar --version &>/dev/null && \
          ar --version | head -1| sed 's/$/ (ok)/' || \
              echo 'ERROR: Not found (in binutils package?)')"
echo $sts

echo -n "    lsb_release: "
sts="trying -i: $(lsb_release -i &>/dev/null && \
                      lsb_release -i 2>&1 | sed 's/$/ (ok)/' || \
                          echo 'ERROR: Not found')"
echo $sts

echo -n "    make: "
sts="$(make --version &>/dev/null && \
           make --version 2>&1  | head -1 | sed 's/$/ (ok)/' || \
               echo 'ERROR: Not found')"
[[ "$sts" = ERROR* ]] || \
    [[ $sts = *GNU* ]] || sts="ERROR: make is not GNU make."
echo $sts

echo -n "    install: "
sts="$(install --version &>/dev/null && \
           install --version | head -1 | sed s'/$/ (ok)/'  || \
               echo 'ERROR: not found' )"
[[ "$sts" == ERROR* ]] || {
    install -pD configure foo/bar
    [[ -d foo && -f foo/bar ]] && rm -rf foo \
        || sts="ERROR: install -pD doesn't work as expected."
    rm -rf foo
}
echo $sts

echo -n "    desktop-file-validate: "
sts="$(desktop-file-validate -h &>/dev/null && echo ' (ok)' || \
    echo 'WARNING: Not found (make install will run, but not make register)')"
echo $sts

echo -n "    gtk-update-icon-cache: "
sts="$(gtk-update-icon-cache -h &>/dev/null && echo ' (ok)' || \
    echo 'WARNING: Not found (make install will run, but not make register)')"
echo $sts

echo -n "    update-desktop-database: "
sts="$(update-desktop-database -h &>/dev/null && echo ' (ok)' || \
    echo 'WARNING: Not found (make install will run, but not make register)')"
echo $sts

echo -n "Determining distro:... "
dist=$(lsb_release -i || echo "Unknown")
dist=$( echo ${dist##*:} )
case "$dist" in
    Ubuntu)
        echo -n '(LinuxMint handled as)... '
        dist='Ubuntu'
        ;;
    Debian)
        if [[ "$( lsb_release -r)" == *7.[0-9] ]]; then
            echo -n '(Debian 7.0 Wheeze handled as)...'
            dist='Ubuntu'
        elif [[ "$( lsb_release -r)" == *'unstable' ]]; then
            echo -n '(Debian unstable handled as)...'
            dist='Ubuntu'
        fi
        ;;
    CentOS|RedHat*)
        echo -n "($dist handled as RHEL)..."
        dist='RHEL'
        ;;
    *)
        echo -n "(no \"special configuration\" of distro needed)..."
        ;;
esac
echo $dist

echo -n "Determining arch:... "
uname_arch=$(uname -i)
[ "$uname_arch" = 'unknown' ] && uname_arch=$(uname -m)
if [ "$uname_arch" = 'x86_64' ]; then
    arch="amd64"
    archlib='^[ ]*/.*64.*/.+'
else
    arch="i386"
    archlib='^[ ]*/lib/|/usr/lib/'
fi
echo $arch

if [ -z "$package" ]; then
    echo -n "Determining version:... "
    wget -qO spotify.dir http://repository.spotify.com/pool/non-free/s/spotify
    version=$(grep "spotify-client_.*$arch.deb" spotify.dir \
                  | sed 's/.*spotify-client_\([^-]*\).*/\1/' \
                  | sed -r 's/[ ]+/\n/g' \
                  | sort | tail -1 )

else
    version=$( echo $package | sed 's/.*spotify-client_\([^-]*\).*/\1/' )
    echo -n "Digging out version from --file:..."
fi
echo $version

# Check for openlssl-compatibility package on distros determined as RHEL,
if [ "$dist" = "RHEL" ]; then
    # Just to be sure we got what we need,
    echo -n "Locating rpm-binary:... "
    if command -v rpm &> /dev/null ; then
        sts=$(rpm --version)
        echo "$sts (ok)"
    else
        echo -n >&2 "Cannot find rpm-binary. Your installation is seriously "
        echo    >&2 "broken. Giving up."
        exit 2;
    fi

    # Try to locate the necessary openssl-libraries,
    echo -n "Checking for openssl098e-compatibility libraries..."
    if [[ -f $(rpm --eval %_libdir/libssl.so.0.9.8e) &&
          -f $(rpm --eval %_libdir/libcrypto.so.0.9.8e) ]] ; then
        echo "found (ok)"
    else
        echo >&2 "Could not find necessary openssl098e-compatibility libraries."
        echo -n >&2 "Suggestion: Try to install package with 'sudo yum install "
        echo >&2 "-y openssl098e'."
        exit 2
    fi
fi

if [ -z "$package" ]; then
    echo -n "Determine .deb file..."
    package=$(grep "spotify-client.*$arch.deb" spotify.dir \
                  | grep "$version" \
                  | sed 's/.*\(spotify-client.*deb\).*/\1/')
else
    echo -n "Using supplied package:..."
fi
echo $package

echo -n "Determine permissions for shared libs (SO_PERMS)..."
if [[ "$dist" = *Fedora*  ||  "$dist" == *SUSE* ]]; then
    so_perms=755
else
    so_perms=644
fi
echo $so_perms

rm -f spotify.dir

echo "Configuration paths:"
echo "      PATH:  $PATH"
echo "    prefix:  $prefix"
echo "    bindir:  $bindir"
echo "    libdir:  $libdir"
echo "    datadir: $datadir"
echo "    mandir:  $mandir"

echo "Writing Makefile"
sed -e  "s|@prefix@|$prefix|" \
    -e  "s|@bindir@|$bindir|" \
    -e  "s|@libdir@|$libdir|" \
    -e  "s|@mandir@|$mandir|" \
    -e  "s|@datadir@|$datadir|" \
    -e  "s|@version@|$version|" \
    -e  "s|@arch@|$arch|" \
    -e  "s;@archlib@;$archlib;" \
    -e  "s|@dist@|$dist|" \
    -e  "s|@archive@|$package|" \
    -e  "s|@so_perms@|$so_perms|" \
    -e  "s|@binary@|$binary|" \
    -e  "s|@path@|$PATH|" \
< Makefile.in > Makefile

echo "Writing spotify.desktop"
sed  -e "/^Exec=/s|=.*|=/bin/bash $bindir/$binary %U|"  \
    < spotify.desktop.in > spotify.desktop
if [[ "$binary" = my* ]]; then
    sed -i '/^Name/s/=.*/=My Spotify/' spotify.desktop
fi

echo "Writing spotify.bash"
sed  -e "/spotify=/s|=.*|=$libdir/spotify-client/spotify|" \
     -e "/version=/s/=.*/=$version/"        \
     <spotify.bash.in >spotify.bash
