#!/bin/sh
##############################################################################
# Name: setup-musicip
# 
# Setup script for installing MusicIP Mixer on VortexBox.
#
# Usage: setup-musicip
#
# Author: Ron Olsen <ronolsen@comcast.net>
#
# Date: Thurs Mar 4 2010
# Version: 1.0
#
# * Mon Mar 08 2010 Version 1.1: Ron Olsen <ronolsen@comcast.net>
# - Fix bug in /etc/init.d/mmserver status option.
#
# * Fri Dec 03 2010 Version 1.2: Ron Olsen <ronolsen@comcast.net>
# - Update URL for index.html from spicefly.com
#
# * Tues Feb 25 2014 Version 1.3: Ron Olsen <ronolsen@comcast.net>
# - Update URL for index-1.1.zip from spicefly.com
# - Use /usr/bin/cp instead of cp to copy index.html to install directory.
#
##############################################################################

# Clean up installation files
cleanup()
{
    cd /var/tmp
    rm -rf MusicMixer* MusicIP* index*.zip* index.html*
}

# Make sure wget is installed; if not, install it using yum

echo "Checking for wget..."
if [ ! -x /usr/bin/wget ]
then
    yum -y install wget
    if [ $? -ne 0 ]
    then
        echo "Error: Couldn't install wget"
        exit 1
    fi
fi

# Remove any stuff left over in /var/tmp
cleanup

# Download MusicIP Linux tarball in /var/tmp

cd /var/tmp
echo "Downloading MusicIP..."
wget http://www.amplifiedmusicservices.com/downloads/MusicMixer_x86_1.8.tgz

if [ $? -ne 0 ]
then
    echo "Error: Couldn't download MusicMixer_x86_1.8.tgz"
    exit 1
fi

# Unpack tarball 

echo "Unpacking tarball..."
gzip -dc MusicMixer_x86_1.8.tgz | tar xf -

if [ $? -ne 0 ]
then
    echo "Error: Couldn't unpack MusicMixer_x86_1.8.tgz"
    cleanup
    exit 1
fi

# Delete previous version in installation directory

if [ -d /opt/MusicMagicMixer ]
then
    echo "Removing previous version in /opt/MusicMagicMixer"
    rm -rf /opt/MusicMagicMixer
fi

# Install MusicIP in /opt/MusicMagicMixer

echo "Installing MusicIP in /opt/MusicMagicMixer..."
cd /var/tmp/MusicIP
cp -r MusicMagicMixer /opt
cd ..

# Download improved MusicIP web interface and install it 

echo "Downloading MusicIP web GUI..."
wget http://www.spicefly.com/files/index-1.1.zip

if [ $? -eq 0 ]
then
    echo "Installing MusicIP web GUI..."
    unzip index-1.1.zip
    /usr/bin/cp -f index.html /opt/MusicMagicMixer/server/index.html
else
    echo "Error: Couldn't download http://www.spicefly.com/images/slim/index.zip"
fi

# Install modified mmserver script /etc/init.d/mmserver

rm -f /etc/init.d/mmserver

echo "Installing /etc/init.d/mmserver..."
cat >> /etc/init.d/mmserver << EOF
#! /bin/sh
# chkconfig: 345 70 35
# description: Starts and stops the MusicMagic service 
#              for use by Squeezeboxserver mix generation.
#              Should be started before SBS at level 3.

# chkconfig also supports LSB-style init stanzas, and will apply them in preference to
# "chkconfig:" lines where available. A LSB stanza looks like:
### BEGIN INIT INFO
# Provides:		mmserver
# Required-Start: 
# Default-Start:	3 4 5
# Default-Stop:		0 1 2 6
# Short-Description:	Startup script for the MusicMagic Server
# Description:		MusicMagic Server provides music mixes for Squeezebox Server
### END INIT INFO

# NON-PRIVILEGED USER TO RUN MUSICMAGICSERVER.
# Doesn't work with user squeezeboxserver, so use root instead.
USER=root

# PATH TO THE MUSICMAGICMIXERSERVER 
export MUSICHOME=/opt/MusicMagicMixer/

case \$1 in
    start)
        if pgrep -f MusicMagicServer > /dev/null
        then
            echo "MusicIP Server is already running"
        else
	    echo "Starting MusicIP Server"
	    su - \$USER -c \$MUSICHOME"MusicMagicServer start  & > /dev/null" 
        fi
	;;
    stop)
        if pgrep -f MusicMagicServer > /dev/null
        then
	    echo "Stopping MusicIP Server"
	    su - \$USER -c \$MUSICHOME"MusicMagicServer stop  & > /dev/null" 
        else
            echo "MusicIP Server is already stopped"
        fi
	;;
    status)
        if pgrep -f MusicMagicServer > /dev/null
        then
            echo "MusicIP Server is running"
        else
            echo "MusicIP Server is stopped"
        fi
        ;;
    *)
        echo "Usage: service mmserver { start | stop | status }"
	;;
esac
EOF

# Make sure /etc/init.d/mmserver has correct permissions
chmod 755 /etc/init.d/mmserver

# Use chkconfig to add mmserver to boot-time run levels
echo "Adding mmserver to boot startup..."
chkconfig --add mmserver

# Start up mmserver
echo "Starting mmserver..."
service mmserver start

# Save instructions for users to start MusicIP Mixer and configure it for VortexBox

cat >> /opt/MusicMagicMixer/README << EOF
Instructions for using MusicIP on VortexBox

Make sure the MusicIP Server is running.
If it isn't, enter 'service mmserver start'.

Point your browser at http://vortexbox-ip:10002 to configure MusicIP settings.
Use the correct VortexBox IP address in place of 'vortexbox-ip'.
On the MusicIP web page, set 'root folder of music' to '/storage/music/flac'
Then click on 'Add Music'.
After Music IP has finished scanning your music library, click on 'Start Verification'.
This will take some time to complete!

When verification completes, point your browser at the SqueezeBox Server web page at
http://vortexbox-ip:9000 and click on 'Settings' on lower right corner of web page.
On the Settings page, click on 'Plugins'.
Go down to MusicIP and check the box on the left to enable it.
Then click on 'Settings' on the right side of the MusicIP Mixer line.
Set up the MusicIP mix settings to suit your preferences.
Hover the mouse over the 'i' icon to see what each setting means.
Click on 'Apply' to save your settings.

>>> You should now reboot your VortexBox to bring up MusicIP Mixer
>>> and SqueezeBox Server in the correct order.

After the reboot, go to the SqueezeBox Server web page.
You should see an 'M' at the right side of artist, album, and song entries.
Click on the 'M' to create a MusicIP playlist.

To start the MusicIP Server, enter 'service mmserver start'.
To stop the MusicIP Server, enter 'service mmserver stop'.
To see if the MusicIP Server is running, enter 'service mmserver status'.

You can enhance the use of MusicIP mixes on SqueezeBox Server by adding the Spicefly
Sugarcube plugin which lets you create never-ending playlists to explore your
music library. See http://spicefly.com for details.

Enjoy!!!
EOF

cat /opt/MusicMagicMixer/README

echo ""
echo "See /opt/MusicMagicMixer/README to repeat these instructions."
echo ""

# Remove installation files
cleanup
exit 0
