#!/bin/bash

# SqueezePlay AutoBuilder v1.10 (28th January 2014) by Andy Davison

SQPGIT="git://github.com/Logitech/squeezeplay.git"
SQPBRANCH="public/7.7"
PATCHVER="5"
BUILDLOC="/home/$USER/Desktop/sqpbuild"
OPENFRAMEBUILD="$BUILDLOC/squeezeplay/build/openframe"

countdown() {
  local i
  echo -n $1
  sleep 1
  for ((i=$1-1; i>=1; i--)); do
    printf "\b%d" $i
    sleep 1
  done
  printf "\b\b\b\bnow..."
}

if [ $# -eq 0 ] || [ ${#1} -ne 4 ]; then
  echo "Usage: $0 <revision>"
  echo
  echo "revision: A four digit revision code is required. Our builds started at 9710."
  exit 0
else
  SQPREVISION="$1"
fi


## Setup build location on the Desktop.
if [ -d $BUILDLOC ]; then
  BUILDEXISTS=1
  echo
  echo "Build location $BUILDLOC already exists!"
  echo
  echo -n "Press CTRL-C to exit, or we'll use it and continue in "
  countdown 5
  sleep 1
  echo -n " "
  rm -rf $BUILDLOC
  sleep 1
else
  BUILDEXISTS=0
fi
echo
sleep 1
[ ! -d $BUILDLOC ] && mkdir $BUILDLOC
cd $BUILDLOC

if [ $BUILDEXISTS -eq 1 ]; then
  rm -rf $OPENFRAMEBUILD
	make clean
else
	echo "Downloading branch $SQPBRANCH from $SQPGIT..."
	echo
	git clone -b $SQPBRANCH --single-branch $SQPGIT
  echo
fi


## Download the patch file.
echo "Downloading SqueezePlay v$PATCHVER patch..."
echo
sleep 1
[ -f squeezeplay_v*.patch ] && rm squeezeplay_v*.patch
wget http://birdslikewires.co.uk/download/joggler/squeezeplay/compile/squeezeplay_v$PATCHVER.patch
patch -p1 -d squeezeplay < squeezeplay_v$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


## Update the PortAudio sources.
echo "Downloading PortAudio source..."
echo
sleep 1
cd $BUILDLOC/squeezeplay/src
svn co https://subversion.assembla.com/svn/portaudio/portaudio/trunk
rm -rf portaudio_v19_1360
mv trunk portaudio_v19_1360
echo


## Write in our revision number and begin the build.
echo $SQPREVISION > $BUILDLOC/squeezeplay/src/squeezeplay.revision
echo "Commencing build..."
echo
sleep 1
make -f Makefile.linux
echo


## Prepare what we've built.
echo "Copying build and thinning for distribution..."
echo
sleep 1

mkdir -p $OPENFRAMEBUILD
cd $OPENFRAMEBUILD

VERSION=`cat $BUILDLOC/squeezeplay/src/squeezeplay.version`
REVISION=`cat $BUILDLOC/squeezeplay/src/squeezeplay.revision`

cp -R "$BUILDLOC/squeezeplay/build/linux" "$OPENFRAMEBUILD/squeezeplay"

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

echo
echo "Compressing thinned distribution version..."
echo
sleep 5
tar zcvf $OPENFRAMEBUILD/squeezeplay-thinned-$VERSION-$REVISION.tgz squeezeplay

PORTAUDIOREV=`svnversion -n $BUILDLOC/squeezeplay/src/portaudio_v19_1360`
echo "Compiled as SqueezePlay v$VERSION r$REVISION using PortAudio r$PORTAUDIOREV"

exit 0








