+#!/bin/bash
+
+if [ ! "$MAKE" ]; then
+ echo "Not run from within Makefile!"
+ exit 4
+fi
+
+
+# The suffix for library files (in and out)
+LIB_SUFFIX=.lib
+#######################################################################
+IFS="
+"
+
+# Clean everything up
+clean() {
+ rm -rf $LIB_DIR $ORG_OBJDIR
+}
+
+# Generate org directory containing the original library objects
+make_org() {
+ if [ ! -d "$ORG_OBJDIR" ]; then
+ echo "Buildlib: Creating directory $ORG_OBJDIR."
+ mkdir $ORG_OBJDIR
+ fi
+ tdir=$PWD
+ cd $ORG_OBJDIR
+ cat $tdir/$ORG_LIBDIR/*$LIB_SUFFIX | ldc -saq
+ cd $tdir
+ touch $ORG_OBJDIR/lastrun
+}
+
+genlib(){
+ if [ ! -d "$LIB_DIR" ]; then
+ echo -n "Creating $LIB_DIR:"
+ mkdir $LIB_DIR && echo "Ok." || echo "FAILED!"
+ fi
+
+ recipe=$1
+ lib=$LIB_DIR/`basename $recipe $RECIPE_SUFFIX`$LIB_SUFFIX
+ echo -e "\nBuildlib: Building library \"$lib\" using recipe \"$1\":"
+ rm -f $lib
+ for item in `cat $recipe| egrep -v '^[[:space:]]*(#.*)*$'`; do
+ iname=`echo $item | egrep -o '[^[:space:]].*[^[:space:]]' |sed 's/.obj$//g'`
+ echo "Buildlib: Appending \"$iname"\"
+ cat $iname >> $lib || (echo "Error! could not find:$item";exit 3)
+ done
+ cat $ENDBLOCK >> $lib
+ echo "Buildlib: Done."
+}
+
+case $1 in
+ clean)
+ clean
+ echo "Buildlib: Cleaning up."
+ ;;
+ makeorg)
+ echo "Buildlib: Splitting original libraries."
+ make_org
+ ;;
+ genlib)
+ genlib $2
+ ;;
+ *)
+ clean
+ make_org
+ genlib
+ ;;
+esac
+