#!/bin/sh

##
## Copyright 2012,2016 Cumulus Networks Inc.
##
## Select an ONIE reboot mode.  Requires a reboot to take effect.
##

command=$(basename "$0")
if [ "$command" = "cl-img-select" ] ; then
        echo "WARNING: '$command' is deprecated and will be removed in a future release."
        echo "WARNING: Please use 'onie-select' instead of this command."
fi

help_brief="[-d] [-f] [-r] [-i] [-k] [-p] [-c] [-n]"
help_summary="This command selects the ONIE mode to use during the next reboot."
help_args=$(cat <<EOF

-d
        Display any pending reboot operations.  This is the default if
        no other operations are specified.

-f
        Force the operation.  Assumes "yes" to any questions the
        script might ask.

-r
        Rescue Boot -- At the next reboot, load ONIE in 'rescue' mode
        for debug purposes.

-i
        Install Boot -- At the next reboot, load ONIE in 'install'
        mode.  This installs a new operating system, overwriting the
        current system.

	WARNING: This is a destructive operation.  Back up your data
	first.

-k
        Uninstall Boot -- At the next reboot, load ONIE in 'uninstall'
        mode.  This will wipe out the current the operating system and
        then return ONIE to 'install' mode.

	WARNING: This is a destructive operation.  Back up your data
	first.

-p
        Update Boot -- At the next reboot, load ONIE in 'self update'
        mode.  This is used to update ONIE itself.  The installed
        operating system is not affected.

-c
        Cancel pending reboot operation -- Use this to cancel a
        pending resue boot, install boot, uninstall boot or update boot.

-n
        Dry run.  Show what would happen, but do not actually modify
        anything.

EOF
)
help_body=$(cat <<EOF
RESCUE BOOT:

Reboot the system and load ONIE in 'rescue' mode.  Using rescue mode
one can attempt system repair operations, while running from a simple
initramfs.

INSTALL BOOT:

Reboot the system and load ONIE in 'install' mode.  This is a quick
install method, without wiping out the mass storage media, used for
installing a new operating system.

*WARNING* -- The install command (-i) is destructive and will destroy
all of your data and the current operating system.  Back up your data
before using.

UNINSTALL:

Reboot the system and load ONIE in 'uninstall' mode.  The uninstaller
wipes all user data from the mass storage media and then returns to
ONIE 'install' mode.

This is a rather lengthy operation, during which the system first
writes random data to the entire mass storage media.

*WARNING* -- The uninstall command (-k) is destructive and will remove
all of your data and the operating system.  Back up your data before
using.

UPDATE:

*WARNING* -- The update command (-p) will update the boot loader.  Only
do this if you know what you are doing.

Reboot the system and load ONIE in 'update' mode.  In this mode, ONIE
attempts to download a new ONIE verseion from the network and install
it.  The operating system is not affected by this operation.

EOF
)

. /usr/share/cumulus/img/functions
cl_check_env root-priv

uninstall=n
rescue=n
force=n
install=n
update=n
cancel=n
dry_run=n
display=y
command=display
log_pre=
cl_add_arg "dfrikcnp"
while getopts "${cl_args}" a ; do
    case $a in
        d)
            command=display
            ;;
        f)
            force=y
            ;;
        r)
            # ONIE rescue mode
            command=rescue
            ;;
        i)
            # ONIE install mode
            command=install
            ;;
        k)
            # ONIE uninstall mode
            command=uninstall
            ;;
        p)
            # ONIE update mode
            command=update
            ;;
        c)
            command=cancel
            ;;
        n)
            dry_run=y
            log_pre="Dry run: "
            printf "${log_pre}No changes will be made.\n"
            ;;
    esac
done

cl_get_args "$@"
shift $cl_arg_shift

display_info()
{
    reboot_cmd=$(cl_reboot_cmd_get)
    text="Cumulus Linux"
    if [ -n "$reboot_cmd" ] ; then
        case "$reboot_cmd" in
            install)
                text="ONIE install mode"
                ;;
            uninstall)
                text="ONIE uninstall mode"
                ;;
            rescue)
                text="ONIE rescue mode"
                ;;
            update)
                text="ONIE update firmware mode"
                ;;
            embed)
                text="ONIE re-install firmware mode"
                ;;
            *)
                text="Unknown $reboot_cmd"
        esac
    fi
    printf "Boot mode: $text\n"
}

if [ "$command" = "rescue" ] ; then
    if [ "$force" != "y" ] ; then
        printf "WARNING:\n"
        printf "WARNING: ONIE rescue mode requested.\n"
        printf "WARNING:\n"
        cl_prompt_yes_no "Are you sure" || exit 0
    fi
    log_begin_msg "${log_pre}Enabling ONIE rescue mode at next reboot"
    cl_reboot_cmd_set "rescue"
    log_end_msg
    if [ "$dry_run" != "y" ] ; then
        _log_msg "Reboot required to take effect.\n"
    fi
elif [ "$command" = "install" ] ; then
    if [ "$force" != "y" ] ; then
        printf "WARNING:\n"
        printf "WARNING: ONIE install mode requested.\n"
        printf "WARNING: This will wipe out all system data.\n"
        printf "WARNING: Make sure to back up your data.\n"
        printf "WARNING:\n"
        cl_prompt_yes_no "Are you sure" || exit 0
    fi
    log_begin_msg "${log_pre}Enabling ONIE install mode at next reboot"
    cl_reboot_cmd_set "install"
    log_end_msg
    if [ "$dry_run" != "y" ] ; then
        _log_msg "Reboot required to take effect.\n"
    fi
elif [ "$command" = "uninstall" ] ; then
    if [ "$force" != "y" ] ; then
        printf "WARNING:\n"
        printf "WARNING: ONIE uninstall mode requested.\n"
        printf "WARNING: This will wipe out all system data.\n"
        printf "WARNING: Make sure to back up your data.\n"
        printf "WARNING:\n"
        cl_prompt_yes_no "Are you sure" || exit 0
    fi
    log_begin_msg "${log_pre}Enabling ONIE uninstall mode at next reboot"
    cl_reboot_cmd_set "uninstall"
    log_end_msg
    if [ "$dry_run" != "y" ] ; then
        _log_msg "Reboot required to take effect.\n"
    fi
elif [ "$command" = "update" ] ; then
    if [ "$force" != "y" ] ; then
        printf "WARNING:\n"
        printf "WARNING: Update ONIE requested.\n"
        printf "WARNING:\n"
        cl_prompt_yes_no "Are you sure" || exit 0
    fi
    log_begin_msg "${log_pre}Enabling ONIE self-update mode at next reboot"
    cl_reboot_cmd_set "update"
    log_end_msg
    if [ "$dry_run" != "y" ] ; then
        _log_msg "Reboot required to take effect.\n"
    fi
elif [ "$command" = "cancel" ] ; then
    reboot_cmd=$(cl_reboot_cmd_get)
    if [ -n "$reboot_cmd" ] ; then
        log_begin_msg "Cancelling pending $reboot_cmd at next reboot"
        cl_reboot_cmd_set
        log_end_msg
    else
        log_failure_msg "No pending reboot operations."
        exit 1
    fi
elif [ "$command" = "display" ] ; then
    display_info
fi
