#!/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
#  - raspi-arm
#

set -e

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

###############################################################################
# Initialisation code
#
#  - sets the top_srcdir variable
#  - sets the LOL_PARALLEL variable
#  - fix PATH and MKPATH if necessary
#
__init__()
{
    top_srcdir="$(dirname "$0")/.."
    cd "$top_srcdir"
    top_srcdir="`pwd`"

    # Try to count CPU cores. First obvious try: /proc/cpuinfo on Linux
    case "$cpu_count" in
      [1-9]|[1-9][0-9]|[1-9][0-9][0-9]) ;;
      *) if [ -r "/proc/cpuinfo" ]; then
           cpu_count="$(grep -c '^processor\>' /proc/cpuinfo 2>/dev/null || true)"
         fi ;;
    esac
    # If that didn't work, try sysctl (for OS X machines)
    case "$cpu_count" in
      [1-9]|[1-9][0-9]|[1-9][0-9][0-9]) ;;
      *) cpu_count="$(sysctl -n hw.ncpu 2>/dev/null || true)" ;;
    esac
    # Otherwise, CPUInfo.exe (from Xoreax's XGE) may help on Windows
    case "$cpu_count" in
      [1-9]|[1-9][0-9]|[1-9][0-9][0-9]) ;;
      *) cpu_count="$(CPUInfo.exe | sed -ne 's/CPU Cores.* \([0-9][0-9]*\)/\1/p')" ;;
    esac
    # Otherwise, Windows may define NUMBER_OF_PROCESSORS
    case "$cpu_count" in
      [1-9]|[1-9][0-9]|[1-9][0-9][0-9]) ;;
      *) cpu_count="$NUMBER_OF_PROCESSORS" ;;
    esac
    # Otherwise, be conservative and assume 1 CPU core
    case "$cpu_count" in
      [1-9]|[1-9][0-9]|[1-9][0-9][0-9]) ;;
      *) cpu_count=1 ;;
    esac
    # Now decide how many parallel threads to launch
    case "$cpu_count" in
      1) LOL_PARALLEL=1 ;;
      2) LOL_PARALLEL=3 ;;
      *) LOL_PARALLEL="$(expr $cpu_count '*' 3 / 2)" ;;
    esac

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

    # Ensure we don’t launch weird Windows binaries when cross-compiling
    WINEDLLOVERRIDES="winemenubuilder.exe,wineboot.exe,explorer.exe,winedbg.exe=d"
    export WINEDLLOVERRIDES

    # Ensure SDL does not redirect stdout/stderr to local files
    SDL_STDIO_REDIRECT=0
    export SDL_STDIO_REDIRECT
}

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

configure()
{
    cd "$top_srcdir"
    case "$platform" in
        android-arm)
            PKG_CONFIG_PATH="$PKG_CONFIG_PATH:$PWD/external/libcaca-0.99.beta18/lib/pkgconfig"
            LDFLAGS="$LDFLAGS -L$PWD/external/libcaca-0.99.beta18/lib/arm-linux-androideabi -lz"
            CPPFLAGS="$CPPFLAGS -I$PWD/external/libcaca-0.99.beta18/include -DCACA_STATIC"
            ;;
        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/external/gtkglarea-2.0.1/lib/pkgconfig"
            PKG_CONFIG_PATH="$PKG_CONFIG_PATH:$PWD/external/libcaca-0.99.beta18/lib/pkgconfig"
            LDFLAGS="$LDFLAGS -L$PWD/external/glew-1.9.0/lib/i686-w64-mingw32"
            LDFLAGS="$LDFLAGS -L$PWD/external/sdl-1.2.15/lib/i686-w64-mingw32"
            LDFLAGS="$LDFLAGS -L$PWD/external/sdl-image-1.2.12/lib/i686-w64-mingw32"
            LDFLAGS="$LDFLAGS -L$PWD/external/sdl-mixer-1.2.12/lib/i686-w64-mingw32"
            LDFLAGS="$LDFLAGS -L$PWD/external/gtk-2.22.1/lib"
            LDFLAGS="$LDFLAGS -L$PWD/external/gtk-2.22.1/bin"
            LDFLAGS="$LDFLAGS -L$PWD/external/gtkglarea-2.0.1/lib"
            LDFLAGS="$LDFLAGS -L$PWD/external/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/external/gtkglarea-2.0.1/lib/pkgconfig"
            PKG_CONFIG_PATH="$PKG_CONFIG_PATH:$PWD/external/libcaca-0.99.beta18/lib/pkgconfig"
            LDFLAGS="$LDFLAGS -L$PWD/external/glew-1.9.0/lib/x86_64-w64-mingw32"
            LDFLAGS="$LDFLAGS -L$PWD/external/sdl-1.2.15/lib/x86_64-w64-mingw32"
            LDFLAGS="$LDFLAGS -L$PWD/external/sdl-image-1.2.12/lib/x86_64-msvc"
            LDFLAGS="$LDFLAGS -L$PWD/external/sdl-mixer-1.2.12/lib/x86_64-msvc"
            LDFLAGS="$LDFLAGS -L$PWD/external/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)
            CPPFLAGS="$CPPFLAGS -Wno-psabi -I$ANDROID_NDK_ROOT/sources/cxx-stl/stlport/stlport -fpic -fomit-frame-pointer -fno-strict-aliasing -finline-limit=64"
            CFLAGS="$CFLAGS -march=armv5te -mtune=xscale -msoft-float -mthumb"
            CXXFLAGS="$CXXFLAGS -march=armv5te -mtune=xscale -msoft-float -mthumb -fno-rtti -fno-exceptions"
            LOL_LIBS="$LOL_LIBS -L$ANDROID_NDK_ROOT/sources/cxx-stl/stlport/libs/armeabi -lstlport_shared -lm -fpic -XCClinker -shared"
            PKG_CONFIG_PATH="$PKG_CONFIG_PATH" ./configure --host=arm-linux-androideabi ac_cv_exeext=.so CPPFLAGS="$CPPFLAGS" CFLAGS="$CFLAGS" CXXFLAGS="$CXXFLAGS" LDFLAGS="$LDFLAGS" LOL_LIBS="$LOL_LIBS"
            # FIXME: is this needed?
            # ant debug
            # ant debug install
            # ant clean
            ;;
        raspi-arm)
            ./configure --host=arm-bcm2708hardfp-linux-gnueabi CPPFLAGS="-I$RASPI_SDK_ROOT/firmware/opt/vc/include -I$RASPI_SDK_ROOT/firmware/opt/vc/include/interface/vcos/pthreads -I$RASPI_SDK_ROOT/chroot/usr/include" LDFLAGS="-L$RASPI_SDK_ROOT/firmware/opt/vc/lib -L$RASPI_SDK_ROOT/chroot/lib/arm-linux-gnueabihf -Wl,-rpath-link -Wl,$RASPI_SDK_ROOT/chroot/lib/arm-linux-gnueabihf -L$RASPI_SDK_ROOT/chroot/usr/lib/arm-linux-gnueabihf -Wl,-rpath-link -Wl,$RASPI_SDK_ROOT/chroot/usr/lib/arm-linux-gnueabihf -Wl,--unresolved-symbols=ignore-in-shared-libs"
            ;;
        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/external/sdl-1.2.15/include"
            CPPFLAGS="$CPPFLAGS -I$PWD/external/sdl-image-1.2.12/include"
            CPPFLAGS="$CPPFLAGS -I$PWD/external/sdl-mixer-1.2.12/include"
            CPPFLAGS="$CPPFLAGS -I$PWD/external/glew-1.9.0/include/GL -DGLEW_STATIC"
            CPPFLAGS="$CPPFLAGS -I$PWD/external/gtk-2.22.1/lib/glib-2.0/include"
            CPPFLAGS="$CPPFLAGS -I$PWD/external/gtk-2.22.1/lib/gtk-2.0/include"
            CPPFLAGS="$CPPFLAGS -I$PWD/external/gtk-2.22.1/include/glib-2.0"
            CPPFLAGS="$CPPFLAGS -I$PWD/external/gtk-2.22.1/include/gtk-2.0"
            CPPFLAGS="$CPPFLAGS -I$PWD/external/gtk-2.22.1/include/cairo"
            CPPFLAGS="$CPPFLAGS -I$PWD/external/gtk-2.22.1/include/pango-1.0"
            CPPFLAGS="$CPPFLAGS -I$PWD/external/gtk-2.22.1/include/gdk-pixbuf-2.0"
            CPPFLAGS="$CPPFLAGS -I$PWD/external/gtk-2.22.1/include/atk-1.0"
            CPPFLAGS="$CPPFLAGS -I$PWD/external/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/external/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
            ;;
        *)
            make -j$LOL_PARALLEL
            ;;
    esac
}

check()
{
    cd "$top_srcdir"
    case "$platform" in
        ios-arm)
            ;;
        android-arm)
            ;;
        raspi-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
            ;;
        *)
            make distclean
            ;;
    esac
}

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