#!/bin/bash

DAEMON="${NEST_SERVER_DAEMON:-0}"
HOST="${NEST_SERVER_HOST:-127.0.0.1}"
PORT="${NEST_SERVER_PORT:-5000}"
STDOUT="${NEST_SERVER_STDOUT:-0}"

usage() {
  echo "NEST Server"
  echo "-----------"
  echo "Usage: nest-server log|status|start|stop|restart [-d] [-h <HOST>] [-o] [-p <PORT>]"
  echo ""
  echo "Commands:"
  echo "  log         display the server log stored in /tmp/nest-server.log"
  echo "  status      display the status of all server instances"
  echo "  start       start a server instance on <HOST>:<PORT>"
  echo "  stop        stop a server instance on <HOST>:<PORT>"
  echo "  restart     restart (i.e. stop and start) a server instance on <HOST>:<PORT>"
  echo
  echo "Options:"
  echo "  -d                 daemonize the server process"
  echo "  -h <HOST>          use hostname/IP address <HOST> for the server [default: 127.0.0.1]"
  echo "  -o                 print all outputs to the console"
  echo "  -p <PORT>          use port <PORT> for opening the socket [default: 5000]"
}

log() {
  # Follow logs in /tmp/nest-server.log.
  tail -f /tmp/nest-server.log
}

pid() {
  # Get process ID of instance on defined host and port.
  pgrep -f "gunicorn nest.server.app --bind ${HOST}:${PORT}"
}

set-gunicorn_opts() {
  # Set opts for gunicorn.
  GUNICORN_OPTS="--bind ${HOST}:${PORT}"
  if [ "${DAEMON}" -eq 1 ]; then
    GUNICORN_OPTS="${GUNICORN_OPTS} --daemon"
  fi
  GUNICORN_OPTS="${GUNICORN_OPTS} --log-file /tmp/nest-server.log"
}

start() {
  # Start server instance.
  if pid > /dev/null;  then
    echo "NEST Server is already running at http://${HOST}:${PORT}."
  else
    set-gunicorn_opts
    gunicorn nest.server:app ${GUNICORN_OPTS}

    if [ "${STDOUT}" -eq 0 ]; then
      echo "NEST Server is running at http://${HOST}:${PORT}."
      if [ "${DAEMON}" -eq 0 ]; then
        read -p "Press any key to stop... "
        stop
      fi
    fi
  fi
}

status() {
  # List all processes of NEST Server.
  PS_AUX="$(ps aux | grep "[g]unicorn nest.server.app")"

  PS_CMD="$(echo ${PS_AUX} | awk '{ for(i=1;i<=NF;i++) {if ( i >= 11 ) printf $i" "}; printf "\n" }')"
  printf "HTTP-SOCKET\t\tUID\n"
  echo "${PS_CMD}" | awk '{ for(i=1;i<=NF;i++) {if ( i == 5 || i == 7 ) printf $i"\t\t"}; printf "\n" }'
}

stop() {
  # Stop server instance.
  if pid > /dev/null; then
    kill "$(pid 2>&1 | head -n 1)"
    echo "NEST Server running at http://${HOST}:${PORT} has stopped."
  else
    echo "NEST Server is not running at http://${HOST}:${PORT}."
    false
  fi
}

CMD=$1; shift
while getopts "dh:op:" opt; do
    case $opt in
        d) DAEMON=1 ;;
        h) HOST=$OPTARG ;;
        o) STDOUT=1 ;;
        p) PORT=$OPTARG ;;
    esac
done

case "$CMD" in
  log) log ;;
  pid) pid ;;
  restart) stop; sleep .5; start ;;
  start)   start ;;
  status)  status ;;
  stop)    stop ;;
  *) usage ;;
esac
