#!/bin/bash

PPCFLAGS="-isysroot /Developer/SDKs/MacOSX10.5.sdk -mmacosx-version-min=10.4  -arch ppc -ggdb"
PPCCC="/usr/bin/gcc-4.0"
PPCCXX="/usr/bin/g++-4.0"
PPCHOST="ppc-apple-darwin8"
PPCPATH="${PWD}/opt-ppc"
PPCLDFLAGS="-arch ppc"

I386FLAGS="-isysroot /Developer/SDKs/MacOSX10.5.sdk -mmacosx-version-min=10.4  -arch i386 -ggdb"
I386CC="/usr/bin/gcc-4.0"
I386CXX="/usr/bin/g++-4.0"
I386HOST="i386-apple-darwin8"
I386PATH="${PWD}/opt-i386"
I386LDFLAGS="-arch i386"

X86_64FLAGS="-isysroot /Developer/SDKs/MacOSX10.5.sdk -mmacosx-version-min=10.5  -arch x86_64 -ggdb"
X86_64CC="/usr/bin/gcc-4.2"
X86_64CXX="/usr/bin/g++-4.2"
X86_64HOST="x86_64-apple-darwin9"
X86_64PATH="${PWD}/opt-x86_64"
X86_64LDFLAGS="-arch x86_64"

FINALPATH="${PWD}/opt"

if [ "$1" = "clean" ]; then
	echo "Cleaning PJProject"
	rm -fR pjproject-i386 pjproject-x86_64 pjproject-ppc pjproject-ios pjproject-simulator
fi
if [ "$1" = "ios" ]; then
	IOSCFLAGS="-miphoneos-version-min=3.1"
	IOS6PATH="${PWD}/opt-ios6"
	IOS7PATH="${PWD}/opt-ios7"
	SIMULATORPATH="${PWD}/opt-simulator"
	
	echo "Copying PJProject"
	PJPROJECTNAME="pjproject"
	cp -R ${PJPROJECTNAME} ${PJPROJECTNAME}-ios6
	echo "#define PJ_CONFIG_IPHONE 1" > ${PJPROJECTNAME}-ios6/pjlib/include/pj/config_site.h
	echo "#include <pj/config_site_sample.h>" >> ${PJPROJECTNAME}-ios6/pjlib/include/pj/config_site.h
	
	OLDDIR="${PWD}"
	cd ${PJPROJECTNAME}-ios6
	patch -p0 < "${OLDDIR}/mips_test.patch"
	cd "${OLDDIR}"
	cp -R ${PJPROJECTNAME}-ios6 ${PJPROJECTNAME}-ios7
	cp -R ${PJPROJECTNAME}-ios6 ${PJPROJECTNAME}-simulator
	
	echo "Configuring PJProject"
	cd ${PJPROJECTNAME}-ios6
	CC="/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/gcc-4.2 -arch armv6" CFLAGS="${IOSCFLAGS}" ./configure-iphone --prefix="${IOS6PATH}"
	cd ../${PJPROJECTNAME}-ios7
	CC="/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/gcc-4.2 -arch armv7" CFLAGS="${IOSCFLAGS}" ./configure-iphone --prefix="${IOS7PATH}"
	cd ../${PJPROJECTNAME}-simulator
	DEVPATH="/Developer/Platforms/iPhoneSimulator.platform/Developer" CC="/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/gcc-4.2" CFLAGS="${IOSCFLAGS}" ./configure-iphone --prefix="${SIMULATORPATH}"
	cd ../
	
	echo "Compiling PJProject"
	cd ${PJPROJECTNAME}-ios6
	make dep
	make
	cd ../${PJPROJECTNAME}-ios7
	make dep
	make
	cd ../${PJPROJECTNAME}-simulator
	make dep
	make
	cd ../
	
	echo "Installing PJProject"
	cd ${PJPROJECTNAME}-ios6
	make install
	cd ../${PJPROJECTNAME}-ios7
	make install
	cd ../${PJPROJECTNAME}-simulator
	make install
	cd ../
	
	echo "Making PJProject Universal"
	mkdir -p "${FINALPATH}/lib/pkgconfig"
	cp -R "${PPCPATH}/include" "${FINALPATH}/include"
	lipo -create "${IOS6PATH}/lib/libg7221codec-arm-apple-darwin9.a" "${IOS7PATH}/lib/libg7221codec-arm-apple-darwin9.a" "${SIMULATORPATH}/lib/libg7221codec-arm-apple-darwin9.a" -output "${FINALPATH}/lib/libg7221codec-ios.a"
	lipo -create "${IOS6PATH}/lib/libgsmcodec-arm-apple-darwin9.a" "${IOS7PATH}/lib/libgsmcodec-arm-apple-darwin9.a" "${SIMULATORPATH}/lib/libgsmcodec-arm-apple-darwin9.a" -output "${FINALPATH}/lib/libgsmcodec-ios.a"
	lipo -create "${IOS6PATH}/lib/libilbccodec-arm-apple-darwin9.a" "${IOS7PATH}/lib/libilbccodec-arm-apple-darwin9.a" "${SIMULATORPATH}/lib/libilbccodec-arm-apple-darwin9.a" -output "${FINALPATH}/lib/libilbccodec-ios.a"
	lipo -create "${IOS6PATH}/lib/libmilenage-arm-apple-darwin9.a" "${IOS7PATH}/lib/libmilenage-arm-apple-darwin9.a" "${SIMULATORPATH}/lib/libmilenage-arm-apple-darwin9.a" -output "${FINALPATH}/lib/libmilenage-ios.a"
	lipo -create "${IOS6PATH}/lib/libpj-arm-apple-darwin9.a" "${IOS7PATH}/lib/libpj-arm-apple-darwin9.a" "${SIMULATORPATH}/lib/libpj-arm-apple-darwin9.a" -output "${FINALPATH}/lib/libpj-ios.a"
	lipo -create "${IOS6PATH}/lib/libpjlib-util-arm-apple-darwin9.a" "${IOS7PATH}/lib/libpjlib-util-arm-apple-darwin9.a" "${SIMULATORPATH}/lib/libpjlib-util-arm-apple-darwin9.a" -output "${FINALPATH}/lib/libpjlib-util-ios.a"
	lipo -create "${IOS6PATH}/lib/libpjmedia-arm-apple-darwin9.a" "${IOS7PATH}/lib/libpjmedia-arm-apple-darwin9.a" "${SIMULATORPATH}/lib/libpjmedia-arm-apple-darwin9.a" -output "${FINALPATH}/lib/libpjmedia-ios.a"
	lipo -create "${IOS6PATH}/lib/libpjmedia-audiodev-arm-apple-darwin9.a" "${IOS7PATH}/lib/libpjmedia-audiodev-arm-apple-darwin9.a" "${SIMULATORPATH}/lib/libpjmedia-audiodev-arm-apple-darwin9.a" -output "${FINALPATH}/lib/libpjmedia-audiodev-ios.a"
	lipo -create "${IOS6PATH}/lib/libpjmedia-codec-arm-apple-darwin9.a" "${IOS7PATH}/lib/libpjmedia-codec-arm-apple-darwin9.a" "${SIMULATORPATH}/lib/libpjmedia-codec-arm-apple-darwin9.a" -output "${FINALPATH}/lib/libpjmedia-codec-ios.a"
	lipo -create "${IOS6PATH}/lib/libpjnath-arm-apple-darwin9.a" "${IOS7PATH}/lib/libpjnath-arm-apple-darwin9.a" "${SIMULATORPATH}/lib/libpjnath-arm-apple-darwin9.a" -output "${FINALPATH}/lib/libpjnath-ios.a"
	lipo -create "${IOS6PATH}/lib/libpjsip-arm-apple-darwin9.a" "${IOS7PATH}/lib/libpjsip-arm-apple-darwin9.a" "${SIMULATORPATH}/lib/libpjsip-arm-apple-darwin9.a" -output "${FINALPATH}/lib/libpjsip-ios.a"
	lipo -create "${IOS6PATH}/lib/libpjsip-simple-arm-apple-darwin9.a" "${IOS7PATH}/lib/libpjsip-simple-arm-apple-darwin9.a" "${SIMULATORPATH}/lib/libpjsip-simple-arm-apple-darwin9.a" -output "${FINALPATH}/lib/libpjsip-simple-ios.a"
	lipo -create "${IOS6PATH}/lib/libpjsip-ua-arm-apple-darwin9.a" "${IOS7PATH}/lib/libpjsip-ua-arm-apple-darwin9.a" "${SIMULATORPATH}/lib/libpjsip-ua-arm-apple-darwin9.a" -output "${FINALPATH}/lib/libpjsip-ua-ios.a"
	lipo -create "${IOS6PATH}/lib/libpjsua-arm-apple-darwin9.a" "${IOS7PATH}/lib/libpjsua-arm-apple-darwin9.a" "${SIMULATORPATH}/lib/libpjsua-arm-apple-darwin9.a" -output "${FINALPATH}/lib/libpjsua-ios.a"
	lipo -create "${IOS6PATH}/lib/libresample-arm-apple-darwin9.a" "${IOS7PATH}/lib/libresample-arm-apple-darwin9.a" "${SIMULATORPATH}/lib/libresample-arm-apple-darwin9.a" -output "${FINALPATH}/lib/libresample-ios.a"
	lipo -create "${IOS6PATH}/lib/libspeex-arm-apple-darwin9.a" "${IOS7PATH}/lib/libspeex-arm-apple-darwin9.a" "${SIMULATORPATH}/lib/libspeex-arm-apple-darwin9.a" -output "${FINALPATH}/lib/libspeex-ios.a"
	lipo -create "${IOS6PATH}/lib/libsrtp-arm-apple-darwin9.a" "${IOS7PATH}/lib/libsrtp-arm-apple-darwin9.a" "${SIMULATORPATH}/lib/libsrtp-arm-apple-darwin9.a" -output "${FINALPATH}/lib/libsrtp-ios.a"
fi
if [ "$1" = "" ]; then
	echo "Copying PJProject"
	PJPROJECTNAME="pjproject"
	cp -R ${PJPROJECTNAME} ${PJPROJECTNAME}-ppc
	echo "#define PJMEDIA_AUDIO_DEV_HAS_PORTAUDIO 0" > ${PJPROJECTNAME}-ppc/pjlib/include/pj/config_site.h
	echo "#define PJMEDIA_AUDIO_DEV_HAS_COREAUDIO 1" >> ${PJPROJECTNAME}-ppc/pjlib/include/pj/config_site.h
	cp -R ${PJPROJECTNAME}-ppc ${PJPROJECTNAME}-i386
	cp -R ${PJPROJECTNAME}-ppc ${PJPROJECTNAME}-x86_64
	
	echo "Configuring PJProject"
	cd ${PJPROJECTNAME}-ppc
	LDFLAGS="${PPCLDFLAGS}" CFLAGS="${PPCFLAGS}" CXXFLAGS="${PPCFLAGS}" ./configure --prefix="${PPCPATH}" --host="${PPCHOST}" --disable-shared --enable-static
	cd ../${PJPROJECTNAME}-i386
	LDFLAGS="${I386LDFLAGS}" CFLAGS="${I386FLAGS}" CXXFLAGS="${I386FLAGS}" ./configure --prefix="${I386PATH}" --host="${I386HOST}" --disable-shared --enable-static
	cd ../${PJPROJECTNAME}-x86_64
	LDFLAGS="${X86_64LDFLAGS}" CFLAGS="${X86_64FLAGS}" CXXFLAGS="${X86_64FLAGS}" ./configure --prefix="${X86_64PATH}" --host="${X86_64HOST}" --disable-shared --enable-static
	cd ../
	
	echo "Compiling PJProject"
	cd ${PJPROJECTNAME}-ppc
	make dep
	make
	cd ../${PJPROJECTNAME}-i386
	make dep
	make
	cd ../${PJPROJECTNAME}-x86_64
	make dep
	make
	cd ../
	
	echo "Installing PJProject"
	cd ${PJPROJECTNAME}-ppc
	make install
	cd ../${PJPROJECTNAME}-i386
	make install
	cd ../${PJPROJECTNAME}-x86_64
	make install
	cd ../
	
	echo "Making PJProject Universal"
	mkdir -p "${FINALPATH}/lib/pkgconfig"
	cp -R "${PPCPATH}/include" "${FINALPATH}/include"
	lipo -create "${PPCPATH}/lib/libg7221codec-powerpc-apple-darwin8.a" "${I386PATH}/lib/libg7221codec-i386-apple-darwin8.a" "${X86_64PATH}/lib/libg7221codec-x86_64-apple-darwin9.a" -output "${FINALPATH}/lib/libg7221codec.a"
	lipo -create "${PPCPATH}/lib/libgsmcodec-powerpc-apple-darwin8.a" "${I386PATH}/lib/libgsmcodec-i386-apple-darwin8.a" "${X86_64PATH}/lib/libgsmcodec-x86_64-apple-darwin9.a" -output "${FINALPATH}/lib/libgsmcodec.a"
	lipo -create "${PPCPATH}/lib/libilbccodec-powerpc-apple-darwin8.a" "${I386PATH}/lib/libilbccodec-i386-apple-darwin8.a" "${X86_64PATH}/lib/libilbccodec-x86_64-apple-darwin9.a" -output "${FINALPATH}/lib/libilbccodec.a"
	lipo -create "${PPCPATH}/lib/libmilenage-powerpc-apple-darwin8.a" "${I386PATH}/lib/libmilenage-i386-apple-darwin8.a" "${X86_64PATH}/lib/libmilenage-x86_64-apple-darwin9.a" -output "${FINALPATH}/lib/libmilenage.a"
	lipo -create "${PPCPATH}/lib/libpj-powerpc-apple-darwin8.a" "${I386PATH}/lib/libpj-i386-apple-darwin8.a" "${X86_64PATH}/lib/libpj-x86_64-apple-darwin9.a" -output "${FINALPATH}/lib/libpj.a"
	lipo -create "${PPCPATH}/lib/libpjlib-util-powerpc-apple-darwin8.a" "${I386PATH}/lib/libpjlib-util-i386-apple-darwin8.a" "${X86_64PATH}/lib/libpjlib-util-x86_64-apple-darwin9.a" -output "${FINALPATH}/lib/libpjlib-util.a"
	lipo -create "${PPCPATH}/lib/libpjmedia-audiodev-powerpc-apple-darwin8.a" "${I386PATH}/lib/libpjmedia-audiodev-i386-apple-darwin8.a" "${X86_64PATH}/lib/libpjmedia-audiodev-x86_64-apple-darwin9.a" -output "${FINALPATH}/lib/libpjmedia-audiodev.a"
	lipo -create "${PPCPATH}/lib/libpjmedia-codec-powerpc-apple-darwin8.a" "${I386PATH}/lib/libpjmedia-codec-i386-apple-darwin8.a" "${X86_64PATH}/lib/libpjmedia-codec-x86_64-apple-darwin9.a" -output "${FINALPATH}/lib/libpjmedia-codec.a"
	lipo -create "${PPCPATH}/lib/libpjmedia-powerpc-apple-darwin8.a" "${I386PATH}/lib/libpjmedia-i386-apple-darwin8.a" "${X86_64PATH}/lib/libpjmedia-x86_64-apple-darwin9.a" -output "${FINALPATH}/lib/libpjmedia.a"
	lipo -create "${PPCPATH}/lib/libpjnath-powerpc-apple-darwin8.a" "${I386PATH}/lib/libpjnath-i386-apple-darwin8.a" "${X86_64PATH}/lib/libpjnath-x86_64-apple-darwin9.a" -output "${FINALPATH}/lib/libpjnath.a"
	lipo -create "${PPCPATH}/lib/libpjsip-powerpc-apple-darwin8.a" "${I386PATH}/lib/libpjsip-i386-apple-darwin8.a" "${X86_64PATH}/lib/libpjsip-simple-x86_64-apple-darwin9.a" -output "${FINALPATH}/lib/libpjsip-simple.a"
	lipo -create "${PPCPATH}/lib/libpjsip-simple-powerpc-apple-darwin8.a" "${I386PATH}/lib/libpjsip-simple-i386-apple-darwin8.a" "${X86_64PATH}/lib/libpjsip-ua-x86_64-apple-darwin9.a" -output "${FINALPATH}/lib/libpjsip-ua.a"
	lipo -create "${PPCPATH}/lib/libpjsip-ua-powerpc-apple-darwin8.a" "${I386PATH}/lib/libpjsip-ua-i386-apple-darwin8.a" "${X86_64PATH}/lib/libpjsip-x86_64-apple-darwin9.a" -output "${FINALPATH}/lib/libpjsip.a"
	lipo -create "${PPCPATH}/lib/libpjsua-powerpc-apple-darwin8.a" "${I386PATH}/lib/libpjsua-i386-apple-darwin8.a" "${X86_64PATH}/lib/libpjsua-x86_64-apple-darwin9.a" -output "${FINALPATH}/lib/libpjsua.a"
	lipo -create "${PPCPATH}/lib/libportaudio-powerpc-apple-darwin8.a" "${I386PATH}/lib/libportaudio-i386-apple-darwin8.a" "${X86_64PATH}/lib/libportaudio-x86_64-apple-darwin9.a" -output "${FINALPATH}/lib/libportaudio.a"
	lipo -create "${PPCPATH}/lib/libresample-powerpc-apple-darwin8.a" "${I386PATH}/lib/libresample-i386-apple-darwin8.a" "${X86_64PATH}/lib/libresample-x86_64-apple-darwin9.a" -output "${FINALPATH}/lib/libresample.a"
	lipo -create "${PPCPATH}/lib/libspeex-powerpc-apple-darwin8.a" "${I386PATH}/lib/libspeex-i386-apple-darwin8.a" "${X86_64PATH}/lib/libspeex-x86_64-apple-darwin9.a" -output "${FINALPATH}/lib/libspeex.a"
	lipo -create "${PPCPATH}/lib/libsrtp-powerpc-apple-darwin8.a" "${I386PATH}/lib/libsrtp-i386-apple-darwin8.a" "${X86_64PATH}/lib/libsrtp-x86_64-apple-darwin9.a" -output "${FINALPATH}/lib/libsrtp.a"
fi