# Pre-Build requirements for El Captian and later users:
#
#  XCode 7 users with El Capitan or Later need to download XCode 6 and copy the MacOSX10.10sdk out of it
#     and put it into the right place inside XCode.app in order to get the now missing openssl headers
#
#     Or extract the /usr/include/openssl directory and contents from the MacOSX101.10.sdk and install it into /usr/local/include 
#
#     Or download and build and install openssl-1.0.2h (or later) and install it into /usr/local and be sure it is found first
#     as this version is incompatible with the openssl dylib that is still installed even on Mac OS X 10.11
#     You will then need to include your openssl dylibs with your build of Sigil
#
#  A working openssl with headers is required for python's pip3 to work properly as a package installer


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

# Also make sure you have the latest XCode and Command Line Tools Installed

# Download Python-3.4.4.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 deelete files older than 3 days in /tmp right out from under you

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

# now build Python 3.4.4 as a framework
# Need to patch Python-3.4.4.tgz to allow it to build Mac OS X 10.9 compliant 
# build under Mac OS X 10.10

tar -zxvf Python-3.4.4.tgz
cd Python-3.4.4
patch -p0 < python_configure_fix.patch

./configure --prefix=${MYDEST} --enable-framework=${MYDEST} --with-ensurepip MACOSX_DEPLOYMENT_TARGET=10.9
make
sudo make frameworkinstall

# next update path in order to use the newly built/installed Python.framework's

# use pip3 to install all other required python packages to its site-packages

export PATH=${MYDEST}/Python.framework/Versions/3.4/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.4/
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.4/Python @rpath/Python python3.4
sudo install_name_tool -change ${MYDEST}/Python.framework/Versions/3.4/Python @rpath/Python python3.4m
sudo install_name_tool -add_rpath @executable_path/../ ./python3.4

# 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.4/Resources/Python.app/Contents/MacOS
sudo install_name_tool -change ${MYDEST}/Python.framework/Versions/3.4/Python @rpath/Python ./Python
sudo install_name_tool -add_rpath @executable_path/../../../../ ./Python

# We should now have a fully relocatable Python.framework ready to use to build and 
# bundle into Sigil

