#!/bin/bash

#--
# This file is part of Sonic Pi: http://sonic-pi.net
# Full project source: https://github.com/samaaron/sonic-pi
# License: https://github.com/samaaron/sonic-pi/blob/master/LICENSE.md
#
# Copyright 2013, 2014, 2015 by Sam Aaron (http://sam.aaron.name).
# All rights reserved.
#
# Permission is granted for use, copying, modification, and
# distribution of modified versions of this work as long as this
# notice is included.
#++

## ---- Modify these to point to your system
QSCINTILLA_QT4QT5_DIR=/Users/sam/Development/tmp/QScintilla_gpl-2.10.2/Qt4Qt5
QSCINTILLA_LIB=libqscintilla2_qt5.13.1.0.dylib
QWT_DIR=/Users/sam/Development/tmp/qwt-6.1.3
QT_DIR=/Users/sam/Qt/5.7.1/5.7
## ----


QT_BIN="$QT_DIR/clang_64/bin"
QMAKE="$QT_BIN/qmake"
LREL="$QT_BIN/lrelease"
MACDEPLOYQT="$QT_BIN/macdeployqt"
MACCHANGEQT="$QT_BIN/macchangeqt"
LIBQS_PATH="$QSCINTILLA_QT4QT5_DIR/$QSCINTILLA_LIB"
SP_APP='Sonic Pi.app'

set -eux
cd $(dirname $0)

# Generate automated docs
../../server/native/ruby/bin/ruby ../../server/ruby/bin/i18n-tool.rb -t
cp -f ruby_help.tmpl ruby_help.h
../../server/native/ruby/bin/ruby ../../server/ruby/bin/qt-doc.rb -o ruby_help.h

# Generate i18n files
"$LREL" SonicPi.pro

# Build app
"$QMAKE" -o Makefile SonicPi.pro
make

# Move to build

rm -rf build
mkdir build
mv "$SP_APP" build/
cd build

# Make stand-alone Qt Mac App
# Pulls in Qt internally and
# futzes with shared lib paths
$MACDEPLOYQT "$SP_APP"
$MACCHANGEQT "$SP_APP" "$QT_DIR"

# Pull in QScintilla to app
cp $LIBQS_PATH "$SP_APP/Contents/MacOS/"
cd "$SP_APP/Contents/MacOS"
cp -R "$QWT_DIR/lib/qwt.framework" .
mv libqscintilla2* libqscintilla2_qt5.dylib


install_name_tool -change libqscintilla2_qt5.13.dylib @executable_path/libqscintilla2_qt5.dylib Sonic\ Pi
install_name_tool -change qwt.framework/Versions/6/qwt @executable_path/qwt.framework/Versions/6/qwt  Sonic\ Pi

# symlink server
# For final release, these need to be mv'd
# rather than symlinked
cd ../../
ln -s ../../../../../app .
ln -s ../../../../../etc .
ln -s ../../../../../app/server .
