#!/bin/sh
#
# Test whether $1 compiles and executes successfully. 
#

. sh/verbose.sh
. sh/tools.sh
. sh/temp-dir.sh

if [ -z "$1" ]
then
    echo 1>&2 "$0:  *** error:  no source file."
    echo 1>&2 "Invocation:   $0 SOURCE-FILE"
fi

source_file="$1"

<$source_file 1>&4 $sed 's/^\(.*\)$/ | \1/'

# 
# We must compile with all flags in $CXXFLAGS, but don't use the
# warning flags, else GCC could fail due to unused variables or such,     
# despite of the function tested for working.  
#
flags="`echo \" $CXXFLAGS \" | $sed 's/-W[^ ]*/ /g'`"

tmp_file=$temp_dir/${$}.tmp

if sh/do $gcc $flags $source_file -o $temp_e 1>$tmp_file 2>&1 
then
    result=t
else
    result=
fi

<$tmp_file 1>&4 $sed 's/^\(.*\)$/ > \1/' 

if [ "$result" ]
then
    if $temp_e 2>&1 >$tmp_file
    then
	result=t
    else
	result=
    fi
    <$tmp_file 1>&4 $sed 's/^\(.*\)$/ > \1/' 
    if [ "$result" ] ; then exit 0 ; else exit 1 ; fi 
else
    exit 1
fi
