#!/bin/sh
#
# Usage: admit1 COMMAND [ARGUMENTS...]
# Usage: admit1 SHELL_EVAL_EXPRESSION
# Usage: env ADMIT1_DENIED=... admit1 ...
#
# Admits only one instance of the given command to run at a time by running
# it only if there are no other instances of this program already doing so.
# Otherwise, evaluates the optional ADMIT1_DENIED expression, which can be
# used for special handling such as sending a signal to the caller process.
#
# ---
# source: https://github.com/sunaku/home/blob/master/bin/admit1

regex=$(ps ho cmd $$ | sed 's/[\^.[$()|*+?{\\]/\\&/g') # see `man 7 regex`

# if already running, don't admit
test $$ = "$(pgrep -u "${USER}" -xf "${regex}")" || set -- "${ADMIT1_DENIED}"

# SHELL_EVAL_EXPRESSION was given
test "$#" -eq 1 && set -- eval "$@"

# execute the given command line
"$@"
