#!/bin/bash
#

#This will make the mac .app bundle

if [ $1 = '--update-config=yes' ]; then
	echo "Updating configuration files"

	PNG_DIR="/usr/local"
	./configure --with-freetype=/usr/local \
		--with-wx-prefix=/usr/local \
		--with-xml-config=/opt/local/bin/xml2-config \
		--with-libpng-flags="-I$PNG_DIR/include" \
		--with-libpng-link="-L$PNG_DIR/lib -lpng14" \
		--with-apple-opengl-framework \
		LDFLAGS="-headerpad_max_install_names"

	if [ $? -ne 0 ]; then
		echo "Configure unsuccessful - exiting"
		exit
	fi


	make clean
fi

make

if [ $? -ne 0 ]; then
	echo "Make unsuccessful - exiting"
	exit
fi

echo "Updating .app bundle..."
mkdir -p ./main.app/Contents/MacOS/
cp ./src/3Depict ./main.app/Contents/MacOS/main
cp src/tex-source/3Depict-icon.icns ./main.app/Contents/Resources/wxmac.icns
touch ./main.app/Contents/PkgInfo
touch ./main.app/Contents/info.plist

# relabel libraries for packaging
dylibbundler -od -b -x ./main.app/Contents/MacOS/main -d ./main.app/Contents/libs -i /usr/lib
install_name_tool -change /opt/local/lib/libfreetype.6.dylib @executable_path/../libs/libfreetype.6.dylib ./main.app/Contents/libs/libftgl.2.1.3.dylib

#copy textures
cp -R ./src/textures ./main.app/Contents/Resources

echo "Done"

