#!/bin/sh

echo "compiling assembly for simpvir.c ..."
./compile-simpvir.sh

echo "compiling shmall.c ..."
./compile-shmall-with-shmall.sh
mv shmall bin

echo "compiling schant-int.c ..."
./compile-schant-with-shmall.sh int
mv schant-int bin

echo "compiling schant-bigfloat8.c ..."
./compile-schant-with-shmall.sh bigfloat8
mv schant-bigfloat8 bin

echo "compiling schant-bigfloat12.c ..."
./compile-schant-with-shmall.sh bigfloat12
mv schant-bigfloat12 bin

echo "recreating shmall simpvir bytcodes ..."
./compile-with-shmall-to-simpvir.sh shmall
diff -w -q shmall bin/shmall-vm.txt
if [ $? = 0 ]; then
  echo "compilation of shmall to simpvir bytecodes is identical to original"
else
  echo "compilation of shmall to simpvir bytecodes is not identical to original"
fi

cat src/c/simpvir.c | bin/shmall >xxx.scm

echo "recreating simpvir-x86-core.asm ..."
echo x86 >compilation-mode.txt
cat bin/schimper.txt compilation-mode.txt xxx.scm | bin/schant-int >xxx
diff xxx src/asm/simpvir-x86-core.asm 
if [ $? = 0 ]; then
  echo "compilation of simpvir.c to x86 target is identical to original"
else
  echo "compilation of simpvir.c to x86 target is not identical to original"
fi

POOLGAP=350
echo "recreating simpvir-arm6-core.s ..."
echo "arm v6 $POOLGAP" >compilation-mode.txt
cat bin/schimper.txt compilation-mode.txt xxx.scm | bin/schant-int >xxx
diff xxx src/asm/simpvir-arm6-core.s
if [ $? = 0 ]; then
  echo "compilation of simpvir.c to arm6 target is identical to original"
else
  echo "compilation of simpvir.c to arm6 target is not identical to original"
fi

echo "recreating simpvir-arm7-core.s ..."
echo "arm v7 $POOLGAP" >compilation-mode.txt
cat bin/schimper.txt compilation-mode.txt xxx.scm | bin/schant-int >xxx
diff xxx src/asm/simpvir-arm7-core.s
if [ $? = 0 ]; then
  echo "compilation of simpvir.c to arm7 target is identical to original"
else
  echo "compilation of simpvir.c to arm7 target is not identical to original"
fi

echo "recreating simpvir-riscv-core.s ..."
echo riscv >compilation-mode.txt
cat bin/schimper.txt compilation-mode.txt xxx.scm | bin/schant-int >xxx
diff xxx src/asm/simpvir-riscv-core.s
if [ $? = 0 ]; then
  echo "compilation of simpvir.c to riscv target is identical to original"
else
  echo "compilation of simpvir.c to riscv target is not identical to original"
fi

rm xxx.scm

echo "recreating schant-compiler ..."
cat bin/schant-compiler.txt src/scheme/schant-compiler.scm | \
  bin/schant-bigfloat12 >xxx
diff bin/schant-compiler.txt xxx
if [ $? = 0 ]; then
  echo "compilation of the schant-compiler using the schant-compiler produces output identical to original"
else
  echo "compilation of the schant-compiler using the schant-compiler does not produce output identical to original"
fi
rm xxx

echo "recreating simpvir-assembler ..."
cat bin/schant-compiler.txt src/scheme/simpvir-assembler.scm | \
  bin/schant-int >xxx
diff bin/simpvir-assembler.txt xxx
if [ $? = 0 ]; then
  echo "compilation of the simpvir-assembler using the schant-compiler produces output identical to original"
else
  echo "compilation of the simpvir-assembler using the schant-compiler does not produce output identical to original"
fi
rm xxx

echo "recreating schimper ..."
cat bin/schant-compiler.txt src/scheme/schimper.scm | \
  bin/schant-int >xxx
diff bin/schimper.txt xxx
if [ $? = 0 ]; then
  echo "compilation of schimper using the schant-compiler produces output identical to original"
else
  echo "compilation of schimper using the schant-compiler does not produce output identical to original"
fi
rm xxx

echo "recreating schant-int simpvir bytecodes ..."
./compile-schant-int-to-simpvir-bytecodes.sh
diff bin/schant-int.vm.txt schant-int.vm.txt
if [ $? = 0 ]; then
  echo "compilation of schant-int to simpvir bytecodes output identical to original"
else
  echo "compilation of schant-int to simpvir bytecodes does not produce output identical to original"
fi
rm schant-int.vm.txt

echo "running simpvir tests ..."
cd tests
./test-simpvir.sh
echo "running scheme tests ..."
./test-scheme.sh
echo "running C tests ..."
./test-c.sh
