#!/bin/sh

source ../Config.sh

pp_clean_local () {
    # fix a CVS import bug
    rm -rf drivers/video/xgi/.tmp_versions
    make mrproper
    return 0
}

pp_build_local () {
    # check if we are on x86 or have mkimage
    if [ "$PP_BUILD_ARCH" != "x86" -a ! -x "${FW_TOPDIR}/bin/mkimage" ]; then
	echo "Build u-boot first, please!"
	return 1
    fi

    # determine JFFS2 option
    if [ "$PP_BUILD_ARCH" = "arm" -a "$PP_FEAT_JFFS2_ROOTFS" = "1" ]; then
	JFFS2OPT="jffs2_"
    fi

    # determine config
    if [ "$PP_FW_TYPE" = "production" ]; then
	cfg="${PP_BOARD}_${PP_SUBBOARD}_production_defconfig"
    elif [ "$PP_FEAT_WLAN" = "1" ]; then
	cfg="${PP_BOARD}_${PP_SUBBOARD}_wlan_${JFFS2OPT}defconfig"
    elif [ "$PP_PRODUCT" = "lara_bmc_test" ]; then
	cfg="${PP_BOARD}_${PP_SUBBOARD}_ipmi_${JFFS2OPT}defconfig"
    else
	cfg="${PP_BOARD}_${PP_SUBBOARD}_${JFFS2OPT}defconfig"
    fi

    # determine config directory
    if [ "$PP_BUILD_ARCH" = "powerpc" ]; then
	cfg_dir="arch/ppc/configs"
    elif [ "$PP_BUILD_ARCH" = "arm" ]; then
	cfg_dir="arch/arm/configs"
    elif [ "$PP_BUILD_ARCH" = "x86" ]; then
	cfg_dir="arch/i386/configs"
    else
	echo "Unknown PP_BUILD_ARCH: '$PP_BUILD_ARCH'"
	return 1
    fi

    cfg_file="${cfg_dir}/${cfg}"

    # generate default config file
    echo -n "Using ${cfg} for peppercon_defconfig ... "
    if [ -f "${cfg_file}" ]; then
	echo
	cp -f "${cfg_file}" "${cfg_dir}/peppercon_defconfig"
	# include all kernel symbols in kernel for non-final builds
	if [ "$PP_BUILD_TYPE" != "final" ]; then
	    echo CONFIG_KALLSYMS=y >> "${cfg_dir}/peppercon_defconfig"
	fi
	# enable profiling for debug/profile build types
	if [ "$PP_BUILD_TYPE" = "debugopt" -o "$PP_BUILD_TYPE" = "profile" ] ; then
	    echo CONFIG_PROFILING=y >> "${cfg_dir}/peppercon_defconfig"
	    echo CONFIG_OPROFILE=m >> "${cfg_dir}/peppercon_defconfig"
	fi
    else
	echo "not found."
	return 1
    fi

    # determine kernel image target
    [ "$PP_BUILD_ARCH" = "x86" ] && kernel_image=bzImage || kernel_image=uImage

    # make / install
    make peppercon_defconfig oldconfig
    make ${kernel_image} modules modules_install pp_install
}

pp_do "$@"
