#!/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 [ -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."
    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."
    fi
}
startservice() {
    echo -ne "* Starting $SERVICE.."
    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."
    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 }'`
      eval start_service_withalert "$SERVICE" "$NC_COMMAND" "$RETRY" "yes"

      disown $SPINPID &> /dev/null
      kill -9 $SPINPID &> /dev/null
      echo -ne "done.\n"
    fi
}
startservice_all() {
  for SERVICE in "${SERVICES[@]}"; do
    # If app isn't installed then skip
    [ ! -d "/federated/apps/$SERVICE" ] && echo "* $SERVICE not installed, skipping." && continue

    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
}
startservice_coreapp() {
  echo "* Starting $1."
  NC_COMMAND=`grep start_service /federated/lib/$1.sh  | awk -F\" '{ print $4 }'`
  RETRY=`grep start_service /federated/lib/$1.sh | awk -F\" '{ print $6 }'`
  eval start_service_withalert2 "$1" "$NC_COMMAND" "$RETRY" "no"
}
startservice_all_new() {
  echo "* Starting $1."
  NC_COMMAND=`grep start_service /federated/lib/$1.sh  | awk -F\" '{ print $4 }'`
  RETRY=`grep start_service /federated/lib/$1.sh | awk -F\" '{ print $6 }'`
  eval start_service_withalert2 "$1" "$NC_COMMAND" "$RETRY" "no"
}

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

if [ "$SERVICE" = "jitsi" ]; then
  # If jitsi is already running then skip
  [ "$(docker container inspect -f '{{.State.Status}}' jitsi_web_1 2>/dev/null)" = "running" ] && echo "* $SERVICE is already running." && exit 2
  startservice_jitsi
elif [ "$SERVICE" = "all" ]; then
  # Starting all apps
  # First start the CORE_APPS one by one
  for i in "${CORE_APPS[@]}"; do
    [ ! -d "/federated/apps/$i" ] && echo "* $i not installed, skipping." && continue
    [ "$(docker container inspect -f '{{.State.Status}}' $i 2>/dev/null)" = "running" ] && echo "* $i is already started." && continue
    startservice_coreapp "$i"
  done
    
  # Next start EXTRA_APPS in parallel
  for i in "${EXTRA_APPS[@]}"; do
    [ ! -d "/federated/apps/$i" ] && echo "* $i not installed, skipping." && continue
    [ "$(docker container inspect -f '{{.State.Status}}' $i 2>/dev/null)" = "running" ] && echo "* $i is already started." && continue
    [ "$i" = "jitsi" ] && [ "$(docker container inspect -f '{{.State.Status}}' jitsi_web_1 2>/dev/null)" = "running" ] && echo "* $i is already started." && continue
    startservice_all_new "$i" &
  done

  wait
  exit 0
elif printf '%s\0' "${SERVICES[@]}" | grep -Fxqz -- "$SERVICE"; then
  # If app is already running then skip
  [ "$(docker container inspect -f '{{.State.Status}}' $SERVICE 2>/dev/null)" = "running" ] && echo "* $SERVICE is already running." && exit 2
  startservice
else
  usage
fi
