#!/bin/sh

SP_APP_SRC=`pwd`

echo "Note, this script assumes you have already installed SuperCollider 3.7.1 to your Applications folder"
echo "And you have also installed the sc3-plugins."
echo "SC 3.7.1 can be downloaded from https://github.com/supercollider/supercollider/releases"
echo "The plugins can be downloaded from https://github.com/supercollider/sc3-plugins/releases"
echo "Installation instructions are here https://github.com/supercollider/sc3-plugins"
echo "We're working to make this script a one shot solution for all OSX platforms"
echo "Please direct rage and suggestions to Factoid in (https://gitter.im/samaaron/sonic-pi)"

#Install homebrew prerequisites
brew install qt55 boost cmake pkg-config aubio

#Build supercollider 3.8 from source
cd ../../../../
SP_ROOT=`pwd`
git clone https://github.com/supercollider/supercollider.git
cd supercollider
git submodule init && git submodule update
mkdir build
cd build
cmake -DSC_EL=no -DCMAKE_PREFIX_PATH=/usr/local/opt/qt55/lib/cmake ..
make
sudo make install
#This should install to /usr/local/

#Build sc3 plugins and install to /usr/local/ so supercollider 3.8 can find them
cd $SP_ROOT
git clone https://github.com/supercollider/sc3-plugins.git
cd sc3-plugins
git submodule init && git submodule update
cp -r external_libraries/nova-simd/* source/VBAPUGens
mkdir build
cd build
cmake -DSC_PATH=../../supercollider/ -DCMAKE_INSTALL_PREFIX=/usr/local -DCMAKE_BUILD_TYPE=Release ..
make
sudo make install
rsync -arv SC3plugins ../../supercollider/build/editors/sc-ide/SuperCollider.app/Contents/Resources/plugins/

#Download and extract source packages
cd ../../../../
wget 'https://sourceforge.net/projects/pyqt/files/QScintilla2/QScintilla-2.9.2/QScintilla_gpl-2.9.2.tar.gz'
wget 'https://downloads.sourceforge.net/project/qwt/qwt/6.1.2/qwt-6.1.2.tar.bz2'
tar -xf QScintilla_gpl-2.9.2.tar.gz
tar -xf qwt-6.1.2.tar.bz2

#Build qscintilla libraries for Qt5
cd QScintilla_gpl-2.9.2/Qt4Qt5/
/usr/local/opt/qt55/bin/qmake qscintilla.pro
make
sudo make install

#Build qwt libraries for Qt5
cd ../../
cd qwt-6.1.2
/usr/local/opt/qt55/bin/qmake qwt.pro
make
sudo make install
sudo cp /usr/local/qwt-6.1.2/features/* /usr/local/opt/qt55/mkspecs/features/

#Build Sonic-Pi server extensions, documentation, and gui
cd $SP_APP_SRC
../../server/bin/compile-extensions.rb
../../server/bin/i18n-tool.rb -t
cp -f ruby_help.tmpl ruby_help.h
../../server/bin/qt-doc.rb -o ruby_help.h
/usr/local/opt/qt55/bin/lrelease SonicPi.pro 
/usr/local/opt/qt55/bin/qmake SonicPi.pro 
make

### FOR FINAL RELEASE ONLY MOVE THIS INTO package-osx-app ###
#/usr/local/opt/qt55/bin/macdeployqt 'Sonic Pi.app'

#Soft Link extra stuff that the app will need at runtime
cd 'Sonic Pi.app'
ln -f -s ../../../../app/ ./
ln -f -s ../../../../etc/ ./
ln -f -s ../../../../../supercollider/build/editors/sc-ide/SuperCollider.app/Contents/Resources/scsynth ../../../../app/server/native/osx/scsynth
rsync -arv ../../../../../QScintilla_gpl-2.9.2/Qt4Qt5/libqscintilla2.* Contents/MacOS/
rsync -arv ../../../../../qwt-6.1.2/lib/ Contents/MacOS/
install_name_tool -change qwt.framework/Versions/6/qwt @executable_path/qwt.framework/Versions/6/qwt Contents/MacOS/Sonic\ Pi 
install_name_tool -change libqscintilla2.12.dylib @executable_path/libqscintilla2.12.dylib Contents/MacOS/Sonic\ Pi 

### FOR FINAL RELEASE ONLY MOVE THIS INTO package-osx-app ###
#rsync -arv ../../../../app ./
#rsync -arv ../../../../etc ./
#cp -R /Applications/SuperCollider/SuperCollider.app/Contents/Resources/scsynth ./app/server/native/osx/scsynth
