#!/usr/bin/env bash
set -eo pipefail

if [[ "$1" == "-h" || "$1" == "--help" ]]; then
    cat <<EOF
Usage: $(basename "$0") [-h] <pid>

Wait for a process to complete.

Options:
    -h, --help    Show this help message

Arguments:
    pid           Process ID to wait for

EOF
    exit 0
fi

if hash systemd-inhibit 2>/dev/null; then
    systemd-inhibit \
        --who=waitfor \
        --why="Awaiting PID $1" \
        tail --pid="$1" -f /dev/null
else
    tail --pid="$1" -f /dev/null
fi
