#!/bin/sh
# --- SDE-COPYRIGHT-NOTE-BEGIN ---
# This copyright note is auto-generated by ./scripts/Create-CopyPatch.
# 
# Filename: package/.../uclibc/functions.in
# Copyright (C) 2006 The OpenSDE Project
# Copyright (C) 2004 - 2006 The T2 SDE Project
# 
# 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 ---

# $conffiles is a list of patters of the form
# ^[XO-] <pattern> [<value>]
# X means enable, O disable, and - removes it
# pattern can use '*' as a wildcard for [^ #]
# optionaly can set a value different that y
apply_conffiles() {
	local file= rule=
	local action= prerule= value= x=

	if [ ! -f $builddir/config.sed ]; then
	for file in $conffiles $extraconffiles; do if [ -f "$file" ]; then
		sed -e '/^[ \t]*$/d;' -e '/^[ \t]*#.*/d;' "$file" |
		while read action prerule value x; do
			rule="$(echo $prerule | sed 's/\*/[^ #]*/')"
			rule="^\(# \)\?\($rule\)[= ].*\$"
			
			if [ -z "$value" ]; then
				value='y'
			fi
			case "$action" in
				X)	echo "apply_conffiles: rule $prerule=$value."
					echo "s,$rule,\2=$value,g" >> $builddir/config.sed
					;;
				O)	echo "apply_conffiles: rule unset $prerule."
					echo "s,$rule,# \2 is not set,g" >> $builddir/config.sed
					;;
				-)	echo "apply_conffiles: rule remove $prerule."
					echo "s,$rule,,g" >> $builddir/config.sed
					;;
				*)
					abort "apply_conffiles: bad rule $action $prerule $value"
					;;
			esac
		done
	fi ; done
	fi
	sed -f $builddir/config.sed .config > $1
}

# get default config, and filter considering <n>
# levels, because new options can appear and other disappear
auto_config() {
	local j=1 n="${1:-1}"

	# defconfig
	eval "$MAKE defconfig"
	cp -v .config .config.$j

	j=2 ; for (( i=1 ; i<n ; i++, j++ )) {
		apply_conffiles .config.$j
		cp -v .config.$j .config
		
		yes '' | eval $MAKE oldconfig

		(( j++ )) ; cp -v .config .config.$j
	}
}