#!/bin/bash
#
# 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
}

download_lib()
{
  local url=$1
  local filename=$2

  if [ ! -e $filename ]; then
    echo "Downloading $filename"
    curl -L -O ${url}${filename} || die "Download of $url$filename failed"
  fi
}

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

# Force build architecture, as sometimes configure is broken.
#  Using multiple values would in theory produce a "universal"
#  or fat binary, but this is untested.
#  Choices are: x86_64 i386 (ppc and ppc64 not supported)
ARCH="x86_64"

# 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).
#  Sometimes configure gets it wrong anyway, especially on cross compiles.
#  This is why we prefer using CFLAGS, CPPFLAGS, and LDFLAGS.

# Check if SYSROOT is set and not empty
if [[ $SYSROOT && ${SYSROOT-_} ]]; then
  CFLAGS="$CFLAGS -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"
  LDFLAGS="$LDFLAGS -mmacosx-version-min=$MIN_OSX_VERSION"
fi
CFLAGS="$CFLAGS -arch $ARCH"
CPPFLAGS="$CPPFLAGS $CFLAGS"
LDFLAGS="$LDFLAGS -arch $ARCH"

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

# TODO: Do we really want to rebuild these every time?

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

# SDL 1.2.15 required for Lion support
LIB_VERSION="SDL-1.2.15"
LIB_ARCHIVE="$LIB_VERSION.tar.gz"
LIB_DIRECTORY=$LIB_VERSION
LIB_URL="http://www.libsdl.org/release/"

mkdir -p sdl
cd sdl

INSTALL_DIR="$(pwd)"

download_lib $LIB_URL $LIB_ARCHIVE

rm -rf $LIB_DIRECTORY
tar -xf $LIB_ARCHIVE
cd $LIB_DIRECTORY

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

cd ../../

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

LIB_VERSION="boost_1_49_0"
LIB_ARCHIVE="$LIB_VERSION.tar.bz2"
LIB_DIRECTORY="$LIB_VERSION"
LIB_URL="http://download.sourceforge.net/boost/"

mkdir -p boost
cd boost

INSTALL_DIR="$(pwd)"

download_lib $LIB_URL $LIB_ARCHIVE

rm -rf $LIB_DIRECTORY
tar -xf $LIB_ARCHIVE
cd $LIB_DIRECTORY

# Can't use macosx-version, see above comment.
(./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 -e "\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?

LIB_VERSION="wxWidgets-2.9.3"
LIB_ARCHIVE="$LIB_VERSION.tar.bz2"
LIB_DIRECTORY="$LIB_VERSION"
LIB_URL="http://download.sourceforge.net/wxwindows/"

mkdir -p wxwidgets
cd wxwidgets

INSTALL_DIR="$(pwd)"

download_lib $LIB_URL $LIB_ARCHIVE

rm -rf $LIB_DIRECTORY
tar -xf $LIB_ARCHIVE
cd $LIB_DIRECTORY

mkdir -p build-release
cd build-release

(../configure CFLAGS="$CFLAGS" CPPFLAGS="$CPPFLAGS" LDFLAGS="$LDFLAGS" --prefix=$INSTALL_DIR --disable-shared --enable-unicode --with-cocoa --with-opengl && make $JOBS && make install) || die "wxWidgets build failed"

cd ../../../

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

LIB_VERSION="jpegsrc.v8c"
LIB_ARCHIVE="$LIB_VERSION.tar.gz"
LIB_DIRECTORY="jpeg-8c"
LIB_URL="http://www.ijg.org/files/"

mkdir -p libjpg
cd libjpg

INSTALL_DIR="$(pwd)"

download_lib $LIB_URL $LIB_ARCHIVE

rm -rf $LIB_DIRECTORY
tar -xf $LIB_ARCHIVE
cd $LIB_DIRECTORY

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

cd ../../

# ---------------------------------------------------
echo -e "\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 better to provide it ourselves

LIB_VERSION="libpng-1.5.8"
LIB_ARCHIVE="$LIB_VERSION.tar.gz"
LIB_DIRECTORY="$LIB_VERSION"
LIB_URL="http://download.sourceforge.net/libpng/"

mkdir -p libpng
cd libpng

INSTALL_DIR="$(pwd)"

download_lib $LIB_URL $LIB_ARCHIVE

rm -rf $LIB_DIRECTORY
tar -xf $LIB_ARCHIVE
cd $LIB_DIRECTORY

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

cd ../../

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

LIB_VERSION="libogg-1.3.0"
LIB_ARCHIVE="$LIB_VERSION.tar.gz"
LIB_DIRECTORY="$LIB_VERSION"
LIB_URL="http://downloads.xiph.org/releases/ogg/"

# 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

download_lib $LIB_URL $LIB_ARCHIVE

rm -rf $LIB_DIRECTORY
tar -xf $LIB_ARCHIVE
cd $LIB_DIRECTORY

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

cd ../../

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

LIB_VERSION="libvorbis-1.3.2"
LIB_ARCHIVE="$LIB_VERSION.tar.bz2"
LIB_DIRECTORY="$LIB_VERSION"
LIB_URL="http://downloads.xiph.org/releases/vorbis/"

cd vorbis

INSTALL_DIR="$(pwd)"

download_lib $LIB_URL $LIB_ARCHIVE

rm -rf $LIB_DIRECTORY
tar -xf $LIB_ARCHIVE
cd $LIB_DIRECTORY

(./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 -e "\nBuilding libxml2...\n"

LIB_VERSION="libxml2-2.7.8"
LIB_ARCHIVE="$LIB_VERSION.tar.gz"
LIB_DIRECTORY="$LIB_VERSION"
LIB_URL="ftp://xmlsoft.org/libxml2/"

mkdir -p libxml2
cd libxml2

INSTALL_DIR="$(pwd)"

download_lib $LIB_URL $LIB_ARCHIVE

rm -rf $LIB_DIRECTORY
tar -xf $LIB_ARCHIVE
cd $LIB_DIRECTORY

(./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 -e "\nBuilding Spidermonkey...\n"
pushd ../source/spidermonkey/

INSTALL_DIR="$(pwd)"

rm -rf lib/
rm -rf js-1.8.5
tar -xf 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"
# Uncomment this line for 32-bit 10.5 cross compile:
#CONF_OPTS="$CONF_OPTS --target=i386-apple-darwin9.0.0"
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
(CC="$CC -arch $ARCH" CXX="$CXX -arch $ARCH" AR=ar CROSS_COMPILE=1 ../configure $CONF_OPTS --enable-debug --disable-optimize && make ${JOBS} && make install) || die "Spidermonkey build failed"
cd ..

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

mkdir -p build-release
cd build-release
(CC="$CC -arch $ARCH" CXX="$CXX -arch $ARCH" AR=ar CROSS_COMPILE=1 ../configure $CONF_OPTS && make ${JOBS} && make install) || die "Spidermonkey build failed"
cd ..

mv Makefile.in.bak Makefile.in

popd

# ---------------------------------------------------
echo -e "\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 clean && make ${JOBS} && make install) || die "ENet build failed"
popd

popd

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

rm -rf build
mkdir -p build

pushd build

# Could use CMAKE_OSX_DEPLOYMENT_TARGET and CMAKE_OSX_SYSROOT
#	but they're not as flexible for cross-compiling
(cmake .. -DCMAKE_LINK_FLAGS="$LDFLAGS" -DCMAKE_C_FLAGS="$CFLAGS" -DCMAKE_CXX_FLAGS="$CPPFLAGS" -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 -e "\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 clean && CXXFLAGS=$CPPFLAGS make $JOBS) || die "FCollada build failed"
# Undo Makefile change
mv Makefile.bak Makefile

popd
