#!/bin/sh

################################################################################
#      This file is part of OpenELEC - http://www.openelec.tv
#      Copyright (C) 2009-2012 Stephan Raue (stephan@openelec.tv)
#
#  This Program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2, or (at your option)
#  any later version.
#
#  This Program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with OpenELEC.tv; see the file COPYING.  If not, write to
#  the Free Software Foundation, 51 Franklin Street, Suite 500, Boston, MA 02110, USA.
#  http://www.gnu.org/copyleft/gpl.html
################################################################################

. config/options $1

get_graphicdrivers

if [ "$LLVM_SUPPORT" = "yes" ]; then
  export LLVM_CONFIG="$SYSROOT_PREFIX/usr/bin/llvm-config"
  MESA_GALLIUM_LLVM="--enable-gallium-llvm"
else
  MESA_GALLIUM_LLVM="--disable-gallium-llvm"
fi

if [ "$MESA_VDPAU_SUPPORT" = "yes" ]; then
  MESA_VDPAU="--enable-vdpau"
else
  MESA_VDPAU="--disable-vdpau"
fi

if [ "$MESA_VAAPI_SUPPORT" = "yes" ]; then
  MESA_VAAPI="--enable-va"
else
  MESA_VAAPI="--disable-va"
fi

cd $PKG_BUILD

HOST_CC="$HOST_CC" \
HOST_OPT_FLAGS="$HOST_CFLAGS" \
X11_INCLUDES= \
DRI_DRIVER_INSTALL_DIR="$XORG_PATH_DRI" \
DRI_DRIVER_SEARCH_DIR="$XORG_PATH_DRI" \

XA_CONFIG="--disable-xa"
for drv in $GRAPHIC_DRIVERS; do
  [ "$drv" = "vmware" ] && \
    XA_CONFIG="--enable-xa"
done

./configure --host=$TARGET_NAME \
            --build=$HOST_NAME \
            --prefix=/usr \
            --sysconfdir=/etc \
            --localstatedir=/var \
            --disable-static \
            --enable-shared \
            --disable-debug \
            --enable-texture-float \
            --disable-selinux \
            --enable-opengl \
            --enable-glx-tls \
            --enable-driglx-direct \
            --disable-gles1 \
            --disable-gles2 \
            --disable-openvg \
            --disable-xorg \
            --disable-glu \
            --disable-osmesa \
            --disable-d3d1x \
            --disable-egl \
            --disable-gbm \
            --disable-xvmc \
            $MESA_VDPAU \
            $MESA_VAAPI \
            --disable-gallium-egl \
            --disable-gallium-gbm \
            --enable-shared-glapi \
            --enable-xcb \
            $XA_CONFIG \
            --enable-shared-dricore \
            --disable-egl \
            $MESA_GALLIUM_LLVM \
            --with-gallium-drivers="$GALLIUM_DRIVERS" \
            --with-dri-drivers="$DRI_DRIVERS" \
            --with-x \

make CC="$HOST_CC" \
     CXX="$HOST_CXX" \
     CFLAGS="$HOST_CFLAGS" \
     CXXFLAGS="$HOST_CXXFLAGS" \
     LDFLAGS="$HOST_LDFLAGS" \
     -C src/glsl builtin_compiler

cp src/glsl/builtin_compiler $ROOT/$TOOLCHAIN/bin
make -C src/glsl clean

sed -e "s#\.\/builtin_compiler#$ROOT/$TOOLCHAIN/bin/builtin_compiler#g" -i src/glsl/Makefile

make

$MAKEINSTALL
