#!/bin/zsh
D=${1:-~/service}
[[ -d $D ]] || { echo 'invalid service directory:' $D; exit 1 }
DSEC_WAIT=10
if [[ -o xtrace ]]; then
usv.check() { zsh -x ${commands[usv.check]} "$@" }
fi
touch $D/lock
(
flock -w 5 3
if ! usv.check $D; then
(
flock -u 3
setsid usv.run $D 2>&1 | logger -t svdir.${${D:t}##.} &>/dev/null
) 3</dev/null &!
while ! usv.check $D; do
DSEC_WAIT=$(( DSEC_WAIT - 1 ))
sleep '0.1s'
if ! (($DSEC_WAIT)); then
print >&2 "failed to spawn runsvdir in: $D"
exit 1
fi
done
fi
) 3<$D/lock