#!/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=/Users/sam/Downloads/tmp/QScintilla_gpl-2.9.3.dev1606101834/Qt4Qt5
QWT=/usr/local/qwt-6.1.2/
QTBIN=/Users/sam/Development/Qt5.6.1/5.6/clang_64/bin
#QTBIN=/Users/sam/Development/Qt5.7/5.7/clang_64/bin
LIBQS=libqscintilla2.12.0.2.dylib
LIBQS_LINK=libqscintilla2.12.dylib
# QSCINTILLA="native/qscintilla"
# QTBIN="native/qt5.5/bin"
#LIBQS=libqscintilla2.12.0.0.dylib
#LIBQS_LINK=libqscintilla2.12.dylib
## ----

QMAKE="$QTBIN/qmake"
LREL="$QTBIN/lrelease"
MACDEPLOYQT="$QTBIN/macdeployqt"
LIBQS_PATH="$QSCINTILLA/$LIBQS"

SP='Sonic Pi.app'

set -eux
cd $(dirname $0)

# Create empty build dir
rm -rf build
mkdir build

# Generate automated docs
../../server/native/osx/ruby/bin/ruby ../../server/bin/i18n-tool.rb -t
cp -f ruby_help.tmpl ruby_help.h
../../server/native/osx/ruby/bin/ruby ../../server/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
mv "$SP" build
cd build

# pull in QScintilla to app
cp $LIBQS_PATH "$SP/Contents/MacOS/"

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

# Put qscintilla dylib in Frameworks dir and symlink
cd "$SP/Contents/MacOS"
# mv ../MacOS/$LIBQS .

cp -R ~/Desktop/qwt.framework .

echo `pwd`
if [ ! -f $LIBQS_LINK ]; then
  ln -s $LIBQS $LIBQS_LINK
fi

# 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 .
