#!/bin/sh
#############################################################################
#
#	This script gets called in two ways,  the first is to set the
#	default config up.  The second "final" pass is to save any settings
#	back to the vendors directory for permanent inclusion.
#
#	Copyright (C) 2001       Lineo    <davidm@snapgear.com>
#	Copyright (C) 2001-2002  SnapGear <davidm@snapgear.com>
#
#############################################################################
#
# Some basic starting points
#

CONFIG=.config
if [ -f ./.oldconfig ]; then
	. ./.oldconfig
fi

CONFIG_OPROFILE=
if [ "$1" == "oprofile" ]; then
	export CONFIG_OPROFILE="y"
	shift
fi

PASS="$1"
VENDOR=""
PRODUCT=""
LINUXDIR=""
LIBCDIR=""
LIBCBASEDIR=""



#############################################################################
#
# this code closely matches that in mkconfig,  it has to !
#

get_kernel()
{
	KERNEL="`grep '^CONFIG_DEFAULTS_KERNEL_.*=y' ${CONFIG}`"
	if [ -z "${KERNEL}" ]; then
		return 1
	fi
	KERNEL=${KERNEL##CONFIG_DEFAULTS_KERNEL_}
	KERNEL=${KERNEL%%=y}
	KERNEL="`echo ${KERNEL} | sed -e 's/_/./g'`.x"
	return 0
}

var_isset() # because of make evironment inheritance,  we must look in the file
{
	if grep "$1=y" $CONFIG > /dev/null
	then
		return 0
	fi
	return 1
}

fix_name()
{
	echo $1 | tr '[\-\.\/\+a-z]' '[____A-Z]'
}

#
# remove line containing $1 append line $2 to file in $3
#
replace_line()
{
	TMPR="/tmp/setconfig.$$"
	if [ -f "$3" ]
	then
		cat "$3" | grep -v "$1" > "$TMPR"
	else
		touch "$TMPR"
	fi
	echo "$2" >> "$TMPR"
	cp -f "$TMPR" "$3"
	rm -f "$TMPR"
}

fix_conf_files()
{
	#
	# add some bits for convienence, must be done after each stage
	#

	replace_line CONFIG_VENDOR= "CONFIG_VENDOR=$VENDOR"       .config
	replace_line CONFIG_PRODUCT= "CONFIG_PRODUCT=$PRODUCT"    .config
	replace_line CONFIG_LINUXDIR= "CONFIG_LINUXDIR=$LINUXDIR" .config
	replace_line CONFIG_LIBCDIR= "CONFIG_LIBCDIR=$LIBCDIR"    .config
	replace_line CONFIG_LANGUAGE= "CONFIG_LANGUAGE=$LANGUAGE" .config

	replace_line CONFIG_VENDOR "#define CONFIG_VENDOR \"$VENDOR\"" \
			 config/autoconf.h
	replace_line CONFIG_PRODUCT "#define CONFIG_PRODUCT \"$PRODUCT\"" \
			 config/autoconf.h
	replace_line CONFIG_LINUXDIR "#define CONFIG_LINUXDIR \"$LINUXDIR\"" \
			 config/autoconf.h
	replace_line CONFIG_LIBCDIR "#define CONFIG_LIBCDIR \"$LIBCDIR\"" \
			 config/autoconf.h
	replace_line CONFIG_LANGUAGE "#define CONFIG_LANGUAGE \"$LANGUAGE\"" \
			 config/autoconf.h
	replace_line VENDORS_AUTOCONF_INCLUDED \
			 "#define VENDORS_AUTOCONF_INCLUDED" config/autoconf.h
	replace_line "#undef AUTOCONF_INCLUDED" "#undef AUTOCONF_INCLUDED" \
			 config/autoconf.h

	replace_line OLD_VENDOR   "OLD_VENDOR=\"$VENDOR\""   ./.oldconfig
	replace_line OLD_PRODUCT  "OLD_PRODUCT=\"$PRODUCT\"" ./.oldconfig
	replace_line OLD_LINUXDIR "OLD_LINUXDIR=\"$LINUXDIR\"" ./.oldconfig
	replace_line OLD_LIBCDIR  "OLD_LIBCDIR=\"$LIBCDIR\"" ./.oldconfig
	replace_line OLD_LANGUAGE  "OLD_LANGUAGE=\"$LANGUAGE\"" ./.oldconfig
}

#############################################################################

for i in vendors/*/*/config.arch
do
	IFS=/
	set -- $i
	IFS=" 
	"
	VDIR="`dirname $i`"
	if [ -f "$VDIR/config.languages" ]
	then
		for j in `cat "$VDIR/config.languages"`
		do
			if var_isset "CONFIG_DEFAULTS_`fix_name $2`_`fix_name $3`_`fix_name $j`"
			then
				VENDOR="$2"
				PRODUCT="$3"
				LANGUAGE="$j"
				break 2
			fi
		done
	fi
	if var_isset "CONFIG_DEFAULTS_`fix_name $2`_`fix_name $3`"
	then
		VENDOR="$2"
		PRODUCT="$3"
		break
	fi
done

#############################################################################

if [ "$VENDOR$PRODUCT" = "" ]; then
	echo "You have not selected a Vendor/Product in the config." >&2
	exit 1
fi

#############################################################################
#
# kernel and libc version
#

# get_kernel defines KERNEL for us
if get_kernel; then
	LINUXDIR="linux-${KERNEL}"
else
	echo "Unknown kernel configuration." >&2
	exit 1
fi

if [ ! -d ${LINUXDIR} ]; then
	echo "Missing kernel directory ${LINUXDIR}." >&2
	exit 1
fi


if var_isset CONFIG_DEFAULTS_UCLIBC; then
	LIBCDIR="uClibc"
	LIBCBASEDIR=$LIBCDIR
elif var_isset CONFIG_DEFAULTS_OLDUCLIBC; then
	LIBCDIR="libc"
	LIBCBASEDIR=$LIBCDIR
elif var_isset CONFIG_DEFAULTS_GLIBC; then
	LIBCDIR="glibc/build"
	LIBCBASEDIR="glibc"
else
	echo "Unknown library configuration." >&2
	exit 1
fi

#############################################################################
#
# Now install the configs,  careful not dump on them unless they asked us to
#

if [ "$PASS" != "final" ]; then

	#
	# if the user touches and important settings then force a clean
	# otherwise bad things can happen in the tree
	#
	if [ "$OLD_LANGUAGE$OLD_PRODUCT$OLD_VENDOR$OLD_LINUXDIR$OLD_LIBCDIR" != \
			"$LANGUAGE$PRODUCT$VENDOR$LINUXDIR$LIBCDIR" ]; then
		if [ -f .oldconfig ]; then
			cp -f .config .config.save
			cp -f .oldconfig .config
			echo; echo; echo; echo
			echo "**** Cleaning tree for old settings ****"
			echo "rm -rf romfs; make clean > /dev/null 2>&1"
			echo; echo; echo; echo
			rm -rf romfs
			make clean > /dev/null 2>&1
			cp -f .config.save .config
		fi
		rm -f $LIBCDIR/.config
		rm -f $LINUXDIR/.config
		rm -f config/.config
		rm -f config.arch
		[ ! -d modules ] || rm -f modules/.config
	fi

    # once before the oldconfig stuff is run
	fix_conf_files

	rm -f config.arch
	ln -s "vendors/$VENDOR/$PRODUCT/config.arch" config.arch

	#THT 9/30/04: Now that we know the architecture, source it.
	#. config.arch
	export ARCH=`grep "^[ ]*\<ARCH\>[ ]*=[ ]\(.*\)" config.arch | awk '{print $3}'`
	export TARGET_ARCH=`grep "^[ ]*\<MACHINE\>[ ]*=[ ]\(.*\)" config.arch | awk '{print $3}'`
	export CROSS_COMPILE=`grep "^[ ]*\<CROSS_COMPILE\>[ ]*=[ ]\(.*\)" config.arch | awk '{print $3}'`
	MAKEARCH="make ARCH=$ARCH TARGET_ARCH=$TARGET_ARCH CROSS_COMPILE=$CROSS_COMPILE"

	# If we are doing an Oprofile build, then don't wipe out the settings done by build.mk
	if  [ -z "$CONFIG_OPROFILE" ]; then
		if [ ! -f $LINUXDIR/.config ] || var_isset CONFIG_DEFAULTS_OVERRIDE; then
			if [ ! -f "vendors/$VENDOR/$PRODUCT/config.$LINUXDIR" ]; then
				echo; echo; echo; echo
				echo "*********************** NOTICE ****************************"
				echo "There is no preset config for $LINUXDIR on this platform."
				echo "If you choose to continue you will have to setup a linux"
				echo "config from scratch which is a very complex task."
				echo "You will find it easier if you can copy the config from"
				echo "another platform that is similar."
				echo
				echo "If you know what you are doing press <ENTER> otherwise"
				echo "Type ^C to abort."
				read dummy
			else
				cp -f "vendors/$VENDOR/$PRODUCT/config.$LINUXDIR" $LINUXDIR/.config
			fi
			$MAKEARCH oldconfig_linux
		fi
	fi

	#
	# everything about modules is optional,  optional modules dir
	# and optional modules config files
	#
	if [ -d modules ]; then
		TMP=`expr $LINUXDIR : "[^-][^-]*\(-.*\)"`
		if [ -f "vendors/$VENDOR/$PRODUCT/config.modules$TMP" ]
		then
			TMP="vendors/$VENDOR/$PRODUCT/config.modules$TMP"
		else
			TMP="vendors/$VENDOR/$PRODUCT/config.modules"
		fi
		if [ ! -f modules/.config ] || var_isset CONFIG_DEFAULTS_OVERRIDE; then
			if [ -f "$TMP" ]; then
				cp -f "$TMP" modules/.config
			fi
			$MAKEARCH oldconfig_modules
		fi
	fi

	# If we are doing an Oprofile build, then don't wipe out the settings done by build.mk
	if  [ -z "$CONFIG_OPROFILE" ]; then
		if [ ! -f config/.config ] || var_isset CONFIG_DEFAULTS_OVERRIDE; then
			TMP=`expr $LINUXDIR : "[^-][^-]*\(-.*\)"`
			if [ -f "vendors/$VENDOR/$PRODUCT/config.vendor$TMP" ]
			then
				cp -f "vendors/$VENDOR/$PRODUCT/config.vendor$TMP" config/.config
			elif [ -f "vendors/$VENDOR/$PRODUCT/config.vendor" ]
			then
				cp -f "vendors/$VENDOR/$PRODUCT/config.vendor" config/.config
			else
				echo; echo; echo; echo
				echo "*********************** NOTICE ****************************"
				echo "There is no preset config for applications on this platform."
				echo "If you choose to continue you will have to setup your"
				echo "application config from scratch.  You will find it easier"
				echo "if you can copy the config from another platform that is"
				echo "similar."
				echo
				echo "If you press <ENTER> you will be forced into the application"
				echo "config screen as part of the config process."
				echo "You may type ^C to abort."
				read dummy
				#
				# force config of user apps
				#
				replace_line CONFIG_DEFAULTS_VENDOR= \
						"CONFIG_DEFAULTS_VENDOR=y" .config
			fi
			$MAKEARCH -C config oldconfig
		fi
	fi

	if [ -L lib/$LIBCBASEDIR ]; then
		rm -f lib/$LIBCBASEDIR
	fi

	if [ ! -e lib/$LIBCBASEDIR -a -e $LIBCBASEDIR/. ] ; then
		ln -s "`pwd`/$LIBCBASEDIR" "lib/$LIBCBASEDIR"
	fi

	if [ "uClibc" = "$LIBCBASEDIR" ]; then
		# if [ ! -f lib/$LIBCDIR/.config ] || var_isset CONFIG_DEFAULTS_OVERRIDE; then
			if [ -f "vendors/$VENDOR/$PRODUCT/config.$LIBCBASEDIR" ]; then
#				cp -f "vendors/$VENDOR/$PRODUCT/config.$LIBCBASEDIR" \
#						lib/$LIBCDIR/.config
				cp -f "vendors/$VENDOR/$PRODUCT/config.$LIBCBASEDIR" \
						$LIBCBASEDIR/.config
				# THT PR9961: For now just tag on, but will make a target
				# for uClibc in later releases.
				$MAKEARCH TARGET_ARCH=$ARCH -C $LIBCBASEDIR oldconfig
				echo "*** Next, you must run 'make dep'."
			else
				echo; echo; echo; echo
				echo "********************** NOTICE ***************************"
				echo "There is no preset config for $LIBCBASEDIR on this"
				echo "platform.  $LIBCBASEDIR cannot build without this."
				echo "You should copy a config.$LIBCBASEDIR from another"
				echo "target that is similar to yours to your chosen"
				echo "vendor/$VENDOR/$PRODUCT directory and check that it is correct."
				echo
				echo "Once you have done that start the config process again."
				exit 1
			fi
		# fi
	fi

	# THT: Copy busybox config over, regardless of setting.
	#if [ ! -f user/busybox/.config ] || var_isset CONFIG_DEFAULTS_OVERRIDE; then
		if [ -f "vendors/$VENDOR/$PRODUCT/config.busybox" ]; then
			cp -f "vendors/$VENDOR/$PRODUCT/config.busybox" \
					user/busybox/.config
			$MAKEARCH -C user/busybox oldconfig
			echo "*** Next, you must run 'make dep'."
		else
			echo; echo; echo; echo
			echo "********************** NOTICE ***************************"
			echo "There is no preset config for busybox on this"
			echo "platform.  busybox cannot build without this."
			echo "You should copy a config.busybox from another"
			echo "target that is similar to yours to your chosen"
			echo "vendor/$VENDOR/$PRODUCT directory and check that it is correct."
			echo
			echo "Once you have done that start the config process again."
			exit 1
		fi
	#fi


	# and once after to clean up
	fix_conf_files
fi

#############################################################################
#
# The override config option is always turned off when we are done so that
# someone doesn't get all upset that they lost their config files.
#
# Always add the VENDOR/PRODUCT dir for the build to work
#

if [ "$PASS" = "final" ]; then

	fix_conf_files

	#
	# check for other config options
	#

	if var_isset CONFIG_DEFAULTS_OVERRIDE; then
		sed 's/^\(CONFIG_DEFAULTS_OVERRIDE\)=y/# \1 is not set/' < .config \
				> .config.tmp
		cp -f .config.tmp .config
		rm -f .config.tmp
	fi

	if var_isset CONFIG_DEFAULTS_VENDOR_UPDATE; then

		cp -f $LINUXDIR/.config "vendors/$VENDOR/$PRODUCT/config.$LINUXDIR"
		TMP=`expr $LINUXDIR : "[^-][^-]*\(-.*\)"`
		if [ -f "vendors/$VENDOR/$PRODUCT/config.vendor$TMP" ]
		then
			cp -f config/.config "vendors/$VENDOR/$PRODUCT/config.vendor$TMP"
		else
			cp -f config/.config "vendors/$VENDOR/$PRODUCT/config.vendor"
		fi
		if [ -f modules/.config ]; then
			if [ -f "vendors/$VENDOR/$PRODUCT/config.modules$TMP" ]
			then
				cp modules/.config "vendors/$VENDOR/$PRODUCT/config.modules$TMP"
			else
				cp modules/.config "vendors/$VENDOR/$PRODUCT/config.modules"
			fi
		fi
		if [ -f "$LIBCDIR/.config" ]; then
			cp -f $LIBCDIR/.config "vendors/$VENDOR/$PRODUCT/config.$LIBCBASEDIR"
		fi

		sed 's/^\(CONFIG_DEFAULTS_VENDOR_UPDATE\)=y/# \1 is not set/' <.config\
				> .config.tmp
		cp -f .config.tmp .config
		rm -f .config.tmp
	fi
	
	if var_isset CONFIG_DEFAULTS_BUSYBOX_UPDATE; then

		cp -f user/busybox/.config "vendors/$VENDOR/$PRODUCT/config.busybox"

	fi

	grep -v CONFIG_DEFAULTS_VENDOR= < .config | \
		grep -v CONFIG_DEFAULTS_MODULES= | \
		grep -v CONFIG_DEFAULTS_KERNEL= > .config.tmp

	cp -f .config.tmp .config
	rm -f .config.tmp
fi

#############################################################################
exit 0
