#!/bin/bash

# moriod - A cli wrapper around the Morio distribution

SRV="[srv]"
bold=$(tput bold)
base=$(tput sgr0)

show_help() {
  echo    "Usage:"
  echo    "  moriod [options]"
  echo    "  moriod [command]"
  echo    "  moriod [service] [command]"
  echo    ""
  echo    "Options:"
  echo    "  -h, --help         Display this help message"
  echo    "  -v, --version      Display version information"
  echo    ""
  echo    "Commands:"
  echo    "  restart            Restart the moriod service"
  echo    "  restartall         Stop all morio containers (except core) and restart moriod"
  echo    "  start              Start the moriod service"
  echo    "  status             Show the status of the moriod service"
  echo    "  stop               Stop the moriod service"
  echo    "  stopall            Stop all morio containers (except core) and stop moriod"
  echo    ""
  echo    "Service Commands:"
  echo -e "  Note: Replace '$SRV' with the name of the service, eg: 'api' or 'broker'"
  echo    ""
  echo    "  [api|core|ui] Service Commands:"
  echo -e "    $SRV info       Show info about the service running inside the container"
  echo -e "    $SRV reload     Reload the service running inside the container"
  echo -e "    $SRV status     Show the status of service inside the container"
  echo    ""
  echo    "  [api|broker|ca|connector|console|core|dbuilder|ui] Service Commands:"
  echo -e "    $SRV  inspect   Inspect the service container"
  echo -e "    $SRV  kill      Terminate the service container (not available on core)"
  echo -e "    $SRV  logs      Follow the service container logs"
  echo    ""
  echo    "  [core] Service Commands:"
  echo    "    core reseed     Reseed the Morio core service"
  exit 0
}

show_version() {
  cat /etc/morio/moriod/version
  exit 0
}

change_core_state() {
  local STATE="$1"
  systemctl $STATE moriod
}

check_your_privilege() {
  if [ "$EUID" -eq 0 ]; then
    true
  else
    echo "Morio requires elevated privileges."
    echo "Please use ${bold}sudo moriod $1 $2${base}, or run this command as root."
    exit 1
  fi
}

stop_all_containers() {
  echo "Stopping all morio containers (except core)..."
  # Get all container names starting with morio- but not morio-core
  local CONTAINERS=$(docker ps -a --filter "name=morio-*" --format "{{.Names}}" | grep -v "^morio-core$")

  if [ -z "$CONTAINERS" ]; then
    echo "No morio containers found to stop."
  else
    for CONTAINER in $CONTAINERS; do
      echo "Stopping $CONTAINER..."
      docker rm -f "$CONTAINER"
    done
    echo "All morio containers (except core) have been stopped."
  fi
}

run_pm2_command() {
  local CONTAINER="$1"
  local COMMAND="$2"
  case $CONTAINER in
    api|core|ui)
    case $COMMAND in
      info|reload|status)
      docker exec -it morio-$CONTAINER $COMMAND
      exit 0
      ;;
    esac
  esac
  show_help
}

run_container_command() {
  case $1 in
    api|broker|ca|connector|console|core|dbuilder|ui)
    case $2 in
      inspect)
      docker inspect morio-$1
      exit 0
      ;;
      logs)
      journalctl CONTAINER_NAME=morio-$1 -o cat
      exit 0
      ;;
      kill)
      if [ "$1" = "core" ]; then
        echo "The morio-core container should not be killed. Run ${bold}moriod stop${base} instead."
        exit 1
      else
        docker rm -f morio-$1
        exit 0
      fi
      exit 0
      ;;
      reseed)
      if [ "$1" = "core" ]; then
        docker exec -it morio-core reseed
        exit 0
      fi
      ;;
    esac
  esac
  show_help
}

# Parse command-line options
while [[ $# -gt 0 ]]; do
  case $1 in
    -h|--help)
      show_help
      ;;
    -v|--version)
      show_version
      ;;
    start)
      check_your_privilege $@
      change_core_state start
      exit 0
      ;;
    stop)
      check_your_privilege $@
      change_core_state stop
      exit 0
      ;;
    restart)
      check_your_privilege $@
      change_core_state restart
      exit 0
      ;;
    stopall)
      check_your_privilege $@
      stop_all_containers
      change_core_state stop
      exit 0
      ;;
    restartall)
      check_your_privilege $@
      stop_all_containers
      change_core_state restart
      exit 0
      ;;
    status)
      check_your_privilege $@
      change_core_state status
      exit 0
      ;;
    api|broker|ca|connector|console|core|dbuilder|ui)
      check_your_privilege $@
      case $2 in
        info|reload|status)
          run_pm2_command $@
          exit 0
          ;;
        inspect|kill|logs)
          run_container_command $@
          exit 0
          ;;
        reseed)
          run_container_command $@
          exit 0
          ;;
      esac
      show_help
      exit 1
      ;;
    *)
      show_help
      exit 1
  esac
done

# If no options were provided, show the general help
if [ $# -eq 0 ]; then
  show_help
fi
