#!/bin/bash

echo In case you are prompted to enter a password, please enter the super user password

if [ ! -d /System/Library/Frameworks/WebKit.framework.bak.tfk ]; then
  sudo ditto /System/Library/Frameworks/WebKit.framework /System/Library/Frameworks/WebKit.framework.bak.tfk;
else
  echo WebKit framework seems to be already installed;
fi

if [ ! -d /System/Library/Frameworks/JavaScriptCore.framework.bak.tfk ]; then
  sudo ditto /System/Library/Frameworks/JavaScriptCore.framework /System/Library/Frameworks/JavaScriptCore.framework.bak.tfk;
else
  echo JavaScriptCore framework seems to be already installed;
fi

if [ ! -d /System/Library/PrivateFrameworks/JavaScriptGlue.framework.bak.tfk ]; then
  sudo ditto /System/Library/PrivateFrameworks/JavaScriptGlue.framework /System/Library/PrivateFrameworks/JavaScriptGlue.framework.bak.tfk;
else
  echo JavaScriptGlue framework seems to be already installed;
fi

CURDIR=$(dirname $0)

sudo ditto $CURDIR/WebKit.app/Contents/Frameworks/10.4/WebKit.framework /System/Library/Frameworks/WebKit.framework
sudo ditto $CURDIR/WebKit.app/Contents/Frameworks/10.4/WebCore.framework /System/Library/Frameworks/WebKit.framework/Versions/A/Frameworks/WebCore.framework
sudo ditto $CURDIR/WebKit.app/Contents/Frameworks/10.4/JavaScriptCore.framework /System/Library/Frameworks/JavaScriptCore.framework
sudo ditto $CURDIR/WebKit.app/Contents/Frameworks/10.4/JavaScriptGlue.framework /System/Library/PrivateFrameworks/JavaScriptGlue.framework

sudo install_name_tool -id /System/Library/Frameworks/WebKit.framework/Versions/A/Frameworks/WebCore.framework/Versions/A/WebCore /System/Library/Frameworks/WebKit.framework/Versions/A/Frameworks/WebCore.framework/Versions/A/WebCore
sudo install_name_tool -id /System/Library/Frameworks/WebKit.framework/Versions/A/WebKit /System/Library/Frameworks/WebKit.framework/Versions/A/WebKit
sudo install_name_tool -id /System/Library/Frameworks/JavaScriptCore.framework/Versions/A/JavaScriptCore /System/Library/Frameworks/JavaScriptCore.framework/Versions/A/JavaScriptCore
sudo install_name_tool -id /System/Library/PrivateFrameworks/JavaScriptGlue.framework/Versions/A/JavaScriptGlue /System/Library/PrivateFrameworks/JavaScriptGlue.framework/Versions/A/JavaScriptGlue

sudo install_name_tool -change /Volumes/Mac_OS_9/WebKit/WebKitBuild/Release/JavaScriptCore.framework/Versions/A/JavaScriptCore /System/Library/PrivateFrameworks/JavaScriptCore.framework/Versions/A/JavaScriptCore /System/Library/Frameworks/WebKit.framework/Versions/A/Frameworks/WebCore.framework/Versions/A/WebCore
sudo install_name_tool -change /Volumes/Mac_OS_9/WebKit/WebKitBuild/Release/JavaScriptCore.framework/Versions/A/JavaScriptCore /System/Library/PrivateFrameworks/JavaScriptCore.framework/Versions/A/JavaScriptCore -change /Volumes/Mac_OS_9/WebKit/WebKitBuild/Release/WebCore.framework/Versions/A/WebCore /System/Library/Frameworks/WebKit.framework/Versions/A/Frameworks/WebCore.framework/Versions/A/WebCore /System/Library/Frameworks/WebKit.framework/Versions/A/WebKit
sudo install_name_tool -change /Volumes/Mac_OS_9/WebKit/WebKitBuild/Release/JavaScriptCore.framework/Versions/A/JavaScriptCore /System/Library/PrivateFrameworks/JavaScriptCore.framework/Versions/A/JavaScriptCore /System/Library/PrivateFrameworks/JavaScriptGlue.framework/Versions/A/JavaScriptGlue

sudo chown -R root:wheel /System/Library/Frameworks/WebKit.framework
sudo chown -R root:wheel /System/Library/Frameworks/JavaScriptCore.framework
sudo chown -R root:wheel /System/Library/PrivateFrameworks/JavaScriptGlue.framework

if [ ! -d /System/Library/Frameworks/WebKit.framework/Frameworks ]; then
  cd /System/Library/Frameworks/WebKit.framework
  sudo ln -s Versions/Current/Frameworks Frameworks
fi

sudo update_prebinding

echo TenFourKit will now be used by any application that was linked against the version of WebKit provided by Apple
