#!/bin/sh

LOCKDIR=.confix-generator-lock-$1

wait=no
mkdir ${LOCKDIR} 2>/dev/null || wait=yes

if test "${wait}" = "yes"; then
    echo "$$: waiting"
    while test -d ${LOCKDIR}; do
        echo Lock directory "${LOCKDIR} is (still?) in place; generator running"
        sleep 1
    done
else
    # trapping condition 0 means trapping "EXIT"
    trap "rmdir ${LOCKDIR}" 0
    while read cmd; do
        echo executing ${cmd} 2>&1
        (eval ${cmd}) || { _status=$?; echo Error: ${cmd}; exit $_status; }
    done
fi
