#!/bin/sh
#
# patch_idle_issue_15853.sh
# rev 0: 2012-12-26
#
# Patches lib/idlelib/configDialog.py for use with
# Mac OS X 10.6+ and Tcl/Tk 8.5.13+ to prevent
# crashes when opening IDLE Preferences.
#
# This patcher applies only to Mac OS X framework installs of the
# following releases downloaded from python.org:
#
#   Python 2.7.3
#   Python 3.2.3
#   Python 3.3.0
#
# These fixes will be included in subsequent releases of Python.
# You only need to run this patcher if you have installed a local version
# of Tcl/Tk 8.5.13 or later (such as from ActiveState) on Mac OS X.
#
# This patcher uses the "sudo" command to ensure the necessary privileges
# to patch the file; on most systems, you will need to run this script
# from an administrator account.
#
# To use:
#   1. Download this file from the python.org tracker.
#   2. In a terminal session shell, run:
#        sh /path/to/patch_idle_issue_15853.sh
#      replacing "/path/to" with the folder where the file was downloaded,
#      for example, "~/Downloads"
#   3. If prompted for password, enter your password.
#
# See http://www.python.org/download/mac/tcltk/
# and http://bugs.python.org/issue15853 for more details and latest version.
#
set -e
BASENAME=configDialog
FILENAME="${BASENAME}.py"
URLBASE="http://hg.python.org/cpython/raw-file"
set -- 2.7 3.2 3.3
for pyver
do
    case ${pyver} in
        2.7) release_cksum=c550e1d23e3b665a63eceb5354e832cdd912c07b 
             changeset_id="d089b8fb0f56"
             patch_cksum=0967fbc613f5a41a000c09c62c80c48035ae2bd2
             ;;
        3.2) release_cksum=6ea38c21e0e203eb8d4620675b33415509b870d9 
             changeset_id="a3d755eaf5fa"
             patch_cksum=aac9daa2c7bc061b0a54655f07c14077e46ed8ed
             ;;
        3.3) release_cksum=d6ca1c1d341000bf65677ae84abe9df694994ce8
             changeset_id="36e7da8d3059"
             patch_cksum=aac9daa2c7bc061b0a54655f07c14077e46ed8ed
             ;;
    esac
    fwlib="/Library/Frameworks/Python.framework/Versions/${pyver}/lib/python${pyver}/idlelib"
    if [ ! -e "${fwlib}/${FILENAME}" ]
    then
        echo " -- ${pyver} not found - skipped"
        continue
    fi
    if /usr/bin/openssl sha1 "${fwlib}/${FILENAME}" | /usr/bin/grep -q ${patch_cksum}
    then
        echo " -- ${pyver} file is already patched"
        continue
    fi
    if ! /usr/bin/openssl sha1 "${fwlib}/${FILENAME}" | /usr/bin/grep -q ${release_cksum}
    then
        echo "ERROR: incorrect checksum for existing ${pyver} file - skipped"
        continue
    fi
    curl -fsSO "${URLBASE}/${changeset_id}/Lib/idlelib/${FILENAME}"
    if ! /usr/bin/openssl sha1 ./${FILENAME} | /usr/bin/grep -q ${patch_cksum}
    then
        echo "ERROR: incorrect checksum for downloaded ${pyver} file - skipped"
        continue
    fi
    sudo cp -p "${fwlib}/${FILENAME}" "${fwlib}/${FILENAME}".release
    sudo cp ./${FILENAME} "${fwlib}/${FILENAME}"
    sudo rm -f "${fwlib}/${FILENAME}c" "${fwlib}/${FILENAME}o"
    sudo rm -f "${fwlib}/__pycache__/"${BASENAME}.cpython*
    echo " -- patched ${fwlib}/${FILENAME}"
done