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

CWD=`pwd`

setup_toolchain host

cd $PKG_BUILD/ncurses-host
./configure --host=$HOST_NAME \
            --build=$HOST_NAME \
            --prefix=$ROOT/$TOOLCHAIN \
            --with-shared \

make -C include
make -C progs tic

cp progs/tic $ROOT/$TOOLCHAIN/bin
cp lib/*.so* $ROOT/$TOOLCHAIN/lib

cd "$CWD"

setup_toolchain target

# causes some segmentation fault's (dialog) when compiled with gcc's link time optimization.
  strip_linker_plugin

$SCRIPTS/build toolchain

cd $PKG_BUILD
./configure --host=$TARGET_NAME \
            --build=$HOST_NAME \
            --prefix=/usr \
            --sysconfdir=/etc \
            --with-build-cc=$HOST_CC \
            --with-build-cppflags="$HOST_CPPFLAGS -I../include" \
            --with-build-cflags="$HOST_CFLAGS" \
            --with-build-ldflags="$HOST_LDFLAGS" \
            --without-cxx \
            --without-cxx-binding \
            --without-ada \
            --without-progs \
            --with-shared \
            --with-normal \
            --without-debug \
            --without-profile \
            --with-termlib \
            --without-dbmalloc \
            --without-dmalloc \
            --without-gpm \
            --disable-rpath \
            --disable-overwrite \
            --enable-database \
            --disable-big-core \
            --enable-termcap \
            --enable-getcap \
            --disable-getcap-cache \
            --disable-bsdpad \
            --without-rcs-ids \
            --enable-ext-funcs \
            --disable-const \
            --enable-no-padding \
            --disable-sigwinch \
            --disable-tcap-names \
            --without-develop \
            --disable-hard-tabs \
            --disable-xmc-glitch \
            --disable-hashmap \
            --disable-safe-sprintf \
            --disable-scroll-hints \
            --disable-widec \
            --disable-echo \
            --disable-warnings \
            --disable-assertions

make -C include
make -C ncurses
make -C panel
make -C menu
make -C form

$AR cru lib/libncurses.a obj_s/*.o

$MAKEINSTALL -C include
$MAKEINSTALL -C ncurses
$MAKEINSTALL -C panel
$MAKEINSTALL -C menu
$MAKEINSTALL -C form

cp $SYSROOT_PREFIX/usr/include/ncurses/curses.h \
   $SYSROOT_PREFIX/usr/include/ncurses/term.h \
   $SYSROOT_PREFIX/usr/include/

cp misc/ncurses-config $ROOT/$TOOLCHAIN/bin
  chmod +x $ROOT/$TOOLCHAIN/bin/ncurses-config
  $SED "s:\(['=\" ]\)/usr:\\1$SYSROOT_PREFIX/usr:g" \
    $ROOT/$TOOLCHAIN/bin/ncurses-config
