#!/bin/sh
set -e

if [ $# -lt 2 ]; then
    echo "$0 iphonesimulator i386|x86_64 [command [args...]]" 1>&2
    echo "$0 iphoneos armv7|armv7s|arm64 [command [args...]]" 1>&2
    exit 1
fi

PLATFORM=$1
shift
ARCH=$1
shift

MINIOSVERSION="9.0"
if [ "$PLATFORM" = "iphoneos" ]; then
    EXTRA_CONFIG="--host=arm-apple-darwin --target=arm-apple-darwin --disable-shared"
    MINVERSION="-miphoneos-version-min=$MINIOSVERSION"
elif [ "$ARCH" = "i386" ]; then
    EXTRA_CONFIG="--host=i386-apple-darwin --target=i386-apple-darwin --disable-shared"
    MINVERSION="-mios-simulator-version-min=$MINIOSVERSION"
elif [ "$ARCH" = "x86_64" ]; then
    EXTRA_CONFIG="--host=x86_64-apple-darwin --target=x86_64-apple-darwin --disable-shared"
    MINVERSION="-mios-simulator-version-min=$MINIOSVERSION"
else
    echo "$0: unsupported configuration" 1>&2
    exit 1
fi

echo ""
echo "Cross compiling for $PLATFORM and $ARCH"

ROOTDIR=$(cd $(dirname $0) && pwd -P)
SOURCEDIR="$ROOTDIR/../../"
DESTDIR="$ROOTDIR/tmp"

export CC="$(xcrun -find -sdk ${PLATFORM} cc)"
export CXX="$(xcrun -find -sdk ${PLATFORM} g++)"
export CPPFLAGS="-arch ${ARCH} -isysroot $(xcrun -sdk ${PLATFORM} --show-sdk-path)"
export CFLAGS="-arch ${ARCH} $MINVERSION -isysroot $(xcrun -sdk ${PLATFORM} --show-sdk-path)"
export CXXFLAGS="-arch ${ARCH} $MINVERSION -isysroot $(xcrun -sdk ${PLATFORM} --show-sdk-path)"
export LDFLAGS="-arch ${ARCH} $MINVERSION -isysroot $(xcrun -sdk ${PLATFORM} --show-sdk-path)"

export pkg_configure_flags="$EXTRA_CONFIG"
export pkg_make_flags="V=0 $pkg_make_flags"
export pkg_prefix="$DESTDIR"
export pkg_cross="${PLATFORM}"
export pkg_cross_arch="${ARCH}"

if [ $# -gt 0 ]; then
    $@
else
    env
fi
