# 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)
#
# *** IMPORTANT ***
#
#     For Python's pip3 and lzma module you will need to compile and install the headers 
#     and static library versions of liblzma.a (xz), libssl.a (libressl), and libcrypto.a (libressl)
#
#     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.11.1 first
#       So see Building_Qt5_From_Source_with_QtWebkit_Added_Back_on_MacOSX.txt

# Before building remember to rename any /Applications/Python 3.7.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.7.0.tgz from www.python.org

export MACOSX_DEPLOYMENT_TARGET=10.11

# 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.7.0 as a framework
# Need to patch Python-3.7.0 to allow it to build Mac OS X 10.11 compliant and later
# 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.7_fixes.patch"

tar -zxvf Python-3.7.0.tgz
cd Python-3.7.0
patch -p1 < python_3.7_fixes.patch

export LDFLAGS="-Wl,-macosx_version_min,10.11" 
export CFLAGS="-mmacosx-version-min=10.11 -Werror=partial-availability" 
./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.7/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

# Note: and for the upcoming release you will want to replace
    sudo pip3 install cssutils
with
    sudo pip3 install css-parser

which is a drop in replacement for cssutils with many bugs fixed.

# 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.7/
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.7/Python @rpath/Python python3.7
sudo install_name_tool -change ${MYDEST}/Python.framework/Versions/3.7/Python @rpath/Python python3.7m
sudo install_name_tool -add_rpath @executable_path/../ ./python3.7
# so that python3 can find the Qt Frameworks and proper plugins for PyQt5
sudo install_name_tool -add_rpath @executable_path/../../../../ ./python3.7

# 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.7/Resources/Python.app/Contents/MacOS
sudo install_name_tool -change ${MYDEST}/Python.framework/Versions/3.7/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.7 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.11.2 and therefore need sip-4.19.13 and PyQt5_gpl-5.11.3


# Building sip-4.19.13 from source and installing it into your Python Interpreter

# From https://sourceforge.net/projects/pyqt/files/sip/
# Download sip-4.19.13.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.7/bin:${PATH}
which python3

tar -zxvf sip-4.19.13.tar.gz
cd sip-4.19.13
python3 ./configure.py --deployment-target=10.11 --sip-module=PyQt5.sip
make
sudo make install


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

# From https://sourceforge.net/projects/pyqt/files/PyQt5/
# Download PyQt5_gpl-5.11.3.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.7/bin:${PATH}
which python3

# Must have the Qt5.11.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}/Qt511/bin:${PATH}
which qmake

export MACOSX_DEPLOYMENT_TARGET=10.11

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


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