#!/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

snapshot_create() {
	local create_time="${1}"
	local ret_val

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

	if [ "${MSM_OPTION_SIZE}" == "y" ]; then
		_calculate_transferred_size "snapshot" "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 snapshot create --cold'"
	fi

	_check_info_file "snapshot"
	ret_val="${?}"

	if [ "${ret_val}" == 0 ]; then
		_show_info "snapshot"
		_question "${MSM_FLAG_YES}" "Do you want to overwrite the existing snapshot shown above? (y/N)"
		_remove_info "snapshot"
	fi

	_calculate_transferred_size "snapshot" "${MSM_FLAG_YES}" "n"

	_create "snapshot" "${MSM_OPTION_MODE}"
	if [ "${MSM_OPTION_MODE}" == "cold" ]; then
		_set_cold_creation_flag "snapshot"
		_log_msg "info" "Please reboot the device to continue the process."
	else
		_create_info "snapshot" "${create_time}" "${SNAPSHOT_DIR}"
		_show_info "snapshot"
		_log_msg "info" "The snapshot has been created successfully."
	fi
}

snapshot_restore() {
	local state

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

	_check_info_file "snapshot" && _show_info "snapshot" || exit ${?}
	_check_min_req_tool_ver "snapshot"
	state=$(mx-system-mgmt --system-failback state --value)
	if [ "${state}" != "${_STATE_DISABLED}" ]; then
		_log_msg "info" "System failback has been enabled and the replica has been created on your system. Continue with restore will disable system failback and remove replica."
		_question "${MSM_FLAG_YES}" "Would you like to continue? (y/N)"
		mx-system-mgmt --system-failback disable -y
	fi

	_calculate_transferred_size "snapshot_restore" "${MSM_FLAG_YES}"

	_log_msg "info" "Start processing..."
	# start to restore the snapshot point
	_restore "snapshot_restore"
	_set_flag "snapshot_restore"

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

snapshot_delete() {
	_log_msg "debug" "${0}, ${FUNCNAME[0]}"

	_check_info_file "snapshot" && _show_info "snapshot" || exit ${?}
	_question "${MSM_FLAG_YES}" "Would you like to continue? (y/N)"

	rm -rf "${SNAPSHOT_DIR}"

	sync
	_log_msg "info" "The snapshot has been deleted successfully."
}

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

	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}"

	case "${sub_command}" in
	create)
		snapshot_create "${create_time}"
		;;
	restore)
		snapshot_restore
		;;
	delete)
		snapshot_delete
		;;
	info)
		_check_info_file "snapshot" && _show_info "snapshot" || exit ${?}
		;;
	*)
		usage
		;;
	esac
}

main "${@}"

exit 0
