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

if [ "$DISPLAYSERVER" = "xorg-server" ]; then
  SDL_X11="--enable-video --enable-x11-shared --disable-video-x11-dgamouse"
  SDL_X11="$SDL_X11 --disable-video-x11-xinerama --disable-video-x11-xme"
  SDL_X11="$SDL_X11 --enable-video-x11-xrandr --enable-video-x11"
  SDL_X11="$SDL_X11 --disable-video-x11-vm --disable-video-x11-xv --with-x"
else
  SDL_X11="--disable-video --disable-x11-shared --disable-video-x11-dgamouse"
  SDL_X11="$SDL_X11 --disable-video-x11-xinerama --disable-video-x11-xme"
  SDL_X11="$SDL_X11 --disable-video-x11-xrandr --disable-video-x11"
  SDL_X11="$SDL_X11 --disable-video-x11-vm --disable-video-x11-xv --without-x"
fi

if [ "$OPENGL" = "Mesa" ]; then
  SDL_OPENGL="--enable-video-opengl"
else
  SDL_OPENGL="--disable-video-opengl"
fi

if [ "$PULSEAUDIO_SUPPORT" = yes ]; then
  SDL_PULSEAUDIO="--enable-pulseaudio --enable-pulseaudio-shared"
else
  SDL_PULSEAUDIO="--disable-pulseaudio --disable-pulseaudio-shared"
fi

cd $PKG_BUILD

rm -f acinclude/alsa.m4
# Skip autoheader because there is a problem with AC_DEFINE's in the configure.in of SDL 1.2.14.
# Added include directory 'acinclude' because SDL 1.2.14 has no Makefile.am in which to specify it.
  AUTOHEADER="true" autoreconf --verbose --install --force -I $SYSROOT_PREFIX/usr/share/aclocal -I acinclude

mkdir -p .build-target && cd .build-target
../configure --host=$TARGET_NAME \
             --build=$HOST_NAME \
             --prefix=/usr \
             --sysconfdir=/etc \
             --localstatedir=/var \
             --enable-shared \
             --disable-static \
             --disable-rpath \
             --enable-libc \
             --enable-audio \
             --enable-events \
             --enable-joystick \
             --enable-cdrom \
             --enable-threads \
             --enable-timers \
             --enable-file \
             --enable-loadso \
             --enable-cpuinfo \
             --enable-assembly \
             --disable-oss \
             --enable-alsa \
             --disable-alsatest \
             --enable-alsa-shared \
             --with-alsa-prefix=$SYSROOT_PREFIX/usr/lib \
             --with-alsa-inc-prefix=$SYSROOT_PREFIX/usr/include \
             --disable-esd \
             --disable-esdtest \
             --disable-esd-shared \
             $SDL_PULSEAUDIO \
             --disable-arts \
             --disable-arts-shared \
             --disable-nas \
             --disable-diskaudio \
             --disable-dummyaudio \
             --disable-mintaudio \
             --enable-nasm \
             --disable-altivec \
             --disable-ipod \
             $SDL_X11 \
             $SDL_OPENGL \
             --disable-video-nanox \
             --disable-nanox-debug \
             --disable-nanox-share-memory \
             --disable-nanox-direct-fb \
             --disable-dga \
             --disable-video-dga \
             --disable-video-photon \
             --disable-video-carbon \
             --disable-video-cocoa \
             --disable-video-fbcon \
             --disable-video-directfb \
             --disable-video-ps2gs \
             --disable-video-ggi \
             --disable-video-svga \
             --disable-video-vgl \
             --disable-video-wscons \
             --disable-video-aalib \
             --disable-video-qtopia \
             --disable-video-picogui \
             --disable-video-dummy \
             --disable-osmesa-shared \
             --enable-input-events \
             --disable-input-tslib \
             --disable-pth \
             --enable-pthreads \
             --enable-pthread-sem \
             --disable-stdio-redirect \
             --disable-directx \
             --enable-sdl-dlopen \
             --disable-atari-ldg \
             --disable-clock_gettime \

make

$MAKEINSTALL

mkdir -p $ROOT/$TOOLCHAIN/bin
  cp $SYSROOT_PREFIX/usr/bin/sdl-config $ROOT/$TOOLCHAIN/bin
  $SED "s:\(['= ]\)/usr:\\1$SYSROOT_PREFIX/usr:g" $ROOT/$TOOLCHAIN/bin/sdl-config

