#!/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:
# 	2023	Wes Huang	<Wes.Huang@moxa.com>

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

_check_refresh_upgrade() {
	local rootfs_sqfs_file=""
	local rootfs_sqfs_hash_signed_file=""
	local itb_file=""

	itb_file=$(basename "$(ls "${BOOT_DIR}")")
	rootfs_sqfs_file=$(basename "${ROOTFS_SQAUSHFS}")
	rootfs_sqfs_hash_signed_file=$(basename "${ROOTFS_SQAUSHFS_HASH_SIGNED}")

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

	if [ ! -f "${REFRESH_UPGRADE_DIR}/${rootfs_sqfs_file}" ]; then
		_log_msg "error" "There is no ${rootfs_sqfs_file} file."
		exit "${_ERR_NO_REFRESH_UPGRADE_SQFS}"
	fi

	if [ ! -f "${REFRESH_UPGRADE_BOOT_DIR}/${itb_file}" ]; then
		_log_msg "error" "There is no ${itb_file} file."
		exit "${_ERR_NO_REFRESH_UPGRADE_ITB}"
	fi

	if [ ! -f "${REFRESH_UPGRADE_DIR}/${rootfs_sqfs_hash_signed_file}" ]; then
		_log_msg "warn" "There is no ${rootfs_sqfs_hash_signed_file} file, the system environment is insecure."
	fi

	mkdir -p "${REFRESH_UPGRADE_DIR}"/{boot,rootfs,docker,bootloader}
}

_check_preserved() {
	local orig_file_path=""
	local target_file_path=""
	local preserved_file=""

	if [ -f "${PRESERVED_DIR}/preserved.conf" ]; then
		while IFS=" " read -r orig_file_path target_file_path; do
			preserved_file="${PRESERVED_DIR}/${orig_file_path}"

			if [ ! -f "${preserved_file}" ]; then
				_log_msg "error" "The ${preserved_file} file is not exist"
				exit "${_ERR_NO_PRESERVED_FILE}"
			fi
			if [ "${target_file_path}" == "" ]; then
				_log_msg "error" "The preserved configuration file is not in the correct format."
				exit "${_ERR_INCORRECT_PRESERVED_CONFIG}"
			fi
		done <"${PRESERVED_DIR}/preserved.conf"
	fi
}

refresh_upgrade_restore() {
	local state

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

	# ToDo Check file

	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

	_check_refresh_upgrade
	_check_preserved

	_log_msg "info" "To restore the refresh-upgrade will overwrite current system."
	_question "${MSM_FLAG_YES}" "Do you want to continue? (y/N)"

	_log_msg "info" "Start processing..."
	rm -rf "${RESTORE_DIR}"
	mv "${REFRESH_UPGRADE_DIR}" "${RESTORE_DIR}"

	_log_msg "info" "Synchronize boot files..."
	_rsync "${RESTORE_DIR}/boot" "${WORKING_BOOT_DIR}"
	# start to do refresh upgrade
	_set_flag "refresh_upgrade_restore"
	_set_preserved_flag "refresh_upgrade"

	_log_msg "info" "Reboot is required to take effect."
}

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

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

	case "${sub_command}" in
	restore)
		refresh_upgrade_restore
		;;
	*)
		refresh_upgrade_help_menu
		;;
	esac
}

main "${@}"

exit 0
