#!/usr/bin/env bash

if [[ -n "$1" ]]; then
  if [[ "$1" == "start" ]]; then
    shift

    impromptu_ping_server
    if [[ "$?" -eq 0 ]]; then
      echo "The Impromptu server is already running."
    else

      impromptu_ensure_server "$@"
    fi
  elif [[ "$1" == "shutdown" ]]; then
    echo -n "shutdown" | impromptu_server_connection
  elif [[ "$1" == "test" ]]; then
    echo -n "test" | impromptu_server_connection 60
  elif [[ "$1" == "foreground" ]]; then
    shift

    impromptu_ping_server
    if [[ "$?" -eq 0 ]]; then
      source impromptu server shutdown "$@"
    fi

    # NOTE: This constant should be temporary; most of the server logic will soon
    # be handled in node.
    _impromptu_start_in_foreground="true"
    impromptu_ensure_server --foreground "$@"
  fi
fi
