#!/bin/rc

switch($#*){
case 1
case *
	echo usage: p9config progname
	exit
}

if(~ $#tobjtype 0)
	tobjtype=$objtype

fn archmap {
	switch($1) {
	case 386
		echo i386-lucent-plan9
	case arm
		echo armel-lucent-plan9
	case spim
		echo mipsel-lucent-plan9
	case *
		echo $1-lucent-plan9
	}
}

build = `{ archmap $cputype }		# arch we're building on
host = `{ archmap $objtype }		# arch what we're building will run on
target = `{ archmap $tobjtype }		# arch that what we're building understands

switch($1) {
case m4*
	diropts = (prefix exec-prefix )
case autoconf* automake*
	diropts = (prefix exec-prefix rc-bindir datadir includedir infodir libdir libexecdir localstatedir mandir \
		oldincludedir rc-sbindir sharedstatedir sysconfdir build host target )
case gcc*
	diropts = (prefix exec-prefix bindir datadir includedir infodir languages libdir libexecdir localstatedir \
		mandir oldincludedir sbindir sharedstatedir sysconfdir build host target )
case *
	diropts = (prefix exec-prefix bindir datadir includedir infodir libdir libexecdir localstatedir \
		mandir oldincludedir sbindir sharedstatedir sysconfdir build host target )
}

options = ()
for(i in $diropts) {
	switch($i) {
	case prefix
		options = ( $options --prefix /sys/lib/gnu )
	case exec-prefix
		options = ( $options --exec-prefix /$objtype/bin/gnu )
	case bindir
		options = ( $options --bindir /$objtype/bin/gnu )
	case rc-bindir
		options = ( $options --bindir /rc/bin/gnu )
	case datadir
		options = ( $options --datadir /sys/lib/gnu )
	case includedir
		options = ( $options --includedir /sys/include/gnu )
	case infodir
		options = ( $options --infodir /sys/lib/gnu/info )
	case languages
		options = ( $options --enable-languages'=c,c++' )
	case libdir
		options = ( $options --libdir /$tobjtype/lib/gnu )
	case libexecdir
		options = ( $options --libexecdir /$objtype/bin/gnu/libexec )
	case localstatedir
		options = ( $options --localstatedir /sys/lib/gnu )
	case mandir
		options = ( $options --mandir /sys/lib/gnu/man )
	case oldincludedir
		options = ( $options --oldincludedir /sys/include/ape )
	case sbindir
		options = ( $options --sbindir /$objtype/bin/gnu  )
	case rc-sbindir
		options = ( $options --sbindir /rc/bin/gnu  )
	case sharedstatedir
		options = ( $options --sharedstatedir /sys/lib/gnu  )
	case sysconfdir
		options = ( $options --sysconfdir /sys/lib/gnu  )
	case build
		options = ( $options --build $build  )
	case host
		options = ( $options --host $host  )
	case target
		options = ( $options --target $target  )
	}
}

TOOLBIN=/$objtype/bin/gnu/$target 
TOOLLIB=/sys/lib/gnu/$target

CC_FOR_BUILD=$build-gcc
CXX_FOR_BUILD=$build-gcc

CC=$host-gcc
AR=$host-ar
AS=$host-as
RANLIB=$host-ranlib

CC_FOR_TARGET=$target-gcc
GCC_FOR_TARGET=$target-gcc
CXX_FOR_TARGET=$target-gcc
AS_FOR_TARGET=$target-as
LD_FOR_TARGET=$target-ld
NM_FOR_TARGET=$target-nm
AR_FOR_TARGET=$target-ar
RANLIB_FOR_TARGET=$target-ranlib

options = ( $options --enable-maintainer-mode )

echo ./configure $options
./configure $options

make -w \
	'CC_FOR_BUILD='$CC_FOR_BUILD \
	'CXX_FOR_BUILD='$CXX_FOR_BUILD \
	'CC='$CC \
	'AR='$AR \
	'RANLIB='$RANLIB \
	'AS='$AS \
	'CC_FOR_TARGET='$CC_FOR_TARGET \
	'GCC_FOR_TARGET='$GCC_FOR_TARGET \
	'CXX_FOR_TARGET='$CXX_FOR_TARGET \
	'AS_FOR_TARGET='$AS_FOR_TARGET \
	'LD_FOR_TARGET='$LD_FOR_TARGET \
	'NM_FOR_TARGET='$NM_FOR_TARGET \
	'AR_FOR_TARGET='$AR_FOR_TARGET \
	'RANLIB_FOR_TARGET='$RANLIB_FOR_TARGET
