#!/bin/sh
set -eu

app_path="$1"
app_pid="$2"
marker_path="$3"

case "$app_pid" in
  ''|*[!0-9]*) exit 64 ;;
esac
case "$app_path" in
  /*.app) ;;
  *) exit 64 ;;
esac
case "$marker_path" in
  /*/Library/Application\ Support/ANKLE/watchdog-*.pending) ;;
  *) exit 64 ;;
esac

while /bin/kill -0 "$app_pid" 2>/dev/null; do
  /bin/sleep 1
done

if [ -f "$marker_path" ]; then
  /bin/rm -f "$marker_path"
  /bin/sleep 1
  /usr/bin/open "$app_path"
fi
