#!/bin/bash

MY_CONTAINER_NAME=z2m
REMOTE_USER="root"
REMOTE_HOST="floor9.swallow-grue.ts.net"
# Reboot command
REBOOT_COMMAND="sudo reboot"
SHARED_FUNTIONS_PATH="/opt/scripts_remote/shared_functions.sh"

source "$SHARED_FUNTIONS_PATH"

# Function to wait for the server to come back online
wait_for_server() {
  local timeout=300  # 5 minutes
  local start_time=$(date +%s)
  echo "Waiting for the server to come back online..."
  while true; do
    # Ping the server to check if it is reachable
    ping -c 1 "$REMOTE_HOST" > /dev/null 2>&1
    if [ $? -eq 0 ]; then
      echo "Server is reachable. Checking SSH availability..."
      # Check if SSH is available
      ssh -o BatchMode=yes -o ConnectTimeout=5 "$REMOTE_USER@$REMOTE_HOST" "exit" > /dev/null 2>&1
      if [ $? -eq 0 ]; then
        echo "Server is back online and accessible via SSH."
        break
      fi
    fi
    local current_time=$(date +%s)
    if [ $((current_time - start_time)) -ge $timeout ]; then
      echo "Timeout reached. Server did not come back online."
      exit 1
    fi
    echo "Server is still down. Retrying in 5 seconds..."
    sleep 5
  done
}

# Check is Container is still active and servicing
_isContainerRunning $MY_CONTAINER_NAME

# Check the return code using $?
if [ $? -eq 0 ]; then
  echo "$MY_CONTAINER_NAME is up and running. Nothing to do."
  exit 0
else
  echo "$MY_CONTAINER_NAME is down, executing recovery."
  apprise -vv -t "$MY_CONTAINER_NAME down!" \
  -b "$MY_CONTAINER_NAME is down, executing recovery." \
  pover://ucz8kryp1v73dizqo7t9krq4312ewy@aaedmsfzc1wfvcifgrg6h2xwa9one2?priority=2 
  # Execute the reboot command via SSH
#  echo "Execute the reboot command via SSH"
#  ssh -o BatchMode=yes "$REMOTE_USER@$REMOTE_HOST" "$REBOOT_COMMAND"
#  echo "Check if the SSH command was successful."
  # Check if the SSH command was successful
#  if [ $? -eq 0 ]; then
#    echo "Reboot command sent successfully to $REMOTE_HOST."
#  else
#    echo "Failed to send reboot command to $REMOTE_HOST."
#    exit 1
#  fi 
  # Wait for the server to come back online
#  echo "Waiting for the server to come back online."
#  wait_for_server
#   if [ $? -eq 0 ]; then
#     echo "Server back online, restarting $MY_CONTAINER_NAME container."
#     cd /opt/appdata/.docker-compose
#     docker compose up -d z2m
#     if [ $? -eq 0 ]; then
#       echo "Sucessully restarted $MY_CONTAINER_NAME."
#     else
#       echo "Failed to restart $MY_CONTAINER_NAME."
#       exit 1
#     fi
#   else
#     exit 1
#   fi
  echo "Restarting $MY_CONTAINER_NAME container."
  cd /opt/appdata/.docker-compose
  docker compose up -d z2m
  if [ $? -eq 0 ]; then
    echo "Sucessully restarted $MY_CONTAINER_NAME."
  else
    echo "Failed to restart $MY_CONTAINER_NAME."
    exit 1
  fi
  sleep 10
  # Check is Container is still active and servicing
  _isContainerRunning $MY_CONTAINER_NAME

  if [ $? -eq 0 ]; then
    echo "$MY_CONTAINER_NAME is up and running. Recovery went ok."
  else
    echo "$MY_CONTAINER_NAME is still down, Giving up."
    apprise -vv -t "$MY_CONTAINER_NAME down!" \
    -b "$MY_CONTAINER_NAME is still down, Giving up." \
    pover://ucz8kryp1v73dizqo7t9krq4312ewy@aaedmsfzc1wfvcifgrg6h2xwa9one2?priority=2 
  fi
fi