#!/bin/sh

# compile a C source program in $1.c to simpvir bytecodes

MACHINE=`uname -m`
if [ $MACHINE = i686 -o $MACHINE = i586 ]; then
  OS=`uname -o 2>/dev/null`
  if [ $? != 0 ]; then # -o option unrecognized
    OS=unknown
  fi
  if [ ! $OS = Cygwin ]; then
    OS=`uname -s`
  fi 
else
  OS=`uname -s`
fi

echo $MACHINE $OS

if [ $OS = OpenBSD -o $OS = FreeBSD ]; then
  REVERSE="tail -r"
else
  REVERSE=tac
fi

# TODO checks to allow valid machine / os combinations

cat $1.c | bin/shmall >$1.scm \
#filter out forward function definitions
cat $1.scm | $REVERSE | sed -e '/^__forward/{N;N;N;d;}' | \
  $REVERSE >xxxxx.scm
mv xxxxx.scm $1.scm
echo vm >compilation-mode.txt
cat bin/schimper.txt compilation-mode.txt $1.scm \
  | bin/schant-int \
  >$1.vm
cat bin/simpvir-assembler.txt $1.vm | bin/schant-int >$1

rm $1.scm $1.vm
