#! /bin/sh
# Here we establish an interlock on the installs; this allows us to run
# multiple installs by using sleep to probe the state of an install.
#
# Usage $0 <directory-to-lock> <architecture>
#
DIR=$1
ARCH=$2
# This is tricky since there is the possibility of a race condition.
# The method is the following:
# if ($1/INSTALLING | $1/INSTALLING*) exists, then exit.
# write ($1/INSTALLING$ARCH)
# if (there is $1/INSTALLING*) for anyone but me, remove installing$ARCH
# and exit.
# Otherwise, write INSTALLING and continue.
# (at end, remove INSTALLING and INSTALLING$ARCH)
#
# This returns 1 on success and 0 on failure so that it may easily be used
# in a while lockdir DIR ; do sleep 60 ; done 
if [ -r "$DIR/INSTALLING" ]; then
exit 0
fi
FTEST=`echo $DIR/INSTALLING*`
if [ "$FTEST" != "$DIR/INSTALLING*" ]; then
    exit 0
fi
cat > $DIR/INSTALLING$ARCH <<.
$ARCH
.
for FILE in $DIR/INSTALLING* 
do 
if [ "$FILE" != "$DIR/INSTALLING$ARCH" ]; then
    exit 0
fi
done
#echo "Createing INSTALLING"
cat > $DIR/INSTALLING <<.
$ARCH
.
/bin/rm -f $1/INSTALLING$ARCH
exit 1
