# Pre-Build requirements for El Captian and later users:
#
#     First make sure you are using the latest XCode for your version of Mac OSX
#     Then make sure you have the command line tools (CLT) installed (via xcode-select --install)

#     XCode 7 and 8 users with El Capitan or Later need to download XCode 6 and copy the MacOSX10.10sdk out of it
#     and extract the /usr/include/openssl directory and its contents into /usr/local/include/
#     The openssl library is still shipped with Mac OSX to prevent breakage but its headers have been removed.
#     FYI: A working openssl with headers is required for python's pip3 to work properly as a package installer

#     Also you need to install the latest free community edition of ActiveState's ActiveTCL 8.6.X or later.
#     See: http://www.activestate.com/activetcl/downloads
#
#     OR 
#
#     Build and install the latest 8.6.X versions of Tcl and Tk Frameworks on Mac OSX 
#     Source archives are Available from the Tcl/Tk sourceforge site (latest current version is 8.6.6)
#     https://www.tcl.tk/software/tcltk/download.html
#     https://www.tcl.tk/doc/howto/compile.html  (use --enable-framework --enable-aqua --enable-threads during configure)

#     And finally as we will be building a required PyQt5 python module, you will need to have built Qt 5.6.2 first
#       So see Building_Qt5_From_Source_with_QtWebkit_Added_Back_on_MacOSX.txt

# Before building remember to rename any /Applications/Python 3.5.app to save it and replace it afterwards
# as the damn python installation from source always overwrites it no matter the configure prefix used

# Download Python-3.5.2.tgz from www.python.org

export MACOSX_DEPLOYMENT_TARGET=10.9

# Pick a location where the relocatable Python framework will be installed
# stay away from /tmp locations since Mac OS X will auto delete files older 
# than 3 days in /tmp right out from under you

export MYDEST=/Users/${USER}/devtools/Frameworks

# now build Python 3.5.2 as a framework
# Need to patch Python-3.5.2 to allow it to build Mac OS X 10.9 compliant 
# and for it to find and use the latest Tk and Tcl frameworks
# Look in Sigil's "docs" to get the required patch "python_3.5_fixes3.patch"

tar -zxvf Python-3.5.2.tgz
cd Python-3.5.2
patch -p0 < python_3.5_fixes3.patch


LDFLAGS="-Wl,-macosx_version_min,10.9" CFLAGS="-mmacosx-version-min=10.9 -Werror=partial-availability" ./configure --prefix=${MYDEST} --enable-framework=${MYDEST} --with-ensurepip

or

./configure --prefix=${MYDEST} --enable-framework=${MYDEST} --with-ensurepip

make
sudo make frameworkinstall

# next update path in order to use the newly built/installed Python.framework's
# and then use pip3 to install all other required python packages to its site-packages

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

sudo pip3 install six
sudo pip3 install html5lib
sudo pip3 install lxml
sudo pip3 install Pillow
sudo pip3 install regex
sudo pip3 install cssutils
sudo pip3 install cssselect
sudo pip3 install chardet

# Now a complete Python.framework has been built in ${MYDEST}
# But we still need to make it a relocatable framework

# To make it relocatable we need to use otool and install_name_tool to change
# the dylib name and path to it from all executables in the Python.framework

# A Quick Guide: On Mac OS X, one may use:
#     "otool -D <file>" to view the install name of a dylib
#     "otool -L <file>" to view the dependencies
#     "otool -l <file> | grep LC_RPATH -A2" to view the RPATHs
#     "install_name_tool -id ..." to change an install name
#     "install_name_tool -change ..." to change the dependencies
#     "install_name_tool -rpath ... -add_rpath ... -delete_rpath ..." to change RPATHs
 
# Make the framework's main dylib relocatable using rpath

cd ${MYDEST}/Python.framework/Versions/3.5/
sudo chmod u+w Python
otool -D ./Python
sudo install_name_tool -id @rpath/Python ./Python

# Change the dependencies of the executable files in bin to point to the relocatable 
# framework in a relative way and add the proper rpath to find the Python (renamed dylib)

cd bin
sudo install_name_tool -change ${MYDEST}/Python.framework/Versions/3.5/Python @rpath/Python python3.5
sudo install_name_tool -change ${MYDEST}/Python.framework/Versions/3.5/Python @rpath/Python python3.5m
sudo install_name_tool -add_rpath @executable_path/../ ./python3.5

# now do the same for the Python.app stored inside the Python.framework Resources 
# This app is needed to allow gui use by python for plugins

cd ${MYDEST}/Python.framework/Versions/3.5/Resources/Python.app/Contents/MacOS
sudo install_name_tool -change ${MYDEST}/Python.framework/Versions/3.5/Python @rpath/Python ./Python
sudo install_name_tool -add_rpath @executable_path/../../../../ ./Python

# We should now have a fully relocatable Python.framework

# We will now use this just-built Python3.5 interpreter to install PyQt5 and sip.

# **** Important versions of PyQt5 and sip must be selected to match with the Qt version
# **** In these instructions we are using Qt 5.6.2 and therefore need sip-4.18.1 and PyQt5_gpl-5.6


# Building sip-4.18.1 from source and installing it into your Pythn Interpreter

# From https://sourceforge.net/projects/pyqt/files/sip/
# Download sip-4.18.1.tar.gz

# More detailed build instructions can be found here:
#    http://pyqt.sourceforge.net/Docs/sip4/installation.html

# Must have the python interpreter you want to install PyQt5 into in the PATH to be found first
export PATH=${MYDEST}/Python.framework/Versions/3.5/bin:${PATH}
which python3

tar -zxvf sip-4.18.1.tar.gz
cd sip-4.18.1
python3 ./configure.py --deployment-target=10.9
make
sudo make install


# Building PyQt5 for Qt-5.6.2 from source and installing it into your Python Interpreter

# From https://sourceforge.net/projects/pyqt/files/PyQt5/
# Download PyQt5_gpl-5.6.tar.gz

# More detailed build instructions can be found here: 
#    http://pyqt.sourceforge.net/Docs/PyQt5/installation.html#building-and-installing-from-source

# Must have the python interpreter you want to install PyQt5 into in the path to be found first
export PATH=${MYDEST}/Python.framework/Versions/3.5/bin:${PATH}
which python3

# Must have the Qt5.6.2 bin directory in the path to specify which Qt to use
# See Building_Qt5_From_Source_with_QtWebkit_Added_Back_on_MacOSX.txt
export PATH=/Users/${USER}/Qt56/bin:${PATH}
which qmake

export MACOSX_DEPLOYMENT_TARGET=10.9

tar -zxvf PyQt5_gpl-5.6.tar.gz
cd PyQt5_gpl-5.6
python3 ./configure.py --confirm-license --no-docstrings --no-stubs 
make
sudo make install


# Once complete you will have properly built a Python 3.5 interpreter to be embedded inside
# of Sigil
