#!/bin/bash

POSTGRESQL_VERSION=9.1beta2
LIBXML_VERSION=2.7.8
UUID_VERSION=1.6.2
JDBCVER=9.1dev-900.jdbc4
BUILDUSER=jokey
SDK=/Developer/SDKs/MacOSX10.6.sdk
OSTARGET=10.6

# ---------------------------

WD=$(pwd)
INSTPREFIX_BASE=/tmp/postgresql
LIBXML_PREFIX=$WD/libxml-install
LIBXML_DOWNLOAD_URL=ftp://xmlsoft.org/libxml2/libxml2-$LIBXML_VERSION.tar.gz
UUID_PREFIX=$WD/ossp-uuid-install
UUID_DOWNLOAD_URL=http://www.mirrorservice.org/sites/ftp.ossp.org/pkg/lib/uuid/uuid-$UUID_VERSION.tar.gz
POSTGRESQL_DOWNLOAD_URL=ftp://ftp9.de.postgresql.org/unix/databases/postgresql/source/v$POSTGRESQL_VERSION/postgresql-$POSTGRESQL_VERSION.tar.gz
PREFIX=/usr/local

[ -e postgresql-$POSTGRESQL_VERSION.tar.gz ] || curl -O $POSTGRESQL_DOWNLOAD_URL || { echo Unable to download $POSTGRESQL_DOWNLOAD_URL; exit; }
rm -rf postgresql-$POSTGRESQL_VERSION
tar -xzf postgresql-$POSTGRESQL_VERSION.tar.gz || exit

# Apple's libxml2 is too old. We build our own and link statically.
if [ ! -e $LIBXML_PREFIX ]; then
   [ -e libxml2-$LIBXML_VERSION.tar.gz ] || curl -O $LIBXML_DOWNLOAD_URL || { echo Unable to download $LIBXML_DOWNLOAD_URL; exit; }
   rm -rf libxml2-$LIBXML_VERSION
   tar -xzf libxml2-$LIBXML_VERSION.tar.gz
   rm -rf $LIBXML_PREFIX

   cd libxml2-$LIBXML_VERSION
   CFLAGS="-O -mmacosx-version-min=$OSTARGET -arch x86_64 -arch i386 -isysroot $SDK" \
   ./configure --disable-shared --enable-static --prefix=$LIBXML_PREFIX \
   --disable-dependency-tracking || exit

   make -j 2 || exit
   make install || exit
fi

if [ ! -e $UUID_PREFIX ]; then
   [ -e uuid-$UUID_VERSION.tar.gz ] || curl -O $UUID_DOWNLOAD_URL || { echo Unable to download $UUID_DOWNLOAD_URL; exit; }
   rm -rf uuid-$UUID_VERSION
   tar -xzf uuid-$UUID_VERSION.tar.gz
   rm -rf $UUID_PREFIX

   cd uuid-$UUID_VERSION
   CFLAGS="-O -mmacosx-version-min=$OSTARGET -arch x86_64 -arch i386 -isysroot $SDK" ./configure \
   --disable-shared --enable-static --prefix=$UUID_PREFIX || exit
   MACOSX_DEPLOYMENT_TARGET=$OSTARGET make -j 2 || exit
   make install || exit
fi


mkdir $INSTPREFIX_BASE
sudo chown -R $BUILDUSER $INSTPREFIX_BASE
rm -rf $INSTPREFIX_BASE/*

INSTPREFIX=$INSTPREFIX_BASE

INSTALLDIRS="prefix=$INSTPREFIX$PREFIX/pgsql \
        exec_prefix=$INSTPREFIX$PREFIX \
        bindir=$INSTPREFIX$PREFIX/bin \
        sbindir=$INSTPREFIX$PREFIX/sbin \
        sysconfdir=$INSTPREFIX$PREFIX/pgsql/etc \
        datarootdir=$INSTPREFIX$PREFIX/pgsql/share \
        includedir=$INSTPREFIX$PREFIX/pgsql/include \
        libdir=$INSTPREFIX$PREFIX/pgsql/lib \
        libexecdir=$INSTPREFIX$PREFIX/pgsql/libexec \
        localstatedir=$INSTPREFIX$PREFIX/pgsql/var \
        sharedstatedir=$INSTPREFIX$PREFIX/pgsql/com \
        mandir=$INSTPREFIX$PREFIX/man \
        infodir=$INSTPREFIX$PREFIX/info"

cd $WD
cd postgresql-$POSTGRESQL_VERSION || exit

MACOSX_DEPLOYMENT_TARGET=$OSTARGET \
PATH=$LIBXML_PREFIX/bin:$UUID_PREFIX/bin:$PATH \
./configure --bindir=$PREFIX/bin --mandir=$PREFIX/man/ \
--with-openssl --with-perl --with-python --with-tcl \
--with-bonjour --with-pam --with-krb5 --with-libxml \
--with-ldap --with-ossp-uuid \
CFLAGS="-O -arch i386 -arch x86_64 -mmacosx-version-min=$OSTARGET -isysroot $SDK" \
LDFLAGS="-arch i386 -arch x86_64 -lz -liconv -ltcl -L$UUID_PREFIX/lib" \
LD="gcc -mmacosx-version-min=$OSTARGET -isysroot $SDK -nostartfiles -arch i386 -arch x86_64" \
CPPFLAGS="-I$UUID_PREFIX/include"

[ $? -eq 0 ] || exit

patch -p0 < ../postgres90beta_int64.patch || exit

make -j 2 || exit
make $INSTALLDIRS install || exit

cd contrib/uuid-ossp
make $INSTALLDIRS install || exit

cd ../../contrib/tsearch2
make $INSTALLDIRS install || exit

cd ../../contrib/pgcrypto
make $INSTALLDIRS install || exit

cd ../../contrib/adminpack
make $INSTALLDIRS install || exit

cd $INSTPREFIX$PREFIX/pgsql/share || exit
mkdir java || exit
cd java || exit
curl -O http://jdbc.postgresql.org/download/postgresql-$JDBCVER.jar || exit
ln -s postgresql-*.jar postgresql.jar

sudo chown -R root:admin /tmp/postgresql

cd $WD
open PostgreSQL.pmdoc