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

export GENERAL_VARIABLES="/lib/moxa-system-manager/general_variables"
source "${GENERAL_VARIABLES}"
export GENERAL_FUNCTIONS="/lib/moxa-system-manager/general_functions"
source "${GENERAL_FUNCTIONS}"

export MSM_FLAG_YES="y"

main() {
	local main_command
	local sub_command
	local ret_val

	# main commands
	case "${1}" in
	refresh-upgrade | --refresh-upgrade)
		main_command="refresh_upgrade"
		case "${2}" in
		restore)
			sub_command="${2}"
			shift 2
			;;
		help | -h | --help)
			refresh_upgrade_help_menu
			exit 0
			;;
		"")
			refresh_upgrade_help_menu
			exit "${_ERR_EMPTY_COMMAND}"
			;;
		*)
			refresh_upgrade_help_menu
			exit "${_ERR_COMMAND}"
			;;
		esac
		;;
	version | --version | -v)
		version
		exit 0
		;;
	help | -h | --help)
		helper_help_menu
		exit 0
		;;
	"")
		helper_help_menu
		exit "${_ERR_EMPTY_COMMAND}"
		;;
	*)
		helper_help_menu
		exit "${_ERR_COMMAND}"
		;;
	esac

	while [ -n "${1}" ]; do
		# options
		case "${1}" in
		-h | --help)
			eval "${main_command}_help_menu"
			exit 0
			;;
		-y | --yes)
			MSM_FLAG_YES="y"
			shift 1
			;;
		*)
			eval "${main_command}_help_menu"
			exit "${_ERR_COMMAND}"
			;;
		esac
	done

	_initial_dir

	case "${main_command}" in
	refresh_upgrade)
		/lib/moxa-system-manager/${main_command} "${sub_command}"
		ret_val="${?}"
		;;
	*)
		helper_help_menu
		exit "${_ERR_COMMAND}"
		;;
	esac

	exit "${ret_val}"
}

main "${@}"

exit 0
