#! /bin/sh
# 
# This script builds the examples for the directory given as the argument.
# The full format is:
# mkex toolsdirectory directorytoprocess architecture makecommand...
#
# It is intended that this is called by buildex; it is not user-friendly.
#
# Get the library type:
#echo $2 
TOOLS="$1"
DIR="$2"
MARCH="$3"
COMM=""
shift 
shift
shift
if [ -r "$DIR/extype" ]; then 
EXT=`sed s%ARCH%$MARCH%g $DIR/extype`
EXTB=`cat $DIR/extype`
else
exit 0
fi

HASMAKE=`test -r $DIR/makefile`
if [ -r $DIR/makefile ]; then
    OK=dummy
else
    exit 0
fi

COMMTYPE=""
if [ -r "$DIR/excomm" ] ; then
    COMMTYPE="COMM=`cat $DIR/excomm`"
fi
# Wait until the directory is available
#while $TOOLS/bin/lockdir $DIR $MARCH ; do
#sleep 60 ;
#done
# Execute the rest of the command line.  We use "make clean" to remove any
# object files (in case they are the wrong type).
echo "[$DIR] about to:"
echo "       $* $COMMTYPE $EXT"
( cd $DIR ; $* clean ; \
  $* $COMMTYPE $EXT ; \
  cnt=0; \
  for i in $EXT ; do \
  ARGS=$i ; \
  if [ -r exargs ] ; then \
      jcnt=0; for j in $EXTB ; do \
	if [ $jcnt = $cnt ] ; then \
	    break ; fi ;\
        jcnt=`expr $jcnt + 1` ; \
	done ; \
      ARGS=`grep $j exargs | sed s%ARCH%$MARCH%g` ; \
  fi ; \
  echo "[$DIR] Running $ARGS..." ; \
  $ARGS ; \
  cnt=`expr $cnt + 1` ; \
  done ; $* clean ; )
# free the directory for others
#$TOOLS/bin/unlockdir $DIR
