--- /dev/null
+#!/bin/bash
+
+set -e
+
+die () {
+ echo "Error: $1" >&2
+ exit 1
+}
+
+if ! [ -d installer-scripts ]
+then
+ die "Doesn't look like you are running this script from the right location"
+fi
+
+if [ "$#" -ne 1 ]
+then
+ die "Must be given on argument (the bindist)"
+fi
+
+BINDIST="$1"
+
+GHC_UNPACKS_INTO=`echo "$BINDIST" | sed 's/^\(.*\/\)\{0,1\}\(ghc-\([0-9]\{1,\}\)\.\([0-9]\{1,\}\)\.\([0-9]\{1,\}\)\(\.\([0-9]\{1,\}\)\)\{0,1\}\)-\([a-z0-9_]\{1,\}\)-apple-darwin\.tar\.bz2$/\2/'`
+GHC_VERSION_DIG1=`echo "$BINDIST" | sed 's/^\(.*\/\)\{0,1\}\(ghc-\([0-9]\{1,\}\)\.\([0-9]\{1,\}\)\.\([0-9]\{1,\}\)\(\.\([0-9]\{1,\}\)\)\{0,1\}\)-\([a-z0-9_]\{1,\}\)-apple-darwin\.tar\.bz2$/\3/'`
+GHC_VERSION_DIG2=`echo "$BINDIST" | sed 's/^\(.*\/\)\{0,1\}\(ghc-\([0-9]\{1,\}\)\.\([0-9]\{1,\}\)\.\([0-9]\{1,\}\)\(\.\([0-9]\{1,\}\)\)\{0,1\}\)-\([a-z0-9_]\{1,\}\)-apple-darwin\.tar\.bz2$/\4/'`
+GHC_VERSION_DIG3=`echo "$BINDIST" | sed 's/^\(.*\/\)\{0,1\}\(ghc-\([0-9]\{1,\}\)\.\([0-9]\{1,\}\)\.\([0-9]\{1,\}\)\(\.\([0-9]\{1,\}\)\)\{0,1\}\)-\([a-z0-9_]\{1,\}\)-apple-darwin\.tar\.bz2$/\5/'`
+GHC_VERSION_DIG4=`echo "$BINDIST" | sed 's/^\(.*\/\)\{0,1\}\(ghc-\([0-9]\{1,\}\)\.\([0-9]\{1,\}\)\.\([0-9]\{1,\}\)\(\.\([0-9]\{1,\}\)\)\{0,1\}\)-\([a-z0-9_]\{1,\}\)-apple-darwin\.tar\.bz2$/\7/'`
+GHC_ARCHITECTURE=`echo "$BINDIST" | sed 's/^\(.*\/\)\{0,1\}\(ghc-\([0-9]\{1,\}\)\.\([0-9]\{1,\}\)\.\([0-9]\{1,\}\)\(\.\([0-9]\{1,\}\)\)\{0,1\}\)-\([a-z0-9_]\{1,\}\)-apple-darwin\.tar\.bz2$/\8/'`
+
+if [ "$GHC_ARCHITECTURE" == "$BINDIST" ]
+then
+ die "Bindist filename not in expected format"
+fi
+
+if [ "$(($GHC_VERSION_DIG2 % 2))" -eq 1 ]
+then
+ if [ "$GHC_VERSION_DIG4" == "" ]
+ then
+ FRAMEWORK_VERSION="$GHC_VERSION_DIG1.$GHC_VERSION_DIG2.$GHC_VERSION_DIG3-$GHC_ARCHITECTURE"
+ CURRENT_PROJECT_VERSION=`printf "%02d%02d00.%08d\n" "$GHC_VERSION_DIG1" "$GHC_VERSION_DIG2" "$GHC_VERSION_DIG3"`
+ else
+ die "Huh? 4 component HEAD version?"
+ fi
+else
+ if [ "$GHC_VERSION_DIG4" == "" ]
+ then
+ FRAMEWORK_VERSION="$GHC_VERSION_DIG1.$GHC_VERSION_DIG2.$GHC_VERSION_DIG3-$GHC_ARCHITECTURE"
+ CURRENT_PROJECT_VERSION=`printf "%02d%02d%02d\n" "$GHC_VERSION_DIG1" "$GHC_VERSION_DIG2" "$GHC_VERSION_DIG3"`
+ else
+ FRAMEWORK_VERSION="$GHC_VERSION_DIG1.$GHC_VERSION_DIG2.$GHC_VERSION_DIG3.$GHC_VERSION_DIG4-$GHC_ARCHITECTURE"
+ CURRENT_PROJECT_VERSION=`printf "%02d%02d%02d.%08d\n" "$GHC_VERSION_DIG1" "$GHC_VERSION_DIG2" "$GHC_VERSION_DIG3" "$GHC_VERSION_DIG4"`
+ fi
+fi
+
+# The user-visible CFBundleShortVersionString
+SHORT_VERSION_STRING="$FRAMEWORK_VERSION"
+
+# Name of the installer package
+PACKAGE_NAME="GHC-$FRAMEWORK_VERSION.pkg"
+
+# Xcode's installation build product location (this is where the GHC.framework
+# is assembled)
+DSTROOT="/tmp/GHC.dst"
+
+# Tools directory for a system volume install
+TOOLS_SYSTEM="$DSTROOT/Library/Frameworks/GHC.framework/Versions/$FRAMEWORK_VERSION/Tools"
+
+rm -rf "$DSTROOT"
+mkdir -p "$TOOLS_SYSTEM"
+sed "s/@FRAMEWORK_VERSION@/$FRAMEWORK_VERSION/" \
+ < installer-scripts/Uninstaller.in \
+ > installer-scripts/Uninstaller
+sed "s/@FRAMEWORK_VERSION@/$FRAMEWORK_VERSION/" \
+ < installer-scripts/create-links.in \
+ > installer-scripts/create-links
+sed "s/@FRAMEWORK_VERSION@/$FRAMEWORK_VERSION/" \
+ < GHC-system.pmdoc/index.xml.in \
+ > GHC-system.pmdoc/index.xml
+cp installer-scripts/Uninstaller installer-scripts/create-links "$TOOLS_SYSTEM"
+xcodebuild -target GHC-systemwide clean build install \
+ BINDIST="$BINDIST" \
+ GHC_UNPACKS_INTO="$GHC_UNPACKS_INTO" \
+ CURRENT_PROJECT_VERSION="$CURRENT_PROJECT_VERSION" \
+ SHORT_VERSION_STRING="$SHORT_VERSION_STRING" \
+ FRAMEWORK_VERSION="$FRAMEWORK_VERSION" \
+ CURRENT_LIBRARY_VERSION="$FRAMEWORK_VERSION" \
+ COMMAND_MODE=unix2003
+rm -f GHC-system.pmdoc/*-contents.xml
+
+/Developer/usr/bin/packagemaker \
+ -v --doc GHC-system.pmdoc \
+ -o $PACKAGE_NAME \
+ -i org.haskell.ghc."$FRAMEWORK_VERSION"
+
+# Clean up
+xcodebuild -target GHC-systemwide clean \
+ CURRENT_PROJECT_VERSION="$CURRENT_PROJECT_VERSION" \
+ SHORT_VERSION_STRING="$SHORT_VERSION_STRING" \
+ FRAMEWORK_VERSION="$FRAMEWORK_VERSION" \
+ CURRENT_LIBRARY_VERSION="$FRAMEWORK_VERSION" \
+ COMMAND_MODE=unix2003
+rm -r "$GHC_UNPACKS_INTO"
+rm -r "$DSTROOT"
+rm installer-scripts/Uninstaller installer-scripts/create-links
+rm GHC-system.pmdoc/index.xml
+