#!/bin/sh
#
# Script for acquiring and building OSX dependencies for 0 A.D.
#
# The script checks whether a source tarball exists for each
#  dependency, if not it will download the correct version from
#  the project's website, then it removes previous temp files,
#  extracts the tarball, configures and builds the lib. The script
#  should die on any errors to ease troubleshooting.
#
# make install is used to copy the compiled libs to each specific
#  directory and also the config tools (e.g. sdl-config). Because
#  of this, every developer must run this script at least once,
#  to configure the correct lib directories. It must be run again
#  if the libraries are moved.
#
# Building against an SDK is an option, though not required,
# as not all build environments contain the Developer SDKs.
# (Xcode does, but the Command Line Tools package does not)
#

set -e

die()
{
  echo ERROR: $*
  exit 1
}

JOBS=${JOBS:="-j5"}

# Define compiler as GCC (in case anything expects e.g. gcc-4.2)
export CC=${CC:="gcc"} CXX=${CXX:="g++"}

# The various libs offer inconsistent configure options, some allow
#  setting sysroot and OSX-specific options, others don't. Adding to
#  the confusion, Apple moved /Developer/SDKs into the Xcode app bundle
#  so the path can't be guessed by clever build tools (like Boost.Build)
#  This is why we set CFLAGS, CPPFLAGS, and LDFLAGS.

# Check if SYSROOT is set and not empty
if [[ $SYSROOT && ${SYSROOT-_} ]]; then
  CFLAGS="$CFLAGS -isysroot $SYSROOT"
  CPPFLAGS="$CPPFLAGS -isysroot $SYSROOT"
  LDFLAGS="$LDFLAGS -Wl,-syslibroot,$SYSROOT"
fi
# Check if MIN_OSX_VERSION is set and not empty
if [[ $MIN_OSX_VERSION && ${MIN_OSX_VERSION-_} ]]; then
  CFLAGS="$CFLAGS -mmacosx-version-min=$MIN_OSX_VERSION"
  CPPFLAGS="$CPPFLAGS -mmacosx-version-min=$MIN_OSX_VERSION"
fi
# Force 64-bit build (we could also add 32-bit if we want universal)
CFLAGS="$CFLAGS -arch x86_64"
CPPFLAGS="$CPPFLAGS -arch x86_64"
LDFLAGS="$LDFLAGS -arch x86_64"

cd "$(dirname $0)"
# Now in libraries/osx/ (where we assume this script resides)

# TODO: Do we really want to rebuild these every time?
# TODO: Debug builds of any of these libs?

# ---------------------------------------------------
echo "\nBuilding SDL...\n"

# SDL 1.2.15 required for Lion support
mkdir -p sdl
cd sdl

INSTALL_DIR="$(pwd)"

if [ ! -e SDL-1.2.15.tar.gz ]; then
    echo "Downloading source tarball"
    curl -O http://www.libsdl.org/release/SDL-1.2.15.tar.gz
fi

rm -rf SDL-1.2.15/
tar xzf SDL-1.2.15.tar.gz
cd SDL-1.2.15

(./configure CFLAGS="$CFLAGS" CPPFLAGS="$CPPFLAGS" LDFLAGS="$LDFLAGS" --prefix="$INSTALL_DIR" --enable-shared=no && make $JOBS && make install) || die "SDL build failed"

cd ../../

# ---------------------------------------------------
echo "\nBuilding Boost...\n"

mkdir -p boost
cd boost

INSTALL_DIR="$(pwd)"

if [ ! -e boost_1_49_0.tar.bz2 ]; then
    echo "Downloading source tarball"
    curl -L -O http://sourceforge.net/projects/boost/files/boost/1.49.0/boost_1_49_0.tar.bz2
fi

rm -rf boost_1_49_0/
tar --bzip2 -xf boost_1_49_0.tar.bz2
cd boost_1_49_0

# Can't use macosx-version because Boost only searches /Developer for SDKs
(./bootstrap.sh --with-libraries=filesystem,system,signals --prefix=$INSTALL_DIR && ./b2 cflags="$CFLAGS" cxxflags="$CPPFLAGS" linkflags="$LDFLAGS" -d2 --layout=tagged --debug-configuration link=static threading=multi variant=release,debug install) || die "Boost build failed"

# Copy files
cd ../../

# ---------------------------------------------------
echo "\nBuilding wxWidgets...\n"

# wxWidgets is required for Atlas
#  version 2.9+ is necessary for 64-bit OSX build
#  with OpenGL support

# TODO: This build takes ages, anything we can exlude?
mkdir -p wxwidgets
cd wxwidgets

INSTALL_DIR="$(pwd)"

if [ ! -e wxWidgets-2.9.3.tar.bz2 ]; then
    echo "Downloading source tarball"
    curl -L -O http://download.sourceforge.net/wxwindows/wxWidgets-2.9.3.tar.bz2
fi

rm -rf wxWidgets-2.9.3
tar --bzip2 -xf wxWidgets-2.9.3.tar.bz2
cd wxWidgets-2.9.3

mkdir -p build-release
cd build-release

CONF_OPTS="--prefix=$INSTALL_DIR --disable-shared --enable-unicode --with-cocoa --with-opengl"
if [[ $MIN_OSX_VERSION && ${MIN_OSX_VERSION-_} ]]; then
  CONF_OPTS="$CONF_OPTS --with-macosx-version-min=$MIN_OSX_VERSION"
fi
if [[ $SYSROOT && ${SYSROOT-_} ]]; then
  CONF_OPTS="$CONF_OPTS --with-macosx-sdk=$SYSROOT"
fi

(../configure $CONF_OPTS && make $JOBS && make install) || die "wxWidgets build failed"

cd ../../../

# ---------------------------------------------------
echo "\nBuilding libjpg...\n"

mkdir -p libjpg
cd libjpg

INSTALL_DIR="$(pwd)"

if [ ! -e jpegsrc.v8c.tar.gz ]; then
    echo "Downloading source tarball"
    curl -O http://www.ijg.org/files/jpegsrc.v8c.tar.gz
fi

rm -rf jpeg-8c
tar xzf jpegsrc.v8c.tar.gz
cd jpeg-8c

CONF_OPTS="--prefix=$INSTALL_DIR --enable-shared=no"
if [[ $SYSROOT && ${SYSROOT-_} ]]; then
  CONF_OPTS="$CONF_OPTS --with-sysroot=$SYSROOT"
fi
(./configure $CONF_OPTS && make $JOBS && make install) || die "libjpg build failed"

cd ../../

# ---------------------------------------------------
echo "\nBuilding libpng...\n"

# Even though libpng is provided in the SDK, the 10.6 bundled version
#  is the ancient 1.2 which lacks functions we use like
#  png_set_longjmp_fn, so it seems easier to provide it ourselves
mkdir -p libpng
cd libpng

INSTALL_DIR="$(pwd)"

if [ ! -e libpng-1.5.8.tar.gz ]; then
    echo "Downloading source tarball"
    curl -L -O http://download.sourceforge.net/libpng/libpng-1.5.8.tar.gz
fi

rm -rf libpng-1.5.8
tar xzf libpng-1.5.8.tar.gz
cd libpng-1.5.8

CONF_OPTS="--prefix=$INSTALL_DIR --enable-shared=no"
if [[ $SYSROOT && ${SYSROOT-_} ]]; then
  CONF_OPTS="$CONF_OPTS --with-sysroot=$SYSROOT"
fi
(./configure $CONF_OPTS && make $JOBS && make install) || die "libpng build failed"

cd ../../

# ---------------------------------------------------
echo "\nBuilding libogg...\n"

# Dependency of vorbis
# we can install them in the same directory for convenience
mkdir -p libogg
mkdir -p vorbis

INSTALL_DIR="$(pwd)/vorbis"

cd libogg

if [ ! -e libogg-1.3.0.tar.gz ]
then
    echo "Downloading source tarball"
    curl -O http://downloads.xiph.org/releases/ogg/libogg-1.3.0.tar.gz
fi

rm -rf libogg-1.3.0
tar xzf libogg-1.3.0.tar.gz
cd libogg-1.3.0

CONF_OPTS="--prefix=$INSTALL_DIR --enable-shared=no"
if [[ $SYSROOT && ${SYSROOT-_} ]]; then
  CONF_OPTS="$CONF_OPTS --with-sysroot=$SYSROOT"
fi
(./configure $CONF_OPTS && make $JOBS && make install) || die "libogg build failed"

cd ../../

# ---------------------------------------------------
echo "\nBuilding libvorbis...\n"

cd vorbis

INSTALL_DIR="$(pwd)"

if [ ! -e libvorbis-1.3.2.tar.bz2 ]; then
    echo "Downloading source tarball"
    curl -O http://downloads.xiph.org/releases/vorbis/libvorbis-1.3.2.tar.bz2
fi

rm -rf libvorbis-1.3.2
tar --bzip2 -xf libvorbis-1.3.2.tar.bz2
cd libvorbis-1.3.2

(./configure CFLAGS="$CFLAGS" CPPFLAGS="$CPPFLAGS" LDFLAGS="$LDFLAGS" --prefix="$INSTALL_DIR" --enable-shared=no --with-ogg="$INSTALL_DIR" && make $JOBS && make install) || die "libvorbis build failed"

cd ../../

# ---------------------------------------------------
echo "\nBuilding libxml2...\n"

mkdir -p libxml2
cd libxml2

INSTALL_DIR="$(pwd)"

if [ ! -e libxml2-2.7.8.tar.gz ]; then
    echo "Downloading source tarball"
    curl -O ftp://xmlsoft.org/libxml2/libxml2-2.7.8.tar.gz
fi

rm -rf libmxl2-2.7.8
tar xzf libxml2-2.7.8.tar.gz
cd libxml2-2.7.8

(./configure CFLAGS="$CFLAGS" CPPFLAGS="$CPPFLAGS" LDFLAGS="$LDFLAGS" --prefix="$INSTALL_DIR" --enable-shared=no && make $JOBS && make install) || die "libxml2 build failed"

cd ../../


# ---------------------------------------------------
# The following libraries are shared on different
#  OSes and may be modified, so we build and install
#  them from bundled sources
# ---------------------------------------------------
echo "\nBuilding Spidermonkey...\n"
pushd ../source/spidermonkey/

INSTALL_DIR="$(pwd)"

rm -f lib/*.a
rm -rf js-1.8.5
tar xzf js185-1.0.0.tar.gz
cd js-1.8.5/js/src

# We want separate debug/release versions of the library, so change their install name in the Makefile
sed -i.bak -e 's/\(STATIC_LIBRARY_NAME),mozjs185-\)\(\$(SRCREL_ABI_VERSION)\)\{0,1\}/\1ps-debug/' Makefile.in

CONF_OPTS="--prefix=${INSTALL_DIR} --disable-tests --disable-shared-js"
if [[ $MIN_OSX_VERSION && ${MIN_OSX_VERSION-_} ]]; then
  CONF_OPTS="$CONF_OPTS --enable-macos-target=$MIN_OSX_VERSION"
fi
if [[ $SYSROOT && ${SYSROOT-_} ]]; then
  CONF_OPTS="$CONF_OPTS --with-macosx-sdk=$SYSROOT"
fi

mkdir -p build-debug
cd build-debug
(../configure $CONF_OPTS --enable-debug --disable-optimize && make ${JOBS} && make install) || die "Spidermonkey build failed"
cd ..

sed -i.bak -e 's/\(STATIC_LIBRARY_NAME),mozjs185-\)\(ps-debug\)\{0,1\}/\1ps-release/' Makefile.in

mkdir -p build-release
cd build-release
(../configure $CONF_OPTS && make ${JOBS} && make install) || die "Spidermonkey build failed"
cd ..

# Remove the install name changes to avoid spurious SVN diffs
sed -i.bak -e 's/\(STATIC_LIBRARY_NAME),mozjs185-\)\(ps-release\)\{0,1\}/\1$(SRCREL_ABI_VERSION)/' Makefile.in

popd

# ---------------------------------------------------
echo "\nBuilding ENet...\n"
pushd ../source/enet/

INSTALL_DIR="$(pwd)"

pushd src
(./configure CFLAGS="${CFLAGS}" CPPFLAGS="${CPPFLAGS}" LDFLAGS="${LDFLAGS}" --prefix=${INSTALL_DIR} --enable-shared=no && make ${JOBS} && make install) || die "ENet build failed"
popd

popd

# ---------------------------------------------------
# NVTT - no install
echo "\nBuilding NVTT...\n"
pushd ../source/nvtt/src

rm -rf build
mkdir -p build

pushd build

CMAKE_OPTS=""
if [[ $SYSROOT && ${SYSROOT-_} ]]; then
  CMAKE_OPTS="-DCMAKE_OSX_SYSROOT=$SYSROOT"
fi
(cmake .. $CMAKE_OPTS -DCMAKE_BUILD_TYPE=Release -DBINDIR=bin -DLIBDIR=lib -DGLUT=0 -DGLEW=0 -DCG=0 -DCUDA=0 -DOPENEXR=0 -G "Unix Makefiles" && make clean && make nvtt ${JOBS}) || die "NVTT build failed"
popd

mkdir -p ../lib
cp build/src/nv*/libnv*.a ../lib/
cp build/src/nvtt/squish/libsquish.a ../lib/

popd

# ---------------------------------------------------
# FCollada - no install
echo "\nBuilding FCollada...\n"
pushd ../source/fcollada/src

rm -rf output
# The Makefile refers to pkg-config for libxml2, but we
# don't have that (replace with xml2-config instead)
sed -i.bak -e 's/pkg-config libxml-2.0/xml2-config/' Makefile
make $JOBS || die "FCollada build failed"
# Undo Makefile change
sed -i.bak -e 's/xml2-config/pkg-config libxml-2.0/' Makefile

popd

