#!/bin/bash
set -e -o pipefail -o xtrace

touch /tmp/if-absent-path
rm -f /tmp/if-absent-cleanup
../run-in-separate-pgrp \
  --forward-signals-to-group --print-signals \
  --if-absent-path=/tmp/if-absent-path --if-absent-cleanup=">/tmp/if-absent-cleanup" \
  bash -c "sleep 100 & sleep 200" &
pid=$!

sleep 0.5
(pstree -ap 2>/dev/null || pstree) | grep -E "perl|sleep" | grep -v grep

rm -f /tmp/if-absent-path

sleep 1
if kill -0 $pid 2>/dev/null; then
  echo "Process $pid is still alive"
  exit 1
fi

set +e
wait $pid
code=$?
set -e
test "$code" -eq 143 # 128 + SIGTERM

test -f /tmp/if-absent-cleanup

echo "ok"
