#!/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-2.11.4/Qt4Qt5
QSCINTILLA_LIB=libqscintilla2_qt5.15.dylib
QT_DIR=/Users/sam/Qt/5.14.1/
## ----

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 utils/ruby_help.tmpl utils/ruby_help.h
../../server/native/ruby/bin/ruby ../../server/ruby/bin/qt-doc.rb -o utils/ruby_help.h

# Generate i18n files
"$LREL" SonicPi.pro

# ensure kiss_fft is visible to the source
ln -s -f external/kiss_fft kiss_fft

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

# Move to build

rm -rf build
mkdir build
cp -R "$SP_APP" build/
cd build

# Make stand-alone Qt Mac App
# Pulls in Qt internally and
# futzes with shared lib paths
$MACDEPLOYQT "$SP_APP" "$QT_DIR"
$MACCHANGEQT Sonic\ Pi.app $QT_DIR/clang_64/

# Pull in QScintilla to app
cp $LIBQS_PATH "$SP_APP/Contents/Frameworks/"

cd "$SP_APP/Contents/MacOS"

rm -rf *.dylib


cd ../Resources

ln -s ../../../../../../../app .
ln -s ../../../../../../server .
ln -s ../../../../../../../etc .
