#!/bin/sh

# This builds a PPC and an i386 version of the PostgreSQL base distribution. Only modify these
# two variables below: the first one is the final output directory and the second one is the
# directory in which the PostgreSQL source is located.

# SVN $Id: build-pgsql-uni.sh 528 2006-12-30 10:46:39Z rsesek $

# The final output path where you want to store everything. This should be an absolute path and should not end in a slash!
OUTPUT="/Library/TindexPgSql"

# The location of the PostgreSQL base distribution source. This should be an absolute path.
PGSRC="/Users/rsesek/Projects/Tindex-2/postgresql-8.2.0/"

###################################################################
# Remove any existing builds

rm -rf "$OUTPUT"{,__ppc,__i386}

cd $PGSRC

###################################################################
# Build the PPC version

./configure --prefix=$OUTPUT

make
make install

mv $OUTPUT "$OUTPUT"__ppc

###################################################################
# Build the i386 version

make distclean

export CFLAGS="-arch i386 -isysroot /Developer/SDKs/MacOSX10.4u.sdk/"
export LDFLAGS="-arch i386 -isysroot /Developer/SDKs/MacOSX10.4u.sdk/"
./configure --prefix=$OUTPUT --host=i386-apple-darwin8.8.0

mv $PGSRC/src/interfaces/ecpg/test/Makefile{,.__ppc}

echo "all install installdirs uninstall dep depend distprep clean distclean maintainer-clean check checktcp installcheck:
	echo \"Skipping regression tests because we can't run code that doesn't fit on our architecture...\"" > $PGSRC/src/interfaces/ecpg/test/Makefile

perl -pi.__ppc -e 's/((.*)test\/regress)/#$1/' $PGSRC/src/Makefile

perl -pi.__ppc -e 's/\.\/zic/zic/' $PGSRC/src/timezone/Makefile

make
make install

rm $PGSRC/src/interfaces/ecpg/test/Makefile
mv $PGSRC/src/interfaces/ecpg/test/Makefile{.__ppc,}

rm $PGSRC/src/Makefile
mv $PGSRC/src/Makefile{.__ppc,}

rm $PGSRC/src/timezone/Makefile
mv $PGSRC/src/timezone/Makefile{.__ppc,}

mv $OUTPUT "$OUTPUT"__i386

###################################################################
# Lipo the two halves together

ditto "$OUTPUT"__ppc $OUTPUT

cd $OUTPUT/bin
for b in *; do
	if [ ! -L $b ]; then
		lipo -create "$OUTPUT"{__ppc,__i386}/bin/$b -output $b
	fi
done

cd $OUTPUT/lib
for l in *; do
	if [ ! -L $l ]; then
		lipo -create "$OUTPUT"{__ppc,__i386}/lib/$l -output $l
	fi
done

ranlib $OUTPUT/lib/*.a

cd $OUTPUT/lib/postgresql
for l in *; do
	lipo -create "$OUTPUT"{__ppc,__i386}/lib/postgresql/$l -output $l
done

rm -rf "$OUTPUT"__ppc
rm -rf "$OUTPUT"__i386

exit 0