# FIRST:  See Building_A_Relocatable_Python_Framework_on_MacOSX.txt
#
# SECOND: See Building_Qt5_From_Source_with_QtWebkit_Added_Back_on_MacOSX.txt
#
#         Also make sure you have the latest XCode and ** Command Line tools** Installed
#
#         You will need to have CMake 3.0 or later installed and in your path
#
#         If you want to use official stock Qt then Make sure you have installed Qt 5.4.2 
#         or Qt 5.5.1 as you can not use stock Qt 5.6 as Qt has removed QtWebkit in Qt 5.6.
#
#         Alternatively, you can follow the Qt build instructions to add QtWebKit back 
#         to Qt 5.6.X and build Qt yourself directly from source.  This is what the official
#         Sigil builds now do.

# XCode 7 users may want to install the MacOSX 10.10sdk from XCode 6.3 as 10.11sdk has removed openssl headers

# standard deployment target
export MACOSX_DEPLOYMENT_TARGET=10.9

# Set the location of where the relocatable Python framework was installed
# See "Building_A_Relocatable_Python_Framework_on_MacOSX.txt

# Remember to stay away from /tmp locations as Mac OSX has a daemon that will delete 
# files in tmp older than 3 days right out from underneath the user
export MYDEST=/Users/$USER/devtools/Frameworks

# Set the location of where Qt is installed

# if you are using stock Qt 5.4.2 build use the next line instead
# export MYQTHOME=~/Qt/5.4/clang_64

export MYQTHOME=~/Qt56

# Set the location of where you want your Sigil repo to be stored
export MYSIGILREPO=~/repo

# clone the Sigil git repo
cd ${MYSIGILREPO}
git clone https://github.com/Sigil-Ebook/Sigil.git

# Add Qt to the path
export PATH=${PATH}:${MYQTHOME}/bin

# Make sure the newly created relocatable python framework is found first
# **before** any system version of Python3

export PATH=${MYDEST}/Python.framework/Versions/3.5/bin:${PATH}

mkdir build
cd build

# Mac OSX Yosemite users with XCode 6.3 should use the following cmake command:

cmake -DPKG_SYSTEM_PYTHON=1 \
      -DCMAKE_OSX_DEPLOYMENT_TARGET=10.9 \
      -DCMAKE_BUILD_TYPE=Release \
      -DCMAKE_PREFIX_PATH=${MYQTHOME}/lib/cmake \
      -DCMAKE_OSX_SYSROOT=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/ \
      ../Sigil

# Mac OSX El Capitan and later users with XCode 7 who have copied in the MacOSX10.10sdk, should use the following cmake command:
cmake -DPKG_SYSTEM_PYTHON=1 \
      -DCMAKE_OSX_DEPLOYMENT_TARGET=10.9 \
      -DCMAKE_BUILD_TYPE=Release \
      -DCMAKE_CXX_FLAGS=-Wno-inconsistent-missing-override \
      -DCMAKE_PREFIX_PATH=${MYQTHOME}/lib/cmake \
      -DCMAKE_OSX_SYSROOT=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/ \
      ../Sigil

# Mac OSX El Capitan and later users with XCode 7 who have NOT copied in the MacOSX10.10sdk should use the following cmake command:
cmake -DPKG_SYSTEM_PYTHON=1 \
      -DCMAKE_OSX_DEPLOYMENT_TARGET=10.9 \
      -DCMAKE_BUILD_TYPE=Release \
      -DCMAKE_CXX_FLAGS=-Wno-inconsistent-missing-override \
      -DCMAKE_PREFIX_PATH=${MYQTHOME}/lib/cmake \
      -DCMAKE_OSX_SYSROOT=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/ \
      ../Sigil

# After cmake completes
make
make addframeworks

# In ${MYSIGILREPO}/build/bin you will find the newly built Sigil.app

# To test if the newly bundled python 3 version of Sigil is working properly ypou can do the following:

1. download testplugin_v013.zip from https://github.com/Sigil-Ebook/Sigil/tree/master/docs
2. open Sigil.app to the normal nearly blank template epub it generates when opened
3. use Plugins->Manage Plugins menu and make sure the "Use Bundled Python" checkbox is checked
4. use the "Add Plugin" button to navigate to and add testplugin.zip and then hit "Okay" to exit the Manage Plugins Dialog
5. use Plugins->Edit->testplugins to launch the plugin and hit the "Start" button to run it
6. check the plugin output window for your missing or broken plugin test results
