#! sh
#
# 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-morphos-gcc -I/ade/morphos/emulinclude/includegcc"        #add -Isomepath/include if needed
AWK=gawk
AR=ar
RANLIB=ranlib
TMP=/t			# should be in ram:, or things get really slow

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

FDDIR=$1
INCDIR=$2
DESTDIR=$3
CWD=`pwd`

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 -v -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 `echo $CWD/$DESTDIR/lib$libname.a | sed -e "s,.*//,/,"` *.o)
	$RANLIB $DESTDIR/lib$libname.a
	rm -f $TMP/*
done
rm -rf $TMP

