#!/bin/sh
#
#	mkconfig -- convert from uClinux/dist config to local .config
#
#	For now we just convert the existing old style configs from the
#	uClinux-dist setup to new style .config for busybox. Eventually
#	I want to switch to new style configs in uClinux-dist and then
#	just include the Kconfig/config.in files here directly.
#

TMP=.config$$.tmp
NEW=.config$$.new

cp defconfig $NEW

# NOTE: Converted to use "tr" to add newlines (\n) as using sed directly
#       to add them is not supported on all platforms.

grep CONFIG_USER_BUSYBOX_ ../../config/.config |
grep -v "^# " |
sed -e 's/^CONFIG_USER_BUSYBOX_//g;s/=y$//g' -e 's/TRUE_FALSE/TRUE^FALSE/' |
tr "^" "\n" |
grep -v '^BUSYBOX$' |
while read OPNAME
do
	cp $NEW $TMP
	awk -v CNAME=CONFIG_$OPNAME -v FNAME=CONFIG_FEATURE_$OPNAME \
		'
		BEGIN {
			found_in_defconfig = 0
		}

		 {
		   if (($2 == CNAME) || ($2 == FNAME)) {
			found_in_defconfig = 1
			if ($2 == "CONFIG_FEATURE_COMMAND_SAVEHISTORY") {
				printf "%s=y\n", $2
				printf "CONFIG_FEATURE_COMMAND_HISTORY=15\n"
			}
			else
				printf "%s=y\n", $2
		   }
		   else
			print $0
		 }

		END {
			if (found_in_defconfig == 0)
				print "WARNING: Option in uClinux config but not busybox config: " FNAME > "/dev/stderr"
		}
		' < $TMP > $NEW
done

cat $NEW
rm -f $NEW $TMP
exit 0
