#!/bin/sh
# Keep the session launcher running after an unexpected failure
child=
stopping=0
stop() {
  stopping=1
  if [ -n "$child" ]; then
    kill -TERM "$child" 2>/dev/null || :
  fi
}
trap stop TERM INT HUP
while [ "$stopping" -eq 0 ]; do
  "$@" &
  child=$!
  wait "$child"
  status=$?
  if [ "$stopping" -ne 0 ]; then
    wait "$child" 2>/dev/null || :
    exit 0
  fi
  child=
  [ "$status" -eq 0 ] && exit 0
  # Allow the previous recorder to finish closing its listener and database
  sleep 3 &
  child=$!
  wait "$child" || :
  child=
done
