# --- SDE-COPYRIGHT-NOTE-BEGIN ---
# This copyright note is auto-generated by ./scripts/Create-CopyPatch.
#
# Filename: package/.../linux/linux.conf
# Copyright (C) 2007 - 2010 The OpenSDE Project
# Copyright (C) 2004 - 2006 The T2 SDE Project
# Copyright (C) 1998 - 2003 Clifford Wolf
#
# More information can be found in the files COPYING and README.
#
# 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; version 2 of the License. A copy of the
# GNU General Public License can be found in the file COPYING.
# --- SDE-COPYRIGHT-NOTE-END ---

. $base/package/*/linux*/linux-conf.in

main_lx() {
	lx_patch
	lx_config

	hook_eval premake

	# we need to rerun the oldconfig since the used .config
	# might not match the one used in the last oldconfig run
	# (e.g. the nomods is run last - and normally the module
	# one is used)
	yes '' | eval $MAKE $makeopt oldconfig > /dev/null

	kerneldir="$PWD"
	moduledir="$root/lib/modules/${lx_kernelrelease}"

	echo "Cleanup kernel ..."
	eval $MAKE $makeopt clean > /dev/null

	cp -vf .config $root/boot/kconfig_${lx_kernelrelease}

	if [ -z "$SDEDEBUG_LINUX_SUBMODULES_ONLY" ] ; then

	var_append makeopt ' ' vmlinux
	if grep -q "CONFIG_MODULES=y" .config ; then
		var_append makeopt ' ' modules
		var_append makeinstopt ' ' "INSTALL_MOD_PATH=$root DEPMOD=/bin/true modules_install"
	else
		makeinstopt=
	fi

	case "$lx_cpu" in
	    x86|i386|x86_64)
		var_append makeopt ' ' bzImage ;;
	    mips)
		var_append makeopt ' ' vmlinux.ecoff ;;
	    ppc|sh)
		var_append makeopt ' ' zImage ;;
	    sparc*)
		var_append makeopt ' ' image ;;
	esac

		echo "Building the kernel ..."
		eval $MAKE $makeopt
		cp -vf System.map $root/boot/System.map_${lx_kernelrelease}

		hook_eval inmake

		[ "$makeinstopt" ] && eval $MAKE $makeinstopt

		echo "Installing the kernel ..."
		# for debugging with oprofile, also used to boot on some arches
		cp -vf vmlinux $root/boot/vmlinux_${lx_kernelrelease}
		case "$lx_cpu" in
		  x86|i386|x86_64)
                	cp -vf arch/${lx_cpu}/boot/bzImage $root/boot/vmlinuz_${lx_kernelrelease}
			;;
		  alpha)
			gzip < vmlinux > $root/boot/vmlinux_${lx_kernelrelease}.gz
			;;
		  mips)
			cp -vf vmlinux.ecoff $root/boot/vmlinux_${lx_kernelrelease}.ecoff
			;;
		  ppc)
			for x in arch/ppc/boot/images/zImage.* ; do
			  cp -v $x $root/boot/${x/*\//}-${lx_kernelrelease}
			done
			;;
		  sh)
			cp -vf arch/sh/boot/zImage $root/boot/vmlinux_${lx_kernelrelease}
			;;
		  sparc*)
			cp -vf arch/$lx_cpu/boot/image $root/boot/vmlinux_${lx_kernelrelease}
			;;
		esac
	else
		eval $MAKE $makeopt scripts
		touch $root/boot/System.map_${lx_kernelrelease}
		mkdir -p $moduledir
		ln -sf $PWD $moduledir/build
	fi

	hook_eval postmake

	# iterate over the packages that provide 3rd party linux kernel modules
	if grep -q "CONFIG_MODULES=y" .config ; then
		module_error=0
		echo_status "Registered 3rd party kernel modules:"
	  	for x in $lx_3rd_party_modules ; do
		  echo "Sub-building 3rd party module package $x ..."
		  pushd $builddir
		  # ugly, bash does not abort properly when we have this in a if
		  # and without it the return 1 aborts immediatly ... :-(
		  set +e
		  build_package $x postlinux.conf
		  error=$?
		  set -e
		  if [ $error -ne 0 ]; then
			echo_warning "Failed building $x."
			module_error=1
		  fi
		  echo "Done building $x"
		  popd
		done

		# maybe fail hard
		if ! atstage rebuild && [ $module_error = 1 ]; then
			abort "At least one of the 3rd party kernel modules failed!"
		fi

		echo "Running 'depmod -ae -b ${root:-/} -q -F /boot/System.map ${lx_kernelrelease}' ..."
		depmod -ae -b ${root:-/} -q -F $root/boot/System.map_${lx_kernelrelease} ${lx_kernelrelease}

		# due static depmod :-(
		for x in $moduledir/modules.*
		do [ -f $x ] && add_flist $x; done

		echo "Fixing /lib/modules/${ver}/build and source symlinks ..."
		for x in build source; do
			rm -f $root/lib/modules/${lx_kernelrelease}/$x
			ln -snf ../../../usr/src/linux-${lx_kernelrelease} \
				$root/lib/modules/${lx_kernelrelease}/$x
		done

		# if module versions is enabled we need to copy 'Module.symvers'
		# to be able to build external 3rd party kernel modules without
		# rebuilding the whole kernel using the linux-src package
		if grep -q "CONFIG_MODVERSIONS=y" .config && pkginstalled linux-src ; then
			echo "Copying Module.symvers -> /lib/modules/${lx_kernelrelease}/build/"
			cp -v Module.symvers $root/lib/modules/${lx_kernelrelease}/build/
		fi
	fi

}

custmain="main_lx"
autopatch=0