# --- SDE-COPYRIGHT-NOTE-BEGIN ---
# This copyright note is auto-generated by ./scripts/Create-CopyPatch.
# 
# Filename: package/.../linux24/linux-conf.in
# Copyright (C) 2006 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 ---

treever=${pkg/linux/} ; treever=${treever/-*/}

[ "$lx_cpu" ] ||
# powerpc and mips share the same arch for 32/64, maybe more follow
lx_cpu=`echo $arch | arch2uname | sed -e s/ppc.*/powerpc/ -e s/mips64/mips/`
lx_extraversion=""
lx_kernelrelease=""
lx_customconf="\$base/config/$config/linux.cfg"

# inject a possible pre-/ post-release patch
# we filter out .tar`s to avoid trouble with the linuxsh kernel drop-in trees
var_insert patchfiles " " "`match_source_file patch-.* | grep -v '\.tar\.'`"

#makeopt=`echo $makeopt | sed -n 's/.*\(-j[^ ]*\).*/\1/p'`
makeopt=""
# we need +1 to really saturate the CPUs during the kernel build
[ $SDECFG_PARALLEL_MAX -gt 1 ] &&
	var_append makeopt ' ' -j$(( $SDECFG_PARALLEL_MAX + 1 ))
var_append makeopt ' ' "ARCH=$lx_cpu CROSS_COMPILE=$archprefix KCC=$KCC"
makeinstopt="$makeopt"

auto_config ()
{
	if [ -f $base/architecture/$arch/kernel$treever.conf.sh ] ; then
		echo "  using: architecture/$arch/kernel$treever.conf.sh"
		. $base/architecture/$arch/kernel$treever.conf.sh > .config
	elif [ -f $base/architecture/$arch/kernel$treever.conf.m4 ] ; then
		echo "  using: architecture/$arch/kernel$treever.conf.m4"
		m4 -I $base/architecture/$arch -I $base/architecture/share \
		   $base/architecture/$arch/kernel$treever.conf.m4 > .config
	elif [ -f $base/architecture/$arch/kernel$treever.conf ] ; then
		echo "  using: architecture/$arch/kernel$treever.conf"
		cp $base/architecture/$arch/kernel$treever.conf .config
	elif [ -f $base/architecture/$arch/kernel.conf.sh ] ; then
		echo "  using: architecture/$arch/kernel.conf.sh"
		. $base/architecture/$arch/kernel.conf.sh > .config
	elif [ -f $base/architecture/$arch/kernel.conf.m4 ] ; then
		echo "  using: architecture/$arch/kernel.conf.m4"
		m4 -I $base/architecture/$arch -I $base/architecture/share \
		   $base/architecture/$arch/kernel.conf.m4 > .config
	elif [ -f $base/architecture/$arch/kernel.conf ] ; then
		echo "  using: architecture/$arch/kernel.conf"
		cp $base/architecture/$arch/kernel.conf .config
	else
		echo "  using: no SDE kernel config found"
		cp arch/$lx_cpu/defconfig .config
	fi
	cp .config{,.1}

	# all modules needs to be first so modules can be disabled by i.e.
	# the targets later
	echo "Enabling all modules ..."
	yes '' | eval $MAKE $makeopt no2modconfig > /dev/null ; cp .config{,.2}

	if [ -f $base/target/$target/kernel$treever.conf.sh ] ; then
		confscripts="$base/target/$target/kernel$treever.conf.sh $confscripts"
	elif [ -f $base/target/$target/kernel.conf.sh ] ; then
		confscripts="$base/target/$target/kernel.conf.sh $confscripts"
	fi

	for x in $confscripts ; do
		echo "  running: $x"
		sh $x .config
	done
	cp .config{,.3}

	# merge various text/plain config files
	for x in $base/config/$config/linux.cfg \
	         $base/target/$target/kernel${treever}.conf \
	         $base/target/$target/kernel.conf ; do
	   if [ -f $x ] ; then
		echo "  merging: '$x'"
		tag="$(sed '/CONFIG_/ ! d; s,.*CONFIG_\([^ =]*\).*,\1,' \
			$x | tr '\n' '|')"
		egrep -v "\bCONFIG_($tag)\b" < .config > .config.4
		sed 's,\(CONFIG_.*\)=n,# \1 is not set,' \
			$x >> .config.4
		cp .config{.4,}
	   fi
	done

	# create a valid .config
	yes '' | eval $MAKE $makeopt oldconfig > /dev/null ; cp .config{,.5}

	# last disable broken stuff
	rm -f /tmp/$$.sed
	list="CONFIG_THIS_DOES_NOT_EXIST"
	for x in $pkg_linux_brokenfiles ; do
	    if [ -f "$x" ] ; then
		echo "Disable broken file: $x"
		list="$list `tr ' ' '\t' < $x | cut -f1 | grep '^CONFIG_'`"
            fi
	done
	for x in $list ; do
		echo "s,^$x=.\$,# $x is not set,;" >> /tmp/$$.sed
	done

	sed -f /tmp/$$.sed < .config > .config.6
	cp .config{.6,} ; rm -f /tmp/$$.sed

	# create a valid .config (dependencies might need to be disabled)
	yes '' | eval $MAKE $makeopt oldconfig > /dev/null

	# save final config
	cp .config{,_modules}

	echo "Creating config without modules ...."
	sed "s,\(CONFIG_.*\)=m,# \1 is not set," .config > .config_new
	mv .config_new .config
	# create a valid .config (dependencies might need to be disabled)
	yes '' | eval $MAKE $makeopt oldconfig > /dev/null
	mv .config .config_nomods

	# which .config to use?
	if [ "$SDECFG_PKG_LINUX_CONFIG_STYLE" = "modules" ] ; then
		cp .config{_modules,}
	else
		cp .config{_nomods,}
	fi
}

lx_grabextraversion () {
	local ev
	ev=$( sed -n -e 's,^[ \t]*EXTRAVERSION[ \t]*=[ \t]*\([^ \t]*\),\1,p' Makefile | tail -n 1 )
	if [ "$ev" ]; then
		lx_extraversion="${lx_extraversion}${ev}"
		# keep intact but commented since the second EXTRAVERSION
		# definition, and clean the first.
		sed -i -e 's,^\([ \t]*EXTRAVERSION[ \t]*=.*\),#\1,g' \
		       -e 's,^#\(EXTRAVERSION =\).*,\1,' Makefile
	fi
}
lx_injectextraversion () {
	local version_header=linux/version.h
	local rerun=

	if [ -f include/linux/utsrelease.h ]; then
		version_header=linux/utsrelease.h
	fi

	# inject final EXTRAVERSION into Makefile
	sed -i -e "s,^\([ \t]*EXTRAVERSION[ \t]*\)=.*,\1= ${lx_extraversion},g" Makefile

	# update version.h - we only do this, because some other freaky
	# projects like rsbac change EXTRAVERSION in other Makefiles ...
	eval $MAKE $makeopt include/$version_header | grep -q "is up to date" && rerun=1
	if [ "$rerun" ] ; then
		echo "WARNING: Your system's timer resolution is too low ..."
		sleep 1 ; touch Makefile
		eval $MAKE $makeopt include/$version_header
	fi

	# get kernel_release
	rerun=1
	while true; do
		lx_kernelrelease="$( echo -e "#include <$version_header>\nUTS_RELEASE" |
		                     gcc -E -I./include - | tail -n 1 | cut -d '"' -f 2 )"
		if [ -z "$lx_kernelrelease" -a "$rerun" ]; then
			$MAKE $makeopt .kernelrelease include/$version_header
			rerun= ; continue
		fi
		break
	done

	[ $lx_kernelrelease ] || abort "Could not grab final kernel version!"
}

lx_filter_patch ()
{
	pref=`sed -n '/^-EXTRAVERSION =/ {
	              s/^-EXTRAVERSION =//p
	              q }' $1`
	sed -e "s/^-EXTRAVERSION =.*/-EXTRAVERSION =/" \
	    -e "s/^+EXTRAVERSION = *$pref/+EXTRAVERSION = /" $1
}

lx_patch ()
{
	echo "Generic linux patching ..."

	# grab extraversion from vanilla
	lx_grabextraversion

	hook_eval prepatch
	apply_patchfiles lx_grabextraversion lx_filter_patch
	hook_eval postpatch

	# some arches (sh64 at the time of writing) have a "defect" Makefile
	# and do not work without a .config ...
	touch .config

	echo "Redefining VERSION  ..."
	lx_extraversion="${lx_extraversion}-dist"
	lx_injectextraversion
	echo "... linux source patching for ${lx_extraversion} finished."

	echo "Correcting user and permissions ..."
	chown -R root:root . ; chmod -R u=rwX,go=rX .

	if [[ $treever = 24* ]] ; then
		echo "Create symlinks and a few headers for <$lx_cpu> ... "
		eval $MAKE $makeopt symlinks
		cp $base/package/*/linux24/autoconf.h include/linux/
		touch include/linux/modversions.h
	elif [ "$lx_kernelrelease" != "UTS_RELEASE" ]; then
		echo "Create symlinks and a few headers for <$lx_cpu> ... "
		eval $MAKE $makeopt include/asm
	fi

	echo "Clean up the *.orig and *~ files ... "
	rm -f .config.old
	find -name '*.orig' -o -name '*~' | xargs rm -f
}

lx_config() {
	echo "Generic linux configuration ..."
	hook_eval preconf
	if [ "$SDECFG_PKG_LINUX_CONFIG_STYLE" = none ] ; then
		echo "Using $lx_customconf."
		echo "Since automatic generation is disabled ..."
		eval cp -v $lx_customconf .config || true
	else
		echo "Automatically creating default configuration ...."
		auto_config
	fi

	if [ "$lx_kernelrelease" == "UTS_RELEASE" ]; then
		# we have to run make prepare
		eval $MAKE $makeopt prepare
	fi

	# regrab the version
	lx_injectextraversion

	[ "$lx_kernelrelease" != "UTS_RELEASE" ] || 
		abort "Unable to detect kernel release version!"
	echo "... configuration of $lx_kernelrelease finished!"
}

pkg_linux_brokenfiles="$base/architecture/$arch/kernel$treever-disable.lst \
	$base/package/*/linux$treever/disable-broken.lst \
	$pkg_linux_brokenfiles"