#!/bin/sh
#
# Copyright (C) 1996 The University of Oregon.  All rights reserved.
# 
# This file may be freely redistributed in its entirety with or without
# modification provided that this copyright notice is not removed.  It
# may not be sold for profit or incorporated in commercial software
# products without the prior written permission of the copyright holder.

LCCHOME=/home/users/lth/net/lcc
ARCH=sparc/solaris

TMP=/tmp/ffitmp.$$
DEFINES="-D__STDC__"
INCLUDES="-I$LCCHOME/include/$ARCH -I/usr/include -I."
output=""
VERBOSE=1

if [ $# -lt 1 ]; then
	echo "Usage: $0 filename ..."
	exit 1
fi

for i
do
  case $i in
    -q)  VERBOSE=0 ;;
    -I*) INCLUDES="$i $INCLUDES" ;;
    -D*) DEFINES="$DEFINES $i" ;;
#    -U*) DEFINES="$DEFINES $i" ;;  # WRONG
    *)   output=`basename $i .h`.ffi
	 if [ $VERBOSE -eq 1 ]; then echo "preprocessing $i"; fi
	 $LCCHOME/lib/fficpp.bin $INCLUDES $DEFINES $i > $TMP 
	 if [ $? -ne 0 ]; then echo "Aborted!"; exit; fi
	 if [ $VERBOSE -eq 1 ]; then echo "parsing $i"; fi
	 $LCCHOME/lib/ffigen.bin -target=null < $TMP > $output
	 if [ $? -ne 0 ]; then echo "Aborted!"; exit; fi
	 if [ $VERBOSE -eq 1 ]; then echo "postprocessing $i"; fi
	 awk '/#pragma macro/ {print "(macro " $3 " \"" $4 "\" \"" $5 "\")"}'\
	     $TMP >> $output
	 rm -f $TMP
    ;;
  esac
done
exit 0
