#!/bin/bash

# SqueezePlay AutoBuilder v1.09 (14th June 2012) by Andy Davison

SQPGIT="git://github.com/Logitech/squeezeplay.git"
SQPBRANCH="7.7"
PATCHVER="3"

if [ "$1" = "log" ]; then
	LOCATION=`dirname $0`
	echo -n "Writing development log to $LOCATION/trunk-$SQPVER.log..."
	svn log http://svn.slimdevices.com/repos/jive/$SQPVER/trunk/squeezeplay > $LOCATION/trunk-$SQPVER.log
	echo " done."
	exit 0
fi

[ -d ~/Desktop/sqpbuild ] || mkdir -p ~/Desktop/sqpbuild
cd ~/Desktop/sqpbuild

echo

if [ -d squeezeplay ]; then
	echo "Urk. I don't know what to do when there's already a squeezeplay directory."
	echo "I'm going to stop to avoid damage."
	exit 0
	#echo "Rolling with it..."
else
	echo "Grabbing branch $SQPBRANCH from repo..."
	echo
	sleep 3
	mkdir squeezeplay
	cd squeezeplay
	git init
	git remote add -t $SQPBRANCH -f origin $SQPGIT
	git checkout $SQPBRANCH
fi

echo "Grabbing SqueezePlay v$PATCHVER patches..."
echo
sleep 10
[ ! -f squeezeplay$PATCHVER.patch ] || rm squeezeplay$PATCHVER.patch*
wget http://birdslikewires.co.uk/download/joggler/squeezeplay/compile/squeezeplay$PATCHVER.patch
patch -N -p1 -d squeezeplay < squeezeplay$PATCHVER.patch
# This would build Spotify components, but they're closed source and not available.
#sed -i "s/SPPRIVATE=no/SPPRIVATE=yes/g" squeezeplay/src/Makefile.linux
echo

echo "Grabbing PortAudio source..."
echo
sleep 5
cd ~/Desktop/sqpbuild/squeezeplay/src
#svn co -r 1625 https://www.portaudio.com/repos/portaudio/trunk
svn co https://subversion.assembla.com/svn/portaudio/portaudio/trunk
rm -rf portaudio_v19_1360
mv trunk portaudio_v19_1360
echo

echo "Let's build!"
echo
sleep 5
make -f Makefile.linux
echo

echo "Take a copy of the build and thin it down for distribution..."
echo
sleep 5
mkdir ~/Desktop/squeezeplay
cd ~/Desktop/squeezeplay

SQPTGZ=`ls -1 ~/Desktop/sqpbuild/squeezeplay/build/ | grep -m1 squeezeplay-7 | grep -m1 .tgz`
VERSION=`echo $SQPTGZ | awk -F\- '{print $2}'`
BUILD=`echo $SQPTGZ | awk -F\: '{print $2}' | cut -b 1-4`

cp ~/Desktop/sqpbuild/squeezeplay/build/"$SQPTGZ" ~/Desktop/squeezeplay/

tar zxvf ~/Desktop/squeezeplay/"$SQPTGZ"
rm *.tgz

rm -rvf ~/Desktop/squeezeplay/bin/freetype-config
rm -rvf ~/Desktop/squeezeplay/bin/gmon.out
rm -rvf ~/Desktop/squeezeplay/bin/lua
rm -rvf ~/Desktop/squeezeplay/bin/luac
rm -rvf ~/Desktop/squeezeplay/bin/sdl-config
rm -rvf ~/Desktop/squeezeplay/include
rm -rvf ~/Desktop/squeezeplay/lib/*.a
rm -rvf ~/Desktop/squeezeplay/lib/*.la
rm -rvf ~/Desktop/squeezeplay/share/jive/applets/Bounce
rm -rvf ~/Desktop/squeezeplay/share/jive/applets/QVGAlandscapeSkin
rm -rvf ~/Desktop/squeezeplay/share/jive/applets/QVGAportraitSkin
rm -rvf ~/Desktop/squeezeplay/share/jive/applets/SetupWallpaper/wallpaper/*
rm -rvf ~/Desktop/squeezeplay/share/jive/applets/Test
rm -rvf ~/Desktop/squeezeplay/share/jive/applets/TestTones
rm -rvf ~/Desktop/squeezeplay/share/jive/applets/WQVGAlargeSkin
rm -rvf ~/Desktop/squeezeplay/share/jive/applets/WQVGAsmallSkin
sleep 4

echo "Make a tarball of the whole thing..."
echo
sleep 5
cd ~/Desktop/
tar zcvf squeezeplay-thinned-$VERSION-$BUILD.tgz squeezeplay
rm -rf ~/Desktop/squeezeplay

echo "Look! A shiny new tarball on your desktop!"










