#!/bin/bash
TMP_DIR=${TMP_DIR:-/tmp}

die() {
   echo $1 >&2
   exit 1
}

if [ $# -lt 1 ]; then
   die "Usage: stop-sandbox PORTS"
fi

exit_status=0

for port in "$@"; do
   if ! [ -d "${TMP_DIR}/$port" ]; then
      echo "MySQL test server on port $port does not exist."
      continue
   fi

   if [ -x "${TMP_DIR}/$port/stop" ]; then
      ${TMP_DIR}/$port/stop
      exit_status=$((exit_status | $?))
   else
      echo "${TMP_DIR}/$port is missing files:" >&2
      ls -la ${TMP_DIR}/$port >&2
   fi

   rm -rf ${TMP_DIR}/$port
   exit_status=$((exit_status | $?))
done

exit $exit_status
