#!/bin/sh

. config/options $1

get_graphicdrivers

# dont use gold linker because of compiling issues
  strip_gold
  strip_linker_plugin
  strip_lto # Mesa fails to build with LTO optimization

# TODO:
  LDFLAGS=""

MESA_CONFIG=" --disable-gallium --disable-gallium-nouveau --disable-gallium-i915 --disable-gallium-i965"
MESA_CONFIG="$MESA_CONFIG --disable-gallium-radeon --disable-gallium-r600 --disable-gallium-svga --disable-gallium-swrast"

for drv in $GRAPHIC_DRIVERS; do
  [ "$drv" = i915 ] && \
    MESA_CONFIG=`echo $MESA_CONFIG | sed -e 's/disable-gallium-intel/disable-gallium-intel/'`

  [ "$drv" = i965 ] && \
    MESA_CONFIG=`echo $MESA_CONFIG | sed -e 's/disable-gallium-intel/disable-gallium-intel/'`

  [ "$drv" = radeon ] && \
    MESA_CONFIG=`echo $MESA_CONFIG | sed -e 's/disable-gallium-radeon/disable-gallium-radeon/'`

  [ "$drv" = r600 ] && \
    MESA_CONFIG=`echo $MESA_CONFIG | sed -e 's/disable-gallium-r600/disable-gallium-r600/'`

  [ "$drv" = nouveau ] && \
    MESA_CONFIG=`echo $MESA_CONFIG | sed -e 's/disable-gallium/enable-gallium/'` && \
    MESA_CONFIG=`echo $MESA_CONFIG | sed -e 's/disable-gallium-nouveau/enable-gallium-nouveau/'` && \
    MESA_CONFIG="$MESA_CONFIG --with-state-trackers=dri,glx,xorg"

  [ "$drv" = vmware ] && \
    MESA_CONFIG=`echo $MESA_CONFIG | sed -e 's/disable-gallium/enable-gallium/'` && \
    MESA_CONFIG=`echo $MESA_CONFIG | sed -e 's/disable-gallium-svga/enable-gallium-svga/'` && \
    MESA_CONFIG="$MESA_CONFIG --with-state-trackers=dri,glx,xorg"
done

cd $PKG_BUILD

make clean

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" \
./configure --host=$TARGET_NAME \
            --build=$HOST_NAME \
            --prefix=/usr \
            --sysconfdir=/etc \
            --localstatedir=/var \
            --disable-static \
            --enable-shared \
            --disable-debug \
            --disable-selinux \
            --enable-xcb \
            --disable-glx-tls \
            --enable-driglx-direct \
            --disable-egl \
            --enable-glu \
            --disable-gl-osmesa \
            --disable-glut \
            --disable-glw \
            --disable-motif \
            --with-driver=dri \
            --with-dri-drivers="$MESA_DRIVERS" \
            --with-dri-driverdir="$XORG_PATH_DRI" \
            --with-xorg-driver-dir="$XORG_PATH_DRIVERS" \
            $MESA_CONFIG \

make

$MAKEINSTALL -C src/mesa
$MAKEINSTALL -C src/glu
