#!/bin/sh
TAG='studio-server=2.7'
PID=`pgrep -f "$TAG"`
STUDIO_IP=${STUDIO_IP:="127.0.0.1"}
STUDIO_PORT=${STUDIO_PORT:="9999"}
STUDIO_DEBUG=${STUDIO_DEBUG:=""}

[ ! -z "$PID" ] && pkill -f "$TAG"
mkdir -p "${0%/*}/data/log"
LOG_DIR=$(realpath "${0%/*}/data/log")

PHP="php -S $STUDIO_IP:$STUDIO_PORT \
  -d memory_limit=16M \
  -d display_errors=Off \
  -d error_reporting=E_ALL \
  -d error_log=\"$LOG_DIR/app.log\" \
  -d date.timezone=UTC \
  -d cli_server.color=1 \
  "

PHP_CLI_SERVER_WORKERS=4

if [ "$STUDIO_DEBUG" != "" ]; then
  XDEBUG_SESSION=1
  PHP_CLI_SERVER_WORKERS=1
  PHP="$PHP \
      -d xdebug.mode=profile \
      -d xdebug.start_with_request=yes \
      -d xdebug.use_compression=false \
      "
fi

$PHP "${0%/*}/app.php" -s $TAG >> "${0%/*}/data/log/app.log" 2>&1 &
pgrep -f "$TAG"
