#!/bin/bash
set -eu

source use-common.sh

check_bash_version
check_gnu_grep_installed
check_gnu_sed_installed
check_yq_installed
check_git_docker_compose_commands_exist

cache_global_vars

echo "[NOTICE] WITH_SUDO=${with_sudo}"

if [[ "${with_sudo}" == "true" ]]; then sudo sed -i -e "s/\r$//g" $(basename $0); else sed -i -e "s/\r$//g" $(basename $0); fi

echo "[NOTICE] To prevent CRLF errors in scripts based on the Windows operating system, currently performing CRLF to LF conversion."
if [[ "${with_sudo}" == "true" ]]; then sudo bash prevent-crlf.sh; else bash prevent-crlf.sh; fi
git config apply.whitespace nowarn || echo "[WARN] A supporting command 'git config apply.whitespace nowarn' has NOT been run."
git config core.filemode false || echo "[WARN] A supporting command 'git config core.filemode false' has NOT been run."


source ./use-app.sh

check_git_docker_compose_commands_exist
cache_global_vars

with_edge_router_nginx="${1:-}"
if [[ "$with_edge_router_nginx" == "1" ]]; then
    nginx_restart=true
else
    nginx_restart=false
fi


# App rollback
echo "[NOTICE] Change the previous app image to the ${new_state} image for rollback."
docker tag ${project_name}:previous ${project_name}:${new_state} || echo "[NOTICE] ${project_name}:previous image does NOT exist."

sleep 2

echo "[NOTICE] Down & Up ${new_state} container."
docker-compose -f docker-compose-${project_name}.yml stop ${project_name}-${new_state} || echo "[NOTICE] The ${new_state} Container has been stopped, if exists."
docker-compose -f docker-compose-${project_name}.yml rm -f ${project_name}-${new_state} || echo "[NOTICE] The ${new_state} Container has been removed, if exists."
docker-compose -f docker-compose-${project_name}.yml up -d ${project_name}-${new_state}

echo "[NOTICE] Wait until the ${new_state} container is fully up."
if [[ $(check_availability_inside_container ${new_state} 60 5 | tail -n 1) != 'true' ]]; then
  echo "[ERROR] Failed to rollback to the ${new_state} container." && exit 1
fi

# Rollback Process
if [[ ${nginx_restart} == 'true' ]]; then
  echo "[NOTICE] Change the 'previous' tagged Nginx image to the 'latest' tagged image."
  docker tag ${project_name}-nginx:previous ${project_name}-nginx:latest || echo "[NOTICE] ${project_name}-nginx:previous image does NOT exist."

  sleep 2

  echo "[NOTICE] Run 'emergency-nginx-restart.sh' "
  bash emergency-nginx-restart.sh
else
  echo "[NOTICE] Nginx will NOT be restarted, as ${nginx_restart} = false"
fi

./nginx-blue-green-activate.sh ${new_state} ${state} ${new_upstream}


if [[ ${orchestration_type} != 'stack' ]]; then
  echo "[NOTICE] Stop the previous ${state} container."
  docker-compose -f docker-${orchestration_type}-${project_name}.yml stop ${project_name}-${state}
else
  echo "[NOTICE] Remove the previous ${state} stack."
  docker stack rm ${project_name}-${state}
fi
