#!/bin/bash
#
# Federated Start Script
. /federated/lib/functions.sh
. /etc/federated

usage() {
  printf -v SERVICES_JOINED '%s|' "${SERVICES[@]}"
  echo "$0: ${SERVICES_JOINED%|}"
  exit 2
}
startservice_jitsi() {
  if [ "$(docker container inspect -f '{{.State.Status}}' jitsi_web_1 2>/dev/null)" = "running" ]; then
      echo -ne "* $SERVICE is already running.\n"
  else
    if [ -z "$ALERTS_EMAIL" ]; then
      spin &
      SPINPID=$!

      NC_COMMAND=`grep start_service /federated/lib/$SERVICE.sh  | awk -F\" '{ print $4 }'`
      RETRY=`grep start_service /federated/lib/jitsi.sh | awk -F\" '{ print $6 }'`
      start_service_withalert "jitsi" "$NC_COMMAND" "$RETRY" "no"

      disown $SPINPID &> /dev/null
      kill -9 $SPINPID &> /dev/null
      echo -ne "done.\n"
    else
      spin &
      SPINPID=$!

      NC_COMMAND=`grep start_service /federated/lib/$SERVICE.sh  | awk -F\" '{ print $4 }'`
      RETRY=`grep start_service /federated/lib/jitsi.sh | awk -F\" '{ print $6 }'`
      start_service_withalert "$SERVICE" "$NC_COMMAND" "$RETRY" "yes"

      disown $SPINPID &> /dev/null
      kill -9 $SPINPID &> /dev/null
      echo -ne "done.\n"
    fi
  fi
}
startservice() {
  if [ "$(docker container inspect -f '{{.State.Status}}' $SERVICE 2>/dev/null)" = "running" ]; then
    echo -ne "* $SERVICE is already running.\n"
  else
    if [ -z "$ALERTS_EMAIL" ]; then
      NC_COMMAND=`grep start_service /federated/lib/$SERVICE.sh  | awk -F\" '{ print $4 }'`
      RETRY=`grep start_service /federated/lib/$SERVICE.sh | awk -F\" '{ print $6 }'`
      start_service_withalert "$SERVICE" "$NC_COMMAND" "$RETRY" "no"
	
      disown $SPINPID &> /dev/null
      kill -9 $SPINPID &> /dev/null
      echo -ne "done."
    else
      spin &
      SPINPID=$!

      NC_COMMAND=`grep start_service /federated/lib/$SERVICE.sh  | awk -F\" '{ print $4 }'`
      RETRY=`grep start_service /federated/lib/$SERVICE.sh | awk -F\" '{ print $6 }'`
      start_service_withalert "$SERVICE" "$NC_COMMAND" "$RETRY" "yes"

      disown $SPINPID &> /dev/null
      kill -9 $SPINPID &> /dev/null
      echo -ne "done.\n"
    fi
  fi
}
startservice_all() {
  for SERVICE in "${SERVICES[@]}"; do
    FAILED="no"
      if [ "$SERVICE" = "jitsi" ]; then
        if [ "$(docker container inspect -f '{{.State.Status}}' jitsi_web_1 2>/dev/null)" = "running" ]; then
          echo -ne "* $SERVICE is already running.\n"
	else
          startservice_jitsi
	fi
      else 
        if [ "$(docker container inspect -f '{{.State.Status}}' $SERVICE 2>/dev/null)" = "running" ]; then
          echo -ne "* $SERVICE is already running.\n"
        else
          if [ -z "$ALERTS_EMAIL" ]; then
            spin &
            SPINPID=$!

            NC_COMMAND=`grep start_service /federated/lib/$SERVICE.sh  | awk -F\" '{ print $4 }'`
            RETRY=`grep start_service /federated/lib/$SERVICE.sh | awk -F\" '{ print $6 }'`
            start_service_withalert "$SERVICE" "$NC_COMMAND" "$RETRY" "no"

	    disown $SPINPID &> /dev/null
            kill -9 $SPINPID &> /dev/null
            echo -ne "done.\n"
          else
            spin &
            SPINPID=$!

            NC_COMMAND=`grep start_service /federated/lib/$SERVICE.sh  | awk -F\" '{ print $4 }'`
            RETRY=`grep start_service /federated/lib/$SERVICE.sh | awk -F\" '{ print $6 }'`
            start_service_withalert "$SERVICE" "$NC_COMMAND" "$RETRY" "yes"

	    disown $SPINPID &> /dev/null
            kill -9 $SPINPID &> /dev/null
            echo -ne "done.\n"
         fi
      fi
    fi
  done
}

#get_installedapps
[ $# != 1 ] && usage
SERVICE=$1

if [ "$SERVICE" = "jitsi" ]; then
  startservice_jitsi
elif [ "$SERVICE" = "all" ]; then
  startservice_all
elif printf '%s\0' "${SERVICES[@]}" | grep -Fxqz -- "$SERVICE"; then
  startservice
else
  usage
fi
