#! /bin/sh

set -e

export V
if test "$V" = 1 -o "$V" = 2; then
    set -x
fi

arch=$(uname -m)
case $arch in
     arm*)
         cpu=arm
         tcc_cpu=arm
         triplet=arm-unknown-linux-gnueabihf
         cross_prefix=${triplet}-
         ;;
     *)
         cpu=x86
         tcc_cpu=i386
         triplet=i686-unknown-linux-gnu
         cross_prefix=${triplet}-
         ;;
esac
export cpu
export cross_prefix
export tcc_cpu
export triplet

GCC_TCC=${GCC_TCC-./${cross_prefix}tcc}
TCC=${TCC-./mes-tcc}
MES_PREFIX=${MES_PREFIX-mes}
OBJDUMP=${OBJDUMP-objdump}
DIFF=${DIFF-diff}

unset C_INCLUDE_PATH LIBRARY_PATH

t=${1-lib/tests/scaffold/t.c}
b=$(dirname "$t")/$(basename "$t" .c)
co="$b"-$triplet-tcc
mo="$b"-mes-tcc

o="$co"

rm -f 1 1.s 1.link
rm -f 2 2.s 2.link
rm -f "$co" "$co".1
rm -f "$mo" "$mo".1

r=0
if [ -x $GCC_TCC ]; then
    $GCC_TCC                                    \
        -c                                      \
        -nostdlib                               \
        -I $MES_PREFIX/include                  \
        -I $MES_PREFIX/scaffold/tinycc          \
        -o "$o".o                               \
        "$t"                                    \
        2> 1
    #$OBJDUMP -d "$t".$GCC_TCC.o > 1.s
    $GCC_TCC                                    \
        -L .                                    \
        -o "$o"                                 \
        "$o".o                                  \
        -lc                                     \
        -ltcc1                                  \
        -ltcc1-mes                              \
        2> 1.link
    set +e
    d=$(dirname "$t")
    d=$(dirname "$d")
    if [ "$d" = lib/tests ]; then
        "$o" -s --long file0 file1 > "$o".1   
    else
        "$o" arg1 arg2 arg3 arg4 arg5 > "$o".1
    fi
    m=$?
    set -e
    [ -f "$b".exit ] && r=$(cat "$b".exit)
    [ $m = $r ]
    if [ -f "$b".expect ]; then
        $DIFF -ub "$b".expect "$o".1;
    fi
fi

o="$b"-mes-tcc
$TCC                                            \
    -c                                          \
    -nostdlib                                   \
    -I $MES_PREFIX/include                      \
    -I $MES_PREFIX/scaffold/tinycc              \
    -o "$o".o                                   \
    "$t"                                        \
    2> 2
$OBJDUMP -d "$o".o > 2.s || true
$TCC                                            \
    -L .                                        \
    -o "$o"                                     \
    "$o".o                                      \
    -lc                                         \
    -ltcc1                                      \
    -ltcc1-mes                                  \
    2> 2.link

set +e
d=$(dirname "$t")
d=$(dirname "$d")
if [ "$d" = lib/tests ]; then
    "$o" -s --long file0 file1 > "$o".1   
else
    "$o" arg1 arg2 arg3 arg4 arg5 > "$o".1
fi
m=$?
#$OBJDUMP -d "$t".$TCC > 2.x

set -e
[ $m = $r ]
[ -f "$b".exit ] && r=$(cat "$b".exit)
if [ -f "$b".expect ]; then
    $DIFF -ub "$b".expect "$co".1;
    $DIFF -ub "$co".1 "$mo".1;
fi

#diff -y 1.s 2.s
#diff -y 1 2
