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

# disable build of GOOM until its fixed
  XBMC_VIS_GOOM="no"

if [ "$DISPLAYSERVER" = "xorg-server" ]; then
  XBMC_XORG="--enable-x11 --enable-xrandr"
else
  XBMC_XORG="--disable-x11 --disable-xrandr"
fi

if [ "$OPENGL" = "Mesa" ]; then
  XBMC_OPENGL="--enable-gl"
else
  XBMC_OPENGL="--disable-gl"
fi

if [ "$OPENGLES_SUPPORT" = yes ]; then
  XBMC_OPENGLES="--enable-gles"
else
  XBMC_OPENGLES="--disable-gles"
fi

if [ "$SDL_SUPPORT" = yes ]; then
  XBMC_SDL="--enable-sdl"
else
  XBMC_SDL="--disable-sdl"
fi

if [ "$XBMC_SCR_RSXS" = yes ]; then
  XBMC_RSXS="--enable-rsxs"
  # fix build of RSXS Screensaver support if not using libiconv
    if [ ! "$ICONV" = "libiconv" ]; then
      export jm_cv_func_gettimeofday_clobber=no
    fi
else
  XBMC_RSXS="--disable-rsxs"
fi

if [ "$XBMC_VIS_PROJECTM" = yes ]; then
  XBMC_PROJECTM="--enable-projectm"
else
  XBMC_PROJECTM="--disable-projectm"
fi

if [ "$XBMC_VIS_GOOM" = yes ]; then
  XBMC_GOOM="--enable-goom"
else
  XBMC_GOOM="--disable-goom"
fi

if [ "$ALSA_SUPPORT" = yes ]; then
  XBMC_ALSA="--enable-alsa"
else
  XBMC_ALSA="--disable-alsa"
fi

if [ "$PULSEAUDIO_SUPPORT" = yes ]; then
  XBMC_PULSEAUDIO="--enable-pulse"
else
  XBMC_PULSEAUDIO="--disable-pulse"
fi

if [ "$CEC_SUPPORT" = yes ]; then
  XBMC_CEC="--enable-libcec"
  if [ "$OPENMAX" = "bcm2835-driver" ]; then
    XBMC_CEC="$XBMC_CEC --enable-rpi-cec-api"
  fi
else
  XBMC_CEC="--disable-libcec"
fi

if [ "$JOYSTICK_SUPPORT" = yes ]; then
  XBMC_JOYSTICK="--enable-joystick"
else
  XBMC_JOYSTICK="--disable-joystick"
fi

if [ "$NONFREE_SUPPORT" = yes ]; then
  XBMC_NONFREE="--enable-non-free"
else
  XBMC_NONFREE="--disable-non-free"
fi

if [ "$AIRPLAY_SUPPORT" = yes ]; then
  XBMC_AIRPLAY="--enable-airplay"
else
  XBMC_AIRPLAY="--disable-airplay"
fi

if [ "$AIRTUNES_SUPPORT" = yes ]; then
  XBMC_AIRTUNES="--enable-airtunes"
else
  XBMC_AIRTUNES="--disable-airtunes"
fi

if [ "$DVDCSS_SUPPORT" = yes ]; then
  XBMC_DVDCSS="--enable-dvdcss"
else
  XBMC_DVDCSS="--disable-dvdcss"
fi

if [ "$ENCODER_LAME" = yes ]; then
  XBMC_LAMEENC="--enable-libmp3lame"
else
  XBMC_LAMEENC="--disable-libmp3lame"
fi

if [ "$ENCODER_VORBIS" = yes ]; then
  XBMC_VORBISENC="--enable-libvorbisenc"
else
  XBMC_VORBISENC="--disable-libvorbisenc"
fi

if [ "$BLURAY_SUPPORT" = yes ]; then
  XBMC_BLURAY="--enable-libbluray"
else
  XBMC_BLURAY="--disable-libbluray"
fi

if [ "$AVAHI_DAEMON" = yes ]; then
  XBMC_AVAHI="--enable-avahi"
else
  XBMC_AVAHI="--disable-avahi"
fi

if [ "$NFS_SUPPORT" = yes ]; then
  XBMC_NFS="--enable-nfs"
else
  XBMC_NFS="--disable-nfs"
fi

if [ "$AFP_SUPPORT" = yes ]; then
  XBMC_AFP="--enable-afpclient"
else
  XBMC_AFP="--disable-afpclient"
fi

if [ "$SAMBA_CLIENT" = yes ]; then
  XBMC_SAMBA="--enable-samba"
  export LIBS="$LIBS -ltalloc -ltdb -lwbclient"
else
  XBMC_SAMBA="--disable-samba"
fi

if [ "$WEBSERVER" = yes ]; then
  XBMC_WEBSERVER="--enable-webserver"
else
  XBMC_WEBSERVER="--disable-webserver"
fi

if [ "$OPENMAX_SUPPORT" = yes ]; then
  XBMC_OPENMAX="--enable-openmax"
  if [ "$OPENMAX" = "bcm2835-driver" ]; then
    XBMC_PLATFORM_SUPPORT="--with-platform=raspberry-pi"
    XBMC_PLAYER="--enable-player=omxplayer"
    BCM2835_INCLUDES="-I$SYSROOT_PREFIX/usr/include/interface/vcos/pthreads/ \
                      -I$SYSROOT_PREFIX/usr/include/interface/vmcs_host/linux"
    CFLAGS="$CFLAGS $BCM2835_INCLUDES"
    CXXFLAGS="$CXXFLAGS $BCM2835_INCLUDES"
  fi
else
  XBMC_OPENMAX="--disable-openmax"
fi

if [ "$VDPAU" = yes ]; then
  XBMC_VDPAU="--enable-vdpau"
else
  XBMC_VDPAU="--disable-vdpau"
fi

if [ "$VAAPI" = yes ]; then
  XBMC_VAAPI="--enable-vaapi"
else
  XBMC_VAAPI="--disable-vaapi"
fi

if [ "$XVBA" = yes ]; then
  XBMC_XVBA="--enable-xvba"
else
  XBMC_XVBA="--disable-xvba"
fi

if [ "$CRYSTALHD" = yes ]; then
  XBMC_CRYSTALHD="--enable-crystalhd"
else
  XBMC_CRYSTALHD="--disable-crystalhd"
fi

# xbmc fails to build with LTO optimization
  strip_lto

# dont use some optimizations because of build problems
  LDFLAGS=`echo $LDFLAGS | sed -e "s|-Wl,--as-needed||"`

# dont build parallel
  MAKEFLAGS=-j1

  export PYTHON_VERSION="2.7"
  export PYTHON_CPPFLAGS="-I$SYSROOT_PREFIX/usr/include/python$PYTHON_VERSION"
  export PYTHON_LDFLAGS="-L$SYSROOT_PREFIX/usr/lib/python$PYTHON_VERSION -lpython$PYTHON_VERSION"
  export PYTHON_SITE_PKG="$SYSROOT_PREFIX/usr/lib/python$PYTHON_VERSION/site-packages"
  export ac_python_version="$PYTHON_VERSION"

# setup skin dir from default skin
  SKIN_DIR="skin.`tolower $SKIN_DEFAULT`"

cd $PKG_BUILD

# adding fake Makefile for stripped skin
  mkdir -p addons/skin.confluence/media
  touch addons/skin.confluence/media/Makefile.in

# autoreconf
  BOOTSTRAP_STANDALONE=1 make -f bootstrap.mk

./configure --host=$TARGET_NAME \
            --build=$HOST_NAME \
            --with-arch=$TARGET_ARCH \
            --with-cpu=$TARGET_CPU \
            --prefix=/usr \
            --exec-prefix=/usr \
            --sysconfdir=/etc \
            --datadir=/usr/share \
            GIT_REV=`cat git.version` \
            --disable-debug \
            --disable-optimizations \
            $XBMC_OPENGL \
            $XBMC_OPENGLES \
            $XBMC_SDL \
            $XBMC_OPENMAX \
            $XBMC_VDPAU \
            $XBMC_VAAPI \
            $XBMC_CRYSTALHD \
            $XBMC_XVBA \
            --disable-vdadecoder \
            --disable-vtbdecoder \
            --disable-tegra \
            --disable-profiling \
            $XBMC_JOYSTICK \
            $XBMC_CEC \
            --enable-udev \
            --disable-libusb \
            $XBMC_GOOM \
            $XBMC_RSXS \
            $XBMC_PROJECTM \
            $XBMC_XORG \
            --disable-ccache \
            $XBMC_ALSA \
            $XBMC_PULSEAUDIO \
            --enable-rtmp \
            $XBMC_SAMBA \
            $XBMC_NFS \
            $XBMC_AFP \
            $XBMC_VORBISENC \
            --enable-ffmpeg-libvorbis \
            $XBMC_LAMEENC \
            $XBMC_DVDCSS \
            --disable-mid \
            --disable-hal \
            $XBMC_AVAHI \
            $XBMC_AIRPLAY \
            $XBMC_AIRTUNES \
            $XBMC_NONFREE \
            --disable-asap-codec \
            $XBMC_WEBSERVER \
            --enable-optical-drive \
            $XBMC_BLURAY \
            --enable-texturepacker --with-texturepacker-root="$ROOT/$TOOLCHAIN" \
            --disable-external-libraries \
            --enable-external-ffmpeg \
            $XBMC_PLATFORM_SUPPORT \
            $XBMC_PLAYER \

# setup default skin inside the sources
  sed -i -e "s|skin.confluence|$SKIN_DIR|g" xbmc/settings/Settings.h

make externals
make xbmc.bin

if [ "$DISPLAYSERVER" = "xorg-server" ]; then
  make xbmc-xrandr
fi

make -C tools/TexturePacker
cp -PR tools/TexturePacker/TexturePacker $ROOT/$TOOLCHAIN/bin
