#! sh
#
# Frontend for fd2inline.
#
# Usage: genstubs.sh fddir incdir destdir
#
# generates link libraries containing MorphOS stubs for all libraries
# found in 'fddir'. 'incdir' should contain a matching
# 'clib/???_protos.h' for all ???_lib.fd files.
#

#---- configure this ------

CC=ppc-amigaos-gcc         #add -Isomepath/include if needed
AWK=gawk
AR=ppc-amigaos-ar
RANLIB=ppc-amigaos-ranlib
TMP=/t                     # should be in ram:, or things get really slow

#--------------------------

FDDIR=$1
INCDIR=$2
DESTDIR=$3

if [ -d $DESTDIR ]; then true; else mkdir $DESTDIR; fi
if [ -d $TMP/genstubstmp ]; then rm -rf $TMP/genstubstmp; fi
mkdir $TMP/genstubstmp
TMP=$TMP/genstubstmp

for fd in $FDDIR/*.fd; do
	rootname=`basename $fd _lib.fd`;
	libname=$rootname
	if test $libname = muimaster; then
	    libname=mui
	fi
	echo "making lib$libname.a";
	../emultools/fd2inline/fd2inline --stubs --morphos $fd $INCDIR/clib/${rootname}_protos.h -o $TMP/$rootname.h;
	grep '#include' $INCDIR/clib/${rootname}_protos.h >$TMP/$rootname.c
	grep 'typedef' $INCDIR/clib/${rootname}_protos.h >>$TMP/$rootname.c
#        if [ -f $(srcdir)/../include/proto-src/$rootname.h ]; then
#            grep '#include' $(srcdir)/../include/proto-src/$rootname.h
#              | grep -v '#include.*<clib/' | grep -v '#include.*<inline/'
#              | grep -v '#include.*<proto/' >>$TMP/$rootname.c;
#        else true;
#        fi
	echo '#include "'$rootname'.h"' >>$TMP/$rootname.c
	$CC -O2 -I$INCDIR -S -o $TMP/$rootname.asm $TMP/$rootname.c
	$AWK -v dest=$TMP -f splitasm.awk $TMP/$rootname.asm
	(cd $TMP; $CC -c *.s)
	(cd $TMP; $AR r $DESTDIR/lib$libname.a *.o)
	$RANLIB $DESTDIR/lib$libname.a
	rm -f $TMP/*
done
rm -rf $TMP

