#!/bin/bash
# zsh handles for-loop in braindead way

CCNAMES="CC c++ g++ g++2 g++3 g++-2.95 g++-3.0 g++-3.2 g++-3.3 \
                              g++-3.4 gcc-4.0 gcc-4.1 gcc-4.2 gcc-4.3 gcc-4.4"

TESTCC=conftest.cc
TESTEXE=conftest

test1()
{
	cat > $TESTCC <<'EOF'
	#define __ENABLE_WSTRING 1
	#include <string>
	
	using namespace std;
	
	int main(void)
	{
	    wstring test;
	    test += (wchar_t)5;
	    return 0;
	}
EOF
	$1 -o $TESTEXE $TESTCC
}

test2()
{
	cat > $TESTCC <<'EOF'
	#include <string>
	
	using namespace std;
	
	int main(void)
	{
	    basic_string<unsigned> test;
	    test += 5;
	    return 0; 
	}
EOF
	$1 -o $TESTEXE $TESTCC
}

echo "Running tests for compilers"
for ccname in $CCNAMES
do
	if which $ccname >/dev/null 2>/dev/null; then
		printf "%10s " $ccname
		c=0
		eka=1
		for test in test1 test2; do
			cmd=''
			if $test $ccname 2>/dev/null; then
				cmd=' passes'
			else
				if [ $? = 127 ]; then
					cmd=" doesn't exist"
					test=""
				else
					cmd=' fails'
				fi
			fi
			if [ "$c" = "$cmd" ]; then
				if [ ! "$test" = "" ]; then
					printf ' and'
				fi
			else
				if [ "$eka" = 1 ]; then
					eka=0
				else
					printf ","
				fi
				printf "$cmd"
				c="$cmd"
			fi
			printf " $test"
		done
		if [ "$c" = 0 ]; then
			printf ' fails all tests'
		fi
		printf "\\n"
	fi
done
rm -f $TESTEXE $TESTCC
echo "--"
echo "  Okay, I just tested your compilers."
echo "  As it seems, various gcc versions handle wide-strings"
echo "  in many various ways. This variety makes it difficult"
echo "  to write wide-string-using programs portably."
echo "  I wish you luck."
echo "  If it fails, try changing the wchar_t/wstring definitions"
echo "  near the beginning of htmlrecode.hh ."
echo "Type 'make'"

