#!/bin/sh

#
# Lol Engine build script
# Usage:
#   lol-build <action> [<platform>]
#
# Where <action> is one of:
#  - bootstrap
#  - configure
#  - build
#  - check
#  - clean
#
# And <platform> is one of:
#  - linux-i386
#  - linux-amd64
#  - nacl-i386
#  - nacl-amd64
#  - ios-arm
#  - osx-amd64
#  - android-arm
#  - ps3-ppu
#  - win*-i386
#  - win*-amd64
#

set -e

action="$1"
platform="$2"

__init__()
{
    top_srcdir="$(dirname "$0")/.."
    cd "$top_srcdir"
    top_srcdir="`pwd`"

    case "${MSYSTEM}" in
        MINGW32|MINGW64)
            PATH="$PATH:./contrib/gtk-2.22.1/bin"
            M4PATH="$M4PATH:./contrib/gtk-2.22.1/share/aclocal"
            ;;
    esac
}

bootstrap()
{
    cd "$top_srcdir"
    case "$platform" in
        ios-arm)
            # No bootstrapping needed
            ;;
        android-arm)
            # No bootstrapping needed
            ;;
        *)
            PATH="$PATH" M4PATH="$M4PATH" ./bootstrap
            ;;
    esac
}

configure()
{
    cd "$top_srcdir"
    case "$platform" in
        win*-i386)
            if test "x${MSYSTEM}" = xMINGW32; then
                :
            elif i586-mingw32msvc-g++ --version >/dev/null 2>&1; then
                HOSTFLAGS=--host=i586-mingw32msvc
                BUILDFLAGS=--build=none
            elif i686-w64-mingw32-g++ --version >/dev/null 2>&1; then
                HOSTFLAGS=--host=i686-w64-mingw32
                BUILDFLAGS=--build=none
            else
                echo "Error: could not find win32 compiler" >&2
                false
            fi
            if test "x${MSYSTEM}" = xMINGW64; then
                # If using mingw64, we're not really cross-compiling
                BUILDFLAGS=
            fi
            PKG_CONFIG_PATH="$PKG_CONFIG_PATH:$PWD/contrib/gtkglarea-2.0.1/lib/pkgconfig"
            PKG_CONFIG_PATH="$PKG_CONFIG_PATH:$PWD/contrib/libcaca-0.99.beta18/lib/pkgconfig"
            LDFLAGS="$LDFLAGS -L$PWD/contrib/glew-1.7.0/lib/i686-w64-mingw32"
            LDFLAGS="$LDFLAGS -L$PWD/contrib/sdl-1.2.14/lib/i686-w64-mingw32"
            LDFLAGS="$LDFLAGS -L$PWD/contrib/sdl-image-1.2.10/lib/i686-w64-mingw32"
            LDFLAGS="$LDFLAGS -L$PWD/contrib/sdl-mixer-1.2.11/lib/i686-w64-mingw32"
            LDFLAGS="$LDFLAGS -L$PWD/contrib/gtk-2.22.1/lib"
            LDFLAGS="$LDFLAGS -L$PWD/contrib/gtk-2.22.1/bin"
            LDFLAGS="$LDFLAGS -L$PWD/contrib/gtkglarea-2.0.1/lib"
            LDFLAGS="$LDFLAGS -L$PWD/contrib/libcaca-0.99.beta18/lib/i686-w64-mingw32"
            ;;
        win*-amd64)
            if test "x${MSYSTEM}" = xMINGW64; then
                :
            elif x86_64-w64-mingw32-g++ --version >/dev/null 2>&1; then
                HOSTFLAGS=--host=x86_64-w64-mingw32
                BUILDFLAGS=--build=none
            else
                echo "Error: could not find win64 compiler" >&2
                false
            fi
            if test "x${MSYSTEM}" = xMINGW32; then
                # If using mingw32, we're not really cross-compiling
                BUILDFLAGS=
            fi
            PKG_CONFIG_PATH="$PKG_CONFIG_PATH:$PWD/contrib/gtkglarea-2.0.1/lib/pkgconfig"
            PKG_CONFIG_PATH="$PKG_CONFIG_PATH:$PWD/contrib/libcaca-0.99.beta18/lib/pkgconfig"
            LDFLAGS="$LDFLAGS -L$PWD/contrib/glew-1.7.0/lib/x86_64-w64-mingw32"
            LDFLAGS="$LDFLAGS -L$PWD/contrib/sdl-1.2.14/lib/x86_64-w64-mingw32"
            LDFLAGS="$LDFLAGS -L$PWD/contrib/sdl-image-1.2.10/lib/x86_64-w64-mingw32"
            LDFLAGS="$LDFLAGS -L$PWD/contrib/sdl-mixer-1.2.11/lib/x86_64-w64-mingw32"
            LDFLAGS="$LDFLAGS -L$PWD/contrib/libcaca-0.99.beta18/lib/x86_64-w64-mingw32"
            ;;
        *-i386)
            # Ensure we're _really_ on i386
            CXXFLAGS="$CXXFLAGS -m32"
            ;;
        *-amd64)
            # Ensure we're _really_ on amd64
            CXXFLAGS="$CXXFLAGS -m64"
            ;;
    esac
    case "$platform" in
        ios-arm)
            # No configuration needed
            ;;
        android-arm)
            cd monsterz/android
            android update project --path .
            ;;
        nacl-i386)
            ./configure CXX=i686-nacl-g++ CC=i686-nacl-gcc ac_cv_exeext=.32.nexe --host=i386 LOL_LIBS="-lppapi -lppapi_gles2 -lppapi_cpp -u _ZN2pp12CreateModuleEv"
            ;;
        nacl-amd64)
            ./configure CXX=x86_64-nacl-g++ CC=x86_64-nacl-gcc ac_cv_exeext=.64.nexe --host=x86_64 LOL_LIBS="-lppapi -lppapi_gles2 -lppapi_cpp -u _ZN2pp12CreateModuleEv"
            ;;
        ps3-ppu)
            PATH="$PATH" ./configure CXX=ppu-lv2-g++ CC=ppu-lv2-gcc ac_cv_exeext=.elf --host=powerpc
            ;;
        win*-i386|win*-amd64)
            CPPFLAGS="$CPPFLAGS -I$PWD/contrib/sdl-1.2.14/include"
            CPPFLAGS="$CPPFLAGS -I$PWD/contrib/sdl-image-1.2.10/include"
            CPPFLAGS="$CPPFLAGS -I$PWD/contrib/sdl-mixer-1.2.11/include"
            CPPFLAGS="$CPPFLAGS -I$PWD/contrib/glew-1.7.0/include/GL -DGLEW_STATIC"
            CPPFLAGS="$CPPFLAGS -I$PWD/contrib/gtk-2.22.1/lib/glib-2.0/include"
            CPPFLAGS="$CPPFLAGS -I$PWD/contrib/gtk-2.22.1/lib/gtk-2.0/include"
            CPPFLAGS="$CPPFLAGS -I$PWD/contrib/gtk-2.22.1/include/glib-2.0"
            CPPFLAGS="$CPPFLAGS -I$PWD/contrib/gtk-2.22.1/include/gtk-2.0"
            CPPFLAGS="$CPPFLAGS -I$PWD/contrib/gtk-2.22.1/include/cairo"
            CPPFLAGS="$CPPFLAGS -I$PWD/contrib/gtk-2.22.1/include/pango-1.0"
            CPPFLAGS="$CPPFLAGS -I$PWD/contrib/gtk-2.22.1/include/gdk-pixbuf-2.0"
            CPPFLAGS="$CPPFLAGS -I$PWD/contrib/gtk-2.22.1/include/atk-1.0"
            CPPFLAGS="$CPPFLAGS -I$PWD/contrib/gtkglarea-2.0.1/include"
            CPPFLAGS="$CPPFLAGS -mms-bitfields"
            LDFLAGS="$LDFLAGS -static-libgcc -static-libstdc++"
            GTK_LIBS="$GTK_LIBS -lgtkgl-2.0 -lopengl32 -lglew32 -lgdi32"
            GTK_LIBS="$GTK_LIBS -lgtk-win32-2.0 -lgdk-win32-2.0"
            GTK_LIBS="$GTK_LIBS -lglib-2.0 -lgthread-2.0 -lgobject-2.0"

            CPPFLAGS="$CPPFLAGS -I$PWD/contrib/libcaca-0.99.beta18/include -DCACA_STATIC"

            PATH="$PATH" PKG_CONFIG_PATH="$PKG_CONFIG_PATH" ./configure $HOSTFLAGS $BUILDFLAGS CPPFLAGS="$CPPFLAGS" LDFLAGS="$LDFLAGS" GTK_LIBS="$GTK_LIBS"
            ;;
        *)
            PATH="$PATH" ./configure CFLAGS="$CXXFLAGS" CXXFLAGS="$CXXFLAGS"
            ;;
    esac
}

build()
{
    cd "$top_srcdir"
    case "$platform" in
        ios-arm)
            cd monsterz/ios
            xcodebuild -configuration Release -sdk iphonesimulator4.3
            ;;
        android-arm)
            cd monsterz/android
            ndk-build
            ant compile
            ;;
        *)
            make -j6
            ;;
    esac
}

check()
{
    cd "$top_srcdir"
    case "$platform" in
        ios-arm)
            ;;
        android-arm)
            ;;
        ps3-ppu)
            ;;
        nacl-*)
            ;;
        win*-i386)
            # If neither $MSYSTEM or $DISPLAY are set, and xvfb-run
            # exists, use it to run the test suite.
            if test "x${MSYSTEM}${DISPLAY}" = x \
               && xvfb-run --help 2>&1 >/dev/null; then
                xvfb-run make check
            else
                make check
            fi
            ;;
        win*-amd64)
            # No support for Wine64 yet
            ;;
        *)
            make check
            ;;
    esac
}

clean()
{
    cd "$top_srcdir"
    case "$platform" in
        ios-arm)
            cd monsterz/ios
            xcodebuild -configuration Release -sdk iphonesimulator4.3 clean
            ;;
        android-arm)
            cd monsterz/android
            ndk-build distclean
            ant clean
            ;;
        *)
            make distclean
            ;;
    esac
}

__init__
echo "lol-build: executing action '$action' on platform '$platform'" >&2
eval "$action"