#! /bin/bash

gcc=${1:-gcc}

host=`./config.guess`
diff=diff
case "$host" in
  # This needs gcc >=4.3 on your PATH or in /c/gcc-4.3/bin on Windows.
  # If not, you'll have to set PATH accordingly.
  *-*-mingw*) export PATH=.:/c/gcc-4.3/bin:$PATH; diff="diff --strip-trailing-cr";;
  *) export PATH=.:$PATH;;
esac

# darktjm: check that duplicate prototypes are removed
echo "#define DUMMY 99" > dummy.h
echo "extern int rand(void);" >> dummy.h
echo "extern int rand(void);" >> dummy.h

# Check that dump-ast works:

dump-ast $gcc dummy.h > dummy.ast
$diff dummy.ast - > /dev/null <<EOF
(CDeclExt (CDecl [(CStorageSpec CExtern),(CTypeSpec CIntType)] [((Just (CDeclr (Just "rand") [(CFunDeclr (Right ([(CDecl [(CTypeSpec CVoidType)] [])],0)) [])] Nothing [])),Nothing,Nothing)]) "dummy.h")
(CDeclExt (CDecl [(CStorageSpec CExtern),(CTypeSpec CIntType)] [((Just (CDeclr (Just "rand") [(CFunDeclr (Right ([(CDecl [(CTypeSpec CVoidType)] [])],0)) [])] Nothing [])),Nothing,Nothing)]) "dummy.h")

EOF

if [ $? == 0 ]; then
  echo "dump-ast: ok"
else
  echo "dump-ast: FAILED"
  rm -f dummy.*
  exit 1
fi

# Check that pure-gen works:

DUMP_AST=dump-ast pure-gen -C -ansi dummy.h
$diff dummy.pure - > /dev/null <<EOF
/* dummy.h: */
const DUMMY = 99;
extern int rand();
EOF

if [ $? != 0 ]; then
  echo "pure-gen: FAILED"
  rm -f dummy.*
  exit 1
fi

# darktjm: check that duplicate prototypes are removed
DUMP_AST=dump-ast pure-gen -C -ansi -fc dummy.h
$diff dummy.c - > /dev/null <<EOF
#include "dummy.h"

int Pure_rand()
{
  return rand();
}
EOF

if [ $? == 0 ]; then
  echo "pure-gen: ok"
else
  echo "pure-gen: FAILED"
  rm -f dummy.*
  exit 1
fi

rm -f dummy.*
exit 0
