#!/bin/bash
#
# Copyright (C) MOXA Inc. All rights reserved.
# This software is distributed under the terms of the MOXA SOFTWARE NOTICE.
# See the file MOXA-SOFTWARE-NOTICE for details.
#
# Authors:
# 	2021	Wes Huang	<Wes.Huang@moxa.com>

if [ -f "${GENERAL_VARIABLES}" ]; then
	source "${GENERAL_VARIABLES}"
fi
if [ -f "${GENERAL_FUNCTIONS}" ]; then
	source "${GENERAL_FUNCTIONS}"
fi

system_failback_enable() {
	local create_time="${1}"
	local state
	local ret_val

	_log_msg "debug" "${0}, ${FUNCNAME[0]}"

	if [ "${MSM_OPTION_SIZE}" == "y" ]; then
		_calculate_transferred_size "replica" "y" "y"
		return 0
	fi

	if [ "${MSM_OPTION_MODE}" == "hot" ]; then
		_log_msg "info" "It is recommended to use cold creation mode 'mx-system-mgmt system-failback enable --cold'"
	fi

	state=$(mx-system-mgmt --system-failback state --value)
	if [ "${state}" == "${_STATE_ENABLED}" ]; then
		_check_info_file "replica"
		ret_val="${?}"
		if [ "${ret_val}" == 0 ]; then
			_show_info "replica"
			_log_msg "info" "System failback has been enabled and replica has been created. This will overwrite the existing replica."
			_question "${MSM_FLAG_YES}" "Would you like to continue? (y/N)"
			_remove_info "replica"
		fi
	elif [ "${state}" == "${_STATE_BOOTING}" ] || [ "${state}" == "${_STATE_RECOVERY}" ]; then
		_log_msg "warn" "The system failback function has been enabled. Plase check first."
	fi

	_calculate_transferred_size "replica" "${MSM_FLAG_YES}" "n"

	_create "replica" "${MSM_OPTION_MODE}"
	if [ "${MSM_OPTION_MODE}" == "cold" ]; then
		_set_cold_creation_flag "replica"
		_log_msg "info" "Please reboot the device to continue the process."
	else
		_set_env replica 1
		_create_info "replica" "${create_time}" "${REPLICA_DIR}"
		_show_info "replica"
		_log_msg "info" "The system failback has been enabled and the replica has been created successfully."
	fi
}

system_failback_state() {
	local ret_val
	local replica

	replica=$(_get_env replica)

	_log_msg "debug" "${0}, ${FUNCNAME[0]}"
	_log_msg "debug" "MSM_OPTION_MODE=${MSM_OPTION_MODE}"

	if [ "${MSM_OPTION_VALUE}" == "y" ]; then
		_log_msg "info" "${replica}"
	else
		_log_msg "info" "Value: ${replica}"
		case "${replica}" in
		"${_STATE_DISABLED}")
			_log_msg "info" "System Failback State: Disabled"
			_log_msg "info" "Description: The system failback function is disabled."
			;;
		"${_STATE_ENABLED}")
			_log_msg "info" "System Failback State: Enabled"
			_log_msg "info" "Description: Replica has been prepared and system failback function is enabled."
			;;
		"${_STATE_BOOTING}")
			_log_msg "info" "System Failback State: Booting"
			_log_msg "info" "Description: The system failback function is enabled and the system has been rebooted. Checking system boot is completed or not."
			;;
		"${_STATE_RECOVERY}")
			_log_msg "info" "System Failback State: Recovery"
			_log_msg "info" "Description: The system failback function is enabled and some incorrect errors occurred during system startup. Please check the environment first."
			;;
		*)
			_log_msg "info" "System Failback State: Unknown"
			_log_msg "info" "Description: Some incorrect errors have occurred in the system. Please check the environment first."
			;;
		esac
	fi
}

system_failback_restore() {
	local state=""

	_log_msg "debug" "${0}, ${FUNCNAME[0]}"

	state=$(mx-system-mgmt --system-failback state --value)
	if [ "${state}" == "${_STATE_DISABLED}" ]; then
		_log_msg "warn" "The system failback is disabled and the replica is not exist."
		exit "${_ERR_NO_SYSTEM_FAILBACK}"
	else
		_question "${MSM_FLAG_YES}" "Would you like to continue? (y/N)"
	fi

	# start to restore replica to working system
	_restore "replica_restore"
	_set_env replica 0
	_set_flag "replica_restore"
	rm -f "${REPLICA_DIR}/info"

	_log_msg "info" "System has been restored successfully. Reboot is required to take effect."
}

system_failback_disable() {
	local ret_val
	local state=""

	_log_msg "debug" "${0}, ${FUNCNAME[0]}"

	state=$(mx-system-mgmt --system-failback state --value)
	if [ "${state}" == "${_STATE_DISABLED}" ]; then
		_log_msg "warn" "System failback is currently not enabled on the system."
		exit "${_ERR_NO_SYSTEM_FAILBACK}"
	else
		_log_msg "info" "This will disable system failback and delete the existing replica."
		_question "${MSM_FLAG_YES}" "Would you like to continue? (y/N)"
	fi

	# delete replica
	rm -rf "${REPLICA_DIR}"
	_set_env replica 0

	_log_msg "info" "The system failback has been disabled and the replica has been deleted successfully."
}

main() {
	local sub_command="${1}"
	local create_time
	local replica

	create_time="$(date +%s)"

	_log_msg "debug" "${0}, ${FUNCNAME[0]}"
	_log_msg "debug" "sub_command=${sub_command}, create_time=${create_time}"
	_log_msg "debug" "MSM_OPTION_MODE=${MSM_OPTION_MODE}, MSM_OPTION_SIZE=${MSM_OPTION_SIZE}, MSM_FLAG_YES=${MSM_FLAG_YES}"
	_log_msg "debug" "MSM_OPTION_VALUE=${MSM_OPTION_VALUE}"

	case "${sub_command}" in
	enable)
		system_failback_enable "${create_time}"
		;;
	restore)
		system_failback_restore
		;;
	disable)
		system_failback_disable
		;;
	info)
		_check_info_file "replica" && _show_info "replica" || exit ${?}
		;;
	state)
		system_failback_state
		;;
	*)
		usage
		;;
	esac
}

main "${@}"

exit 0
