#!/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.
#
# Name:
#	MOXA Bootloader Utility
#
# Description:
#	A set of MOXA Bootloader Tools
#
# Copyright (C) Moxa, Inc. All rights reserved.
# Copyright (C) 2025	Tsungbao TY Chang	<TsungbaoTY.Chang@moxa.com>

main_help() {
    echo
    echo "Moxa Bootloader Manager Command-line Utility"
    echo
    echo "USAGE:"
    echo "    mx-bootloader-mgmt [SUBCOMMAND] [OPTION]..."
    echo
    echo "OPTION:"
    echo "    -h, --help                      Display the help menu."
    echo "    -v, --version                   Show version information."
    echo "    -J, --json                      Show version information in JSON format. Note that this flag must be used along with \"-v\" flag."
    echo
    echo "SUBCOMMAND:"
    echo "    info                            Get bootloader basic information."
    echo "    decommission                    Reset bootloader configuration and clear all logs."
    echo "    log                             Get bootloader audit log."
    echo "    mode                            Set/Get Moxa bootloader mode (Production/Developer.)"
    echo "    upgrade                         Upgrade Moxa device's bootloader."
    echo "    image_auto_install              Auto install system image upon next boot-up."
    echo
}

decommission_help() {
    local interface_name="mx-bootloader-mgmt decommission"

    echo
    echo "Reset bootloader configuration (e.g., reset bootloader environment variables) and clear all bootloader logs upon next boot-up."
    echo
    echo "USAGE:"
    echo "    $interface_name [SUBCOMMAND] [OPTION]"
    echo
    echo "OPTION:"
    echo "    -y, --yes                       Automatic yes to prompts"
    echo "    -h, --help                      Display the help menu"
    echo "    -J, --json                      Get decommission status in JSON format. Note that this flag must be used along with \"info\" subcommand."
    echo
    echo "SUBCOMMAND:"
    echo "    info                      Get bootloader decommission status"
    echo "    set                       Set to decommission status. This will reset bootloader configuration and clear all logs upon next boot-up."
    echo "    unset                     Revoke decommission status."
    echo
}

log_help() {
    local interface_name="mx-bootloader-mgmt log"

    echo
    echo "Print bootloader audit log"
    echo
    echo "USAGE:"
    echo "    $interface_name [SUBCOMMAND] [OPTION]"
    echo
    echo "OPTION:"
    echo "    -h, --help                 Display the help menu"
    # echo "    -J, --json                 Print bootloader log in JSON format. Note that this flag must be used along with \"print\" subcommand."
    echo
    echo "SUBCOMMAND:"
    echo "    print                      Print bootloader log"
    echo
}

mode_help() {
    local interface_name="mx-bootloader-mgmt mode"

    echo
    echo "Set/Get Moxa bootloader mode (Production/Developer)"
    echo "The Secure Model's bootloader menu has two modes (Production and Developer mode) where the Production Mode is the default mode with security configuration configured to comply with IEC 62443-4-2 security level 2 standard. Developer Mode provides additional operations and configurations that should only be used during development or maintenance stage ."
    echo
    echo "USAGE:"
    echo "    $interface_name [SUBCOMMAND] [OPTION]"
    echo
    echo "OPTION:"
    echo "    -h, --help                Display the help menu"
    echo "    -J, --json                Get current mode of bootloader (Developer or Production) in JSON format. Note that this flag must be used along with \"info\" subcommand."
    echo
    echo "SUBCOMMAND:"
    echo "    info                      Get current mode of bootloader"
    echo "    production                Set bootloader to Production mode"
    echo "    developer                 Set bootloader to Developer mode"
    echo
}

upgrade_help() {
    local interface_name="mx-bootloader-mgmt upgrade"

    echo
    echo "Upgrade the bootloader of the device with user-specified bootloader file (u-boot.bin)"
    echo
    echo "USAGE: $interface_name [OPTION]... [FILE]"
    echo
    echo "OPTION:"
    echo "  -f, --file FILE_PATH            Upgrade bootloader with specified bootloader binary file."
    echo "                                  We highly recommend you enable system failback function before performing the bootloader upgrade, given that power outage during the process may cause the device fail to boot up. Please refer to system failback feature from Moxa System Manager (MSM) tool."
    echo "  -i, --info                      Get the information of currently in-use bootloader or user-specified bootloader file."
    echo "  -l, --list                      Display the list of bootloader files downloaded on the device that are available to upgrade the machine's bootloader."
    echo "                                  This will show information (file path + bootloader version + compatible model + sha256sum + md5sum) for each bootloader file."
    echo "                                  Note that the bootloader files should be downloaded by Moxa official bootloader .deb package so that it can be detected by this tool."
    echo "  -d, --detail VERSION          Display the information (file path + bootloader version + compatible model + sha256sum + md5sum) of the corresponding bootloader whose version matches VERSION."
    echo "                                  Note that the bootloader files should be downloaded by Moxa official bootloader .deb package so that it can be detected by this tool."
    echo "  -y, --yes                       Automatic yes to prompts."
    echo "  -J, --json                      Get bootloader information in JSON format. Note that this flag must be used along with \"-i\", \"-l\" or \"-d\" flag."
    echo
    echo "  -h, --help                      Display the help menu"
    echo
    echo "Examples:"
    echo "  $interface_name -i [-J]                 # Get information of currently in-use bootloader."
    echo "  $interface_name -i -f u-boot.bin [-J]   # Get information of user-specified bootloader file u-boot.bin"
    echo "  $interface_name -f u-boot.bin           # Upgrade bootloader of the device with 'u-boot.bin'"
    echo "  $interface_name -l [-J]                 # Display the list of bootloader files downloaded on the device"
    echo "  $interface_name -d VERSION [-J]         # Show the information of the bootloader file whose version matches VERSION"
}

image_auto_install_help() {
    local interface_name="mx-bootloader-mgmt image_auto_install"

    echo
    echo "Trigger the image installation process. Once this process is triggered, the Arm-based computer will automatically install the specified system image in the USD or SD. The new image will be available upon next boot-up"
    echo
    echo "UASGE: $interface_name [OPTION]..."
    echo
    echo "OPTION:"
    echo "  -d, --disk DISK        Trigger the image installation process. Once this process is triggered, the Arm-based computer will automatically install the specified system image in the USD or SD. The new image will be available upon next boot-up."
    echo "                         Set the DISK (USB or SD) where FILE (specified by -f) is stored. Note that the name of DISK should match \"NAME\" field from \"mx-interface-mgmt disk\"."
    echo "                         The format supported for USB and SD are FAT32 and ext4, respectively."
    echo "  -f, --file FILE        Set FILE as image file. FILE must be stored in partition 1 of DISK. This will automatically install FILE upon next boot-up."
    echo "                         Make sure that the image file and sha256/512 hash files are available in partition 1 of USB or SD before triggering image auto installation process."
    echo "  -i, --info             Get DISK and FILE of \"image_auto_install\"."
    echo "  -r, --remove           Disable auto installation upon next boot."
    echo "  -J, --json             Get DISK and FILE of \"image_auto_install\" in JSON format. Note that this flag must be used along with \"-i, --info\" flag."
    echo
    echo "  -h, --help             Display the help menu"
    echo
    echo "Examples:"
    echo "  $interface_name -d SD -f IMG_UC-8200_MIL3_V1.0_Build_22042223.img  # Install Image from SD Card"
    echo "  $interface_name -d USB -f IMG_UC-8200_MIL3_V1.0_Build_22042223.img # Install Image from USB DISK"
}

info_help() {
    local interface_name="mx-bootloader-mgmt info"

    echo
    echo "Get currently in-use bootloader information"
    echo
    echo "USAGE:"
    echo "    $interface_name [SUBCOMMAND] [OPTION]"
    echo
    echo "OPTION"
    echo "    -h, --help                 Display the help menu"
    echo
    echo "SUBCOMMAND:"
    echo "    all                        Print currently in-use bootloader information"
    echo "    uboot-ver                  Print uboot version of currently in-use bootloader"
    echo
}

main() {
    case "$1" in
    "main")
        main_help
        ;;
    "decommission")
        decommission_help
        ;;
    "log")
        log_help
        ;;
    "mode")
        mode_help
        ;;
    "upgrade")
        upgrade_help
        ;;
    "image_auto_install")
        image_auto_install_help
        ;;
    "info")
        info_help
        ;;
    *)
        "${2}_help"
        exit 2
        ;;
    esac
    exit 0
}

main "$@"
