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

LINUX=`ls -d $PKG_BUILD`

if [ -f $PROJECT_DIR/$PROJECT/$1/$1.$TARGET_ARCH.conf ]; then
  KERNEL_CFG_FILE=$PROJECT_DIR/$PROJECT/$1/$1.$TARGET_ARCH.conf
else
  KERNEL_CFG_FILE=$PKG_DIR/config/$1.$TARGET_ARCH.conf
fi

sed -i -e "s|^HOSTCC[[:space:]]*=.*$|HOSTCC = $HOST_CC|" \
       -e "s|^HOSTCXX[[:space:]]*=.*$|HOSTCXX = $HOST_CXX|" \
       -e "s|^ARCH[[:space:]]*?=.*$|ARCH = $TARGET_ARCH|" \
       -e "s|^CROSS_COMPILE[[:space:]]*?=.*$|CROSS_COMPILE = $TARGET_PREFIX|" \
       $LINUX/Makefile

cp $KERNEL_CFG_FILE $LINUX/.config
sed -i -e "s|^CONFIG_INITRAMFS_SOURCE=.*$|CONFIG_INITRAMFS_SOURCE=\"$ROOT/$BUILD/image/initramfs.cpio\"|" \
  $LINUX/.config

# disable PPP support if not enabled
  if [ ! "$PPTP_SUPPORT" = yes ]; then
    sed -i -e "s|^CONFIG_PPP=.*$|# CONFIG_PPP is not set|" $LINUX/.config
  fi

# disable swap support if not enabled
  if [ ! "$SWAP_SUPPORT" = yes ]; then
    sed -i -e "s|^CONFIG_SWAP=.*$|# CONFIG_SWAP is not set|" $LINUX/.config
  fi

# dont install some debug stuff on release builds
  if [ ! "$DEVTOOLS" = yes ]; then
    sed -i -e "s|^CONFIG_DEBUG_FS=.*$|# CONFIG_DEBUG_FS is not set|" $LINUX/.config
#    sed -i -e "s|^CONFIG_NET_SCHED=.*$|# CONFIG_NET_SCHED is not set|" $LINUX/.config
#    sed -i -e "s|^CONFIG_NET_SCH_NETEM=.*$|# CONFIG_NET_SCH_NETEM is not set|" $LINUX/.config
  fi

# copy some extra firmware to linux tree
  cp -R $PKG_DIR/firmware/* $LINUX/firmware

make -C $LINUX oldconfig
