#!/bin/bash

if [ -z "$base" ]; then export base=.; else export base; fi
config='config.bad'
cfglog_base="config.log"
cfglog="$cfglog_base.$$"
unset keeplog
echo -n > "$cfglog"

. $base/configure__f.sh
. $base/configure_clev.sh
. $base/configure_args.sh

if [ "$mode" == 'help' ]; then
	. $base/configure_help.sh
	exit
fi

if [ -z "$keeplog" -a "$mode" != 'regen' ]; then
	cat "$cfglog" > "$cfglog_base"
else
	cat "$cfglog" >> "$cfglog_base"
fi
rm -f "$cfglog"
cfglog="$cfglog_base"


# Possible modes are: (none), native, cross, buildmini, target
# Default mode, (none), means -z $mode -a -z $target
# $build is needed for (none), native, buildmini, and for cross to pass it to buildmini
# $target is needed for cross and target

if [ -z "$buildarch" -a "$mode" != "target" ]; then
	mstart "Guessing build platform"
	if [ "x$build" == "x" ]; then
		buildarch=`$base/config.guess 2>>$cfglog`
	else
		buildarch=`$base/config.sub "$build" 2>>$cfglog`
	fi
	if [ -n "$buildarch" ]; then
		result "$buildarch"
	else
		gccbuild=`gcc -v 2>&1 | grep Target | sed -e 's/Target: *//'`
		buildarch=`$base/config.sub "$gccbuild" 2>>$cfglog`
		if [ -n "$gccbuild" ]; then
			result "$buildarch"
		else
			if [ -n "$build" ]; then
				setvar 'buildarch' "$build"
			else
				setvar 'buildarch' 'unknown'
			fi
			result "$buildarch"
			msg "WARNING: Couldn't guess build platform, using '$buildarch'"
		fi
	fi
fi
# past this point $build/$buildarch are non-empty unless we're in target mode

if [ -z "$mode" ]; then
	if [ -z "$target" -o "$target" == "$build" ]; then
		msg "Assuming native build"
		mode='native'
	else
		msg "Assuming cross-compilation for '$target'"
		mode='cross'
	fi
fi
# past this point $mode can't be empty

if [ "$mode" == "buildmini" -o "$mode" == "native" ]; then
	targetarch="$buildarch"
	target="$build"
fi

if [ -n "$target" -a -z "$targetarch" ]; then
	mstart "Guessing target architecture"	
	targetarch=`$base/config.sub "$target" 2>>$cfglog`
	if [ -n "$targetarch" ]; then
		result "$targetarch"
	else
		gcctarget=`$target-gcc -v 2>&1 | grep Target | sed -e 's/Target: *//'`
		targetarch=`$base/config.sub "$gcctarget" 2>>$cfglog`
		if [ -n "$targetarch" ]; then
			result "$targetarch"
		else
			result "($target)"
			targetarch="$target"
			msg "WARNING: Couldn't determine target architecture, using '$target'"
		fi
	fi
elif [ -z "$target" ]; then
	if [ "$mode" == 'cross' -o "$mode" == 'target' ]; then
		die "--target must be specified for a cross build"
	fi
fi

if [ "$mode" == "cross" ]; then
	msg "Respawning configure for build and target platforms"
	msg
	run $0 $hco --keeplog --mode=buildmini --build="$build" --buildarch="$buildarch"\
		|| die "configure --mode=buildmini failed"
	msg
       	run $0 "$@" --keeplog --mode=target --target="$target" --targetarch="$targetarch"\
		|| die "configure --mode=target failed"
	msg
	msg "Ok, back to the top-level configure"
elif [ "$mode" == "buildmini" ]; then
	targetlabel=`archlabel "$build" "$targetarch"`
	msg "Configuring build-time miniperl for $targetlabel"
	if [ "x$build" != "x" ]; then pf1="$build-"; else pf1=""; fi
	pf2="$build-"
	config='xconfig.sh'
elif [ "$mode" == "target" ]; then
	targetlabel=`archlabel "$target" "$targetarch"`
	msg "Configuring primary perl executable for $targetlabel"
	if [ "x$target" != "x" ]; then pf1="$target-"; else pf1="$targetarch-"; fi
	pf2="$target-"
	config='config.sh'
elif [ "$mode" == "native" ]; then 
	msg "Configuring for native build"
	config='config.sh'
fi

if [ "$mode" == "target" ]; then
	setvar 'usecrosscompile' 'define'
fi

if [ "$mode" == 'native' -o "$mode" == 'buildmini' -o "$mode" == "target" ]; then
	. $base/configure_version.sh
	. $base/configure_hint.sh
	. $base/configure_vars.sh
	. $base/configure_prog.sh
	. $base/configure_hinu.sh
	. $base/configure_cfby.sh
	. $base/configure_hdrs.sh
	. $base/configure_type.sh
	. $base/configure_type_ext.sh
	. $base/configure_type_sel.sh
	. $base/configure_byte.sh
	. $base/configure_sigs.sh
	. $base/configure_libs.sh
	. $base/configure_func.sh
	. $base/configure_defs.sh
	. $base/configure_fung.sh
	. $base/configure_attr.sh
	. $base/configure_link.sh
	. $base/configure_dbug.sh
	if [ "$usethreads" == 'define' ]; then
		. $base/configure_thrd.sh
		failpoint
	fi
	. $base/configure_misc.sh
	if [ "$mode" != "buildmini" ]; then
		. $base/configure_mods.sh
		failpoint
	fi
	. $base/configure_genc.sh
	failpoint
fi


if [ "$mode" == 'native' ]; then
	echo "Generating config.h"
	./config_h.SH
	echo "Generating Makefile.config"
	./Makefile.config.SH

	echo
	echo "Configuration completed for native build"
	echo
	echo "            platform:  $archname"
	echo "          c compiler:  $cc"
	echo "                  ld:  $ld"
	echo "                  ar:  $ar"
	echo "              ranlib:  $ranlib"
	echo
	echo "              prefix:  $prefix"
	echo " installation prefix:  $installprefix"
	echo "         executables:  $bin"
	echo "             scripts:  $bin"
	echo "             privlib:  $privlib"
	echo "             archlib:  $archlib"
	echo " section 1 man pages:  $man1dir  (with '$man1ext' extension)"
	echo " section 3 man pages:  $man3dir  (with '$man3ext' extension)"
	echo
elif [ "$mode" == 'cross' ]; then
	echo "Generating config.h and xconfig.h"
	CONFIG_SH=config.sh  CONFIG_H=config.h  ./config_h.SH
	CONFIG_SH=xconfig.sh CONFIG_H=xconfig.h ./config_h.SH
	echo "Generating Makefile.config"
	./Makefile.config.SH
	echo
	echo "Configuration completed for cross build"
	echo
	. ./xconfig.sh
	echo "      build platform:  $archname"
	echo "          C compiler:  $cc"
	echo "              linker:  $ld"
	echo "                  ar:  $ar"
	echo "              ranlib:  $ranlib"
	echo "             objdump:  $objdump"
	echo
	. ./config.sh
	echo "     target platform:  $archname"
	echo "          C compiler:  $cc"
	echo "                  ld:  $ld"
	echo "                  ar:  $ar"
	echo "              ranlib:  $ranlib"
	echo "             objdump:  $objdump"
	echo
	echo "              prefix:  $prefix"
	echo "             sysroot:  $sysroot"
	echo " installation prefix:  $installprefix"
	echo "         executables:  $bin"
	echo "             scripts:  $bin"
	echo "             privlib:  $privlib"
	echo "             archlib:  $archlib"
	echo " section 1 man pages:  $man1dir  (with '$man1ext' extension)"
	echo " section 3 man pages:  $man3dir  (with '$man3ext' extension)"
	echo
elif [ "$mode" == "regen" ]; then
	if [ -f config.sh ]; then
		CONFIG_SH=config.sh  CONFIG_H=config.h  ./config_h.SH
	fi
	if [ -f xconfig.sh ]; then
		CONFIG_SH=xconfig.sh CONFIG_H=xconfig.h ./config_h.SH
	fi
	echo "Generating Makefile.config"
	./Makefile.config.SH
fi
