#! /bin/sh
# 
# This script builds the libraries for the directory given as the argument.
# The full format is:
# mklib toolsdirectory directorytoprocess architecture makecommand...
#
# It is intended that this is called by buildlib; it is not user-friendly.
#
# Get the library type:
echo $2 
TOOLS="$1"
DIR="$2"
MARCH="$3"
COMM=""
shift 
shift
shift
#
# We use the existence of "comtype" to find directories that we 
# should run make in
if [ -r "$DIR/comtype" ]; then 
EXT=`cat $DIR/comtype`
else
exit 0
fi
#
# A second test is to check on the makefile
HASMAKE=`test -r $DIR/makefile`
if [ -r $DIR/makefile ]; then
    OK=dummy
else
    exit 0
fi
#
# Get mansec; we use this to find some special cases
if [ -r $DIR/mansec ] ; then 
    MANSEC=`cat $DIR/mansec`
else
    MANSEC=""
fi
#
# Check for comm workers and interfaces
MAKEOPTS=""
if [ "$MANSEC" = "6" ] ; then
   if [ -r $DIR/fmain.c ] ; then
       MAKEOPTS="workers iface"
   fi
fi
#
# Check for the blas
#echo "mansec = $MANSEC, Toolsnoblas = $TOOLSNOBLAS, dir = $DIR"
if [ "$MANSEC" = "b" ] ; then
    if [ "$TOOLSNOBLAS" = "YES" ] ; then
        if [ "$DIR" = "./blas/blas1" ] ; then 
            exit 0
	fi
        if [ "$DIR" = "./blas/blas2" ] ; then 
            exit 0
	fi
        if [ "$DIR" = "./blas/blas3" ] ; then 
            exit 0
	fi
    fi
    # Check for lapack
    if [ "$TOOLSNOLAPACK" = "YES" ] ; then 
	if [ $DIR = "./lapack" ] ; then
   	    exit 0
	fi
    fi
fi
#
# Finally, some systems don't support X; we check for that case here
if [ "$TOOLSNOX" = "YES" ] ; then
    if [ -r $DIR/mansec ] ; then
        if [ $MANSEC = "x" ] ; then
            exit 0
        fi
    fi
fi
# Wait until the directory is available
# This is normally disabled; use 
#    setenv TOOLSLOCK "YES"
# to enable it (most users should NOT enable it)
# (disabled for now)
if [ "$TOOLSLOCK" = "YES" ] ; then
    while $TOOLS/bin/lockdir $DIR $MARCH ; do
	echo "Waiting for directory $DIR to unlock" ;
        sleep 60 ;
    done
fi
# Execute the rest of the command line
# echo "[$DIR] $* $MAKEOPTS"
( cd $DIR ; $* $MAKEOPTS )
# free the directory for others
if [ "$TOOLSLOCK" = "YES" ] ; then
    $TOOLS/bin/unlockdir $DIR
fi
