#!/bin/sh
# -*- sh -*-
# Drive configuration and built of tcl
# Argument: Base directory containing the
#           sources and configurations to be built.

#echo cdrive /DISABLED ; exit 0

# ====================================================

cfg=$1

origin=`pwd`

# ====================================================
# Assumed structure:
# ====================================================
#
#     base/
#	src/            - Sources
#       build/	        - Build directories, cfg & code
#       install/        - Installed interpreters
#       data/           - Information for reports
#
#	src/
#	  tcl...
#
#       build/
#         cfgNNN/ ...	(Any name will do)
#            cdrive.cfg		sources=tcl...
#                               config=-D...
#       install/
#         cfgNNN/
#
#       data/
#         cfgNNN/
#            static.size     - Library size
#            stack.usage     - Raw stacktest results
#            available.tests - Listing of all tests in the testsuite.
#   
# ====================================================

cd `dirname $0`/../work/build

here=`pwd`
echo $0 ... Compile configurations in $here


if [ X$cfg != X ]
then
    dir=$cfg
	if [ ! -d "$dir" ]; then continue; fi
	if [ ! -f "$dir/cdrive.cfg" ]; then continue; fi
	echo '*' $dir ...
	cd $dir
	    srcdir=`grep sources= cdrive.cfg | sed -e 's/sources=//'`
	    config=`grep config= cdrive.cfg | sed -e 's/config=//'`
	    install=`pwd`/../../install/$dir

	    echo ' ' '-' 'sources' = $srcdir
	    echo ' ' '-' 'config ' = $config
	    echo ' ' '-' 'install' = $install
	    echo ' ' Configuring ...
	    ../../src/$srcdir/unix/configure --enable-symbols --disable-shared -prefix $install > build.log 2>&1

	    echo ' ' Compiling $config ...
	    make clean all tcltest "ENV_FLAGS=$config"                        >> build.log 2>&1

	    echo ' ' Installing ...
	    rm -rf $install
	    make install "ENV_FLAGS=$config"                                  >> build.log 2>&1

	    echo ' ' Data collection ...
	    mkdir -p ../../data/$dir

	    echo ' ' Determine which tests are in the testsuite ...
	    grep test ../../src/$srcdir/tests/*.test | grep 'test:test' | sed \
		-e 's/ [{"].*$//' \
		-e 's|^.*/||' -e 's/test:test/test/' \
                | grep '.-[0-9]' > ../../data/$dir/available.tests
	    echo ' ' ' ' '#tests = '          `wc -l ../../data/$dir/available.tests`

	    echo ' ' '-' Sizes of tcl binary libraries and shells
	    cd $install
	    strip lib/lib* bin/*
	    wc -c lib/lib* bin/* | grep -v total > ../../data/$dir/static.size

	    exit
fi




for dir in `ls -d *`
do
	if [ ! -d "$dir" ]; then continue; fi
	if [ ! -f "$dir/cdrive.cfg" ]; then continue; fi
	echo '*' $dir ...
	cd $dir
	    srcdir=`grep sources= cdrive.cfg | sed -e 's/sources=//'`
	    config=`grep config= cdrive.cfg | sed -e 's/config=//'`
	    install=`pwd`/../../install/$dir

	    if [ -f STACK ]
	    then
		: #config="$config -DTCL_RECORD_STACK_SIZES"
	    fi

	    echo ' ' '-' 'sources' = $srcdir
	    echo ' ' '-' 'config ' = $config
	    echo ' ' '-' 'install' = $install

	    echo ' ' Configuring ...
	    ../../src/$srcdir/unix/configure --disable-shared -prefix $install > build.log 2>&1

	    echo ' ' Compiling $config ...
	    make clean all tcltest "ENV_FLAGS=$config"                        >> build.log 2>&1

	    echo ' ' Installing ...
	    rm -rf $install
	    make install "ENV_FLAGS=$config"                                  >> build.log 2>&1

	    echo ' ' Data collection ...
	    mkdir -p ../../data/$dir

	    echo ' ' Determine which tests are in the testsuite ...
	    grep test ../../src/$srcdir/tests/*.test | grep 'test:test' | sed \
		-e 's/ [{"].*$//' \
		-e 's|^.*/||' -e 's/test:test/test/' \
                | grep '.-[0-9]' > ../../data/$dir/available.tests
	    echo ' ' ' ' '#tests = '          `wc -l ../../data/$dir/available.tests`

	    echo ' ' '-' Sizes of tcl binary libraries and shells
	    cd $install
	    strip lib/lib* bin/*
	    wc -c lib/lib* bin/* | grep -v total > ../../data/$dir/static.size
	cd $here
done

# ====================================================
exit