1. env_pre.mk: Add additional build-tree states and unify them under the $(BUILD_TREE_STATE) variable.
2. Makefile: Resolve issue spotted by Mitani-san because it was an overlooked corner case that wasn't properly expressed until $(BUILD_TREE_STATE) was introduced.
3. testscripts/... : resolve some issues with the test scripts.

Signed-off-by: Garrett Cooper <yanegomi@gmail.com>
diff --git a/Makefile b/Makefile
index a31e401..500548b 100644
--- a/Makefile
+++ b/Makefile
@@ -70,7 +70,7 @@
 
 COMMON_TARGETS		+= testcases tools
 # Don't want to nuke the original files if we're installing in-build-tree.
-ifneq ($(INSTALL_IN_BUILD_TREE),1)
+ifneq ($(BUILD_TREE_STATE),$(BUILD_TREE_SRCDIR_INSTALL))
 INSTALL_TARGETS		+= runtest testscripts
 CLEAN_TARGETS		+= include runtest testscripts
 endif
@@ -139,7 +139,9 @@
 clean_install_dir::
 	$(RM) -Rf "$(INSTALL_DIR)"
 
-ifneq ($(INSTALL_IN_BUILD_TREE),1)
+# Don't clean the directory if the build-tree is set to the srcdir, or the
+# build tree is unconfigured.
+ifneq ($(filter $(BUILD_TREE_STATE),$(BUILD_TREE_SRCDIR_INSTALL) $(BUILD_TREE_UNCONFIGURED)),)
 CLEAN_TARGETS	+= clean_install_dir
 endif
 
@@ -166,7 +168,7 @@
 $(addprefix $(INSTALL_DIR)/,$(SRCDIR_INSTALL_SCRIPTS)): %:
 	install -m 00755 "$(top_srcdir)/$(@F)" "$@"
 
-ifneq ($(INSTALL_IN_BUILD_TREE),1)
+ifneq ($(BUILD_TREE_STATE),$(BUILD_TREE_SRCDIR_INSTALL))
 INSTALL_TARGETS		+= $(addprefix $(INSTALL_DIR)/,$(SRCDIR_INSTALL_TARGETS))
 endif
 INSTALL_TARGETS		+= $(addprefix $(DESTDIR)/$(bindir)/,$(BINDIR_INSTALL_SCRIPTS))
diff --git a/include/mk/env_pre.mk b/include/mk/env_pre.mk
index c49788a..4444124 100644
--- a/include/mk/env_pre.mk
+++ b/include/mk/env_pre.mk
@@ -27,6 +27,15 @@
 ifndef ENV_PRE_LOADED
 ENV_PRE_LOADED = 1
 
+# "out-of-build-tree" build.
+BUILD_TREE_BUILDDIR_INSTALL	:= 1
+# "in-srcdir" build / install.
+BUILD_TREE_SRCDIR_INSTALL	:= 2
+# "in-srcdir" build, non-srcdir install.
+BUILD_TREE_NONSRCDIR_INSTALL	:= 3
+# configure not run.
+BUILD_TREE_UNCONFIGURED		:= 4
+
 ifndef MAKE_VERSION_CHECK
 export MAKE_VERSION_CHECK = 1
 ifneq ($(firstword $(sort 3.80 $(MAKE_VERSION))),3.80)
@@ -85,31 +94,45 @@
 srcdir				:= .
 endif
 
-# We can piece together where we're located in the source and object trees with
-# just these two vars and $(CURDIR).
-export abs_top_srcdir abs_top_builddir
-
-# DO NOT MOVE THIS BELOW include [..]/config.mk (will break out-of-build tree
-# checks)!
-ifneq ($(abs_builddir),$(abs_srcdir))
-OUT_OF_BUILD_TREE		:= 1
-else
-# Stub support for installing directly in the build tree; the support is not
-# there yet, but the variable itself has its own uses...
-ifeq ($(strip $(DESTDIR)$(prefix)),)
-INSTALL_IN_BUILD_TREE		:= 1
-else
-ifeq ($(subst $(abs_top_srcdir),,$(prefix)),)
-INSTALL_IN_BUILD_TREE		:= 1
-endif
-endif
-endif
-
+# autotools, *clean, and help don't require config.mk...
 ifeq ($(filter autotools %clean help,$(MAKECMDGOALS)),)
+
 include $(abs_top_builddir)/include/mk/config.mk
+
+# START out-of-build-tree check.
+ifneq ($(abs_builddir),$(abs_srcdir))
+BUILD_TREE_STATE		:= $(BUILD_TREE_BUILDDIR_INSTALL)
+else
+# Else, not out of build tree..
+
+# START srcdir build-tree install checks
+ifeq ($(strip $(DESTDIR)$(prefix)),)
+BUILD_TREE_STATE		:= $(BUILD_TREE_SRCDIR_INSTALL)
+else  # Empty $(DESTDIR)$(prefix)
+ifeq ($(abs_top_srcdir),$(prefix))
+BUILD_TREE_STATE		:= $(BUILD_TREE_SRCDIR_INSTALL)
+endif
+# END srcdir build-tree install checks
+endif
+# END out-of-build-tree check.
+endif
+
+# Is the build-tree configured yet?
+ifeq ($(BUILD_TREE_STATE),)
+ifneq ($(wildcard $(abs_top_builddir)/include/mk/config.mk),)
+BUILD_TREE_STATE		:= $(BUILD_TREE_NONSRCDIR_INSTALL)
+endif
 endif
 
 # make 3.80 called it .DEFAULT_TARGET.
 .DEFAULT_GOAL			:= all
 
+endif	# END autotools, *clean...
+
+BUILD_TREE_STATE		?= $(BUILD_TREE_UNCONFIGURED)
+
+# We can piece together where we're located in the source and object trees with
+# just these two vars and $(CURDIR).
+export abs_top_srcdir abs_top_builddir BUILD_TREE_STATE
+
 endif
diff --git a/testscripts/build/build_test_function.sh b/testscripts/build/build_test_function.sh
index 46aecb1..734e7ca 100755
--- a/testscripts/build/build_test_function.sh
+++ b/testscripts/build/build_test_function.sh
@@ -30,22 +30,40 @@
 		tst_brkm tst_exit TBROK 'Failed to create tmp_builddir';
 	fi
 	if tmp_destdir=$(mktemp -d) ; then
-		vars="$vars $destdir"
+		vars="$vars $tmp_destdir"
 	else
 		tst_brkm tst_exit TBROK 'Failed to create tmp_destdir';
 	fi
+	if tmp_prefix=$(mktemp -d) ; then
+		vars="$vars $tmp_prefix"
+	else
+		tst_brkm tst_exit TBROK 'Failed to create tmp_prefix';
+	fi
 	if tmp_srcdir=$(mktemp -d) ; then
 		vars="$vars $tmp_srcdir"
 	else
 		tst_brkm tst_exit TBROK 'Failed to create tmp_srcdir';
 	fi
 	trap cleanup EXIT
+	cat <<EOF
+================================
+SUMMARY OF VARIABLES:
+================================
+builddir 	-> $tmp_builddir
+destdir		-> $tmp_destdir
+prefix		-> $tmp_prefix
+srcdir		-> $tmp_srcdir
+================================
+EOF
+
 }
 
 cleanup() {
-	cd /
-	trap '' EXIT
-	rm -Rf $vars
+	if [ "x${NO_CLEAN:-0}" != 1 ] ; then
+		cd /
+		trap '' EXIT
+		rm -Rf $vars
+	fi
 }
 
 # Pull from CVS.
@@ -53,7 +71,7 @@
 
 	export CVSROOT=:pserver:anonymous@ltp.cvs.sourceforge.net:/cvsroot/ltp
 
-	if ([ -f ~/.cvspass ] || touch ~/.cvspass) ; then
+	if ( [ -f ~/.cvspass ] || touch ~/.cvspass ) ; then
 		cvs -d$CVSROOT login && cvs -z3 export -f -r HEAD ltp && srcdir="$PWD/ltp"
 	fi
 
@@ -87,15 +105,17 @@
 # set it up to pass DESTDIR as well so it will properly set the installdir
 # global and thus I won't need to include the same checks down below...
 configure() {
+
 	abspath=$(readlink -f "$testscript_dir/../../scripts/abspath.sh")
+
 	if [ "x$2" != x ] ; then
 		(test -d "$2" || mkdir -p "$2") || return $?
 	fi
+
 	make -C "$1" autotools || return $?
-	ARGS=
-	[ "x$3" != x ] && ARGS="--prefix=$("$abspath" $3)"
-	cd "$2" && "$1/configure" $ARGS
-	install_dir=$("$abspath" "$4/$3")
+
+	cd "$2" && "$1/configure" ${3:+--prefix=$("$abspath" $3)}
+
 }
 
 # Build a source tree.
@@ -103,25 +123,37 @@
 # 1 - source directory
 # 2 - build directory
 build() {
-	make ${2:--C "$2"} ${1:-top_srcdir=$1} all
+	make ${2:+-C "$2"} \
+	     ${1:+-f "$1/Makefile" "top_srcdir=$1"} \
+	     ${2:+"top_builddir=$2"} \
+	     all
 }
 
 # Install the binaries and scripts from a build tree.
 #
 # 1 - source directory
 # 2 - build directory
+# 3 - DESTDIR
 install_ltp() {
-	make ${2:--C "$2"} ${1:-top_srcdir=$1} install
+	make ${2:+-C "$2"} \
+	     ${1:+-f "$1/Makefile" "top_srcdir=$1"} \
+	     ${2:+"top_builddir=$2"} \
+	     ${3:+"DESTDIR=$3"} \
+	     install
 }
 
 # Run a test on the installed tree.
 #
-# 1 install directory - installed tree, e.g. $(DESTDIR)/$(prefix)
+# 1 - install directory for tree, e.g. $(DESTDIR)/$(prefix)
 test_ltp() {
+
+	[ "x${1}" != x ] && export LTPROOT="$1"
+
 	# XXX (garrcoop): I haven't tracked down the root cause for the
 	# issue, but some versions of sed combined with some terminal
 	# configurations cause sed to block waiting for EOF on certain
 	# platforms when executing runltp. Thus, we should effectively close
 	# /dev/stdin before executing runltp via execltp.
-	cd "$1" && "$1/bin/execltp" < /dev/null
+	"${1:-.}/bin/execltp" -v < /dev/null
+
 }
diff --git a/testscripts/build/test_install_in_build_tree.sh b/testscripts/build/test_install_in_build_tree.sh
index e3100ff..822ccc6 100755
--- a/testscripts/build/test_install_in_build_tree.sh
+++ b/testscripts/build/test_install_in_build_tree.sh
@@ -36,4 +36,4 @@
 # 4. -->> Install in-build-tree. <<--
 install_ltp "$srcdir" "$srcdir" ""
 # 5. Test.
-test_ltp "$install_dir"
+test_ltp ""
diff --git a/testscripts/build/test_install_out_of_build_tree.sh b/testscripts/build/test_install_out_of_build_tree.sh
index ef35ef7..765eca2 100755
--- a/testscripts/build/test_install_out_of_build_tree.sh
+++ b/testscripts/build/test_install_out_of_build_tree.sh
@@ -1,6 +1,6 @@
 #!/bin/sh
 #
-#    install outside build-tree script.
+#    build / install outside build-tree script.
 #
 #    Copyright (C) 2010, Cisco Systems Inc.
 #
@@ -30,10 +30,10 @@
 # 1. Pull the SCM.
 pull_scm cvs "$tmp_srcdir"
 # 2. Configure.
-configure "$srcdir" "$srcdir" "$prefix" "$destdir"
+configure "$srcdir" "$srcdir" "$tmp_prefix" "$tmp_destdir"
 # 3. -->> Compile out-of-build-tree. <<--
 build "$srcdir" "$srcdir"
 # 4. Install.
-install_ltp "$srcdir" "$srcdir" "$destdir"
+install_ltp "$srcdir" "$srcdir" "$tmp_destdir"
 # 5. Test.
-test_ltp "$install_dir"
+test_ltp "$("$abspath" "$tmp_destdir/${tmp_prefix:-/opt/ltp}")"
diff --git a/testscripts/build/test_out_of_build_tree.sh b/testscripts/build/test_out_of_build_tree.sh
index 85f2f83..85805c2 100755
--- a/testscripts/build/test_out_of_build_tree.sh
+++ b/testscripts/build/test_out_of_build_tree.sh
@@ -30,10 +30,10 @@
 # 1. Pull the SCM.
 pull_scm cvs "$tmp_srcdir"
 # 2. Configure.
-configure "$srcdir" "$tmp_builddir" "$prefix" "$destdir"
+configure "$srcdir" "$tmp_builddir" "$tmp_prefix" "$tmp_destdir"
 # 3. -->> Compile out-of-build-tree. <<--
 build "$srcdir" "$tmp_builddir"
 # 4. Install.
-install_ltp "$srcdir" "$tmp_builddir" "$destdir"
+install_ltp "$srcdir" "$tmp_builddir" "$tmp_destdir"
 # 5. Test.
-test_ltp "$install_dir"
+test_ltp "$("$abspath" "$tmp_destdir/${tmp_prefix:-/opt/ltp}")"