#!/bin/sh
set -e

uninstall_x86_64()
{
    if [ "$1" = "manual" ]; then
        # Clear any ONIE boot selection settings and default to Install
        mkdir -p /mnt/onie-boot
        mount `blkid -L ONIE-BOOT` /mnt/onie-boot
        rm -rf /mnt/onie-boot/grub/grubenv
        umount /mnt/onie-boot

        # Force ONIE boot selection
        onlfs mount boot --rw
        echo "default=1" >> /mnt/onl/boot/grub/grub.cfg

    else
        if [ "$1" = "factory" ]; then
            mode=uninstall
        else
            mode=install
        fi
        onl-onie-boot-mode $mode
    fi
}

uninstall_uboot()
{
    if [ "$1" = "factory" ]; then
        fw_setenv onie_boot_reason uninstall
    else
        fw_setenv nos_bootcmd echo
    fi
}

uninstall()
{
    case `uname -m` in
        x86_64)
            uninstall_x86_64 $1
            ;;
        ppc|armv7l)
            uninstall_uboot $1
            ;;
        *)
            echo "Uninstall for the current architecture is not implemented. This is a bug."
            exit 1
            ;;
    esac
}

############################################################

uninstall $1
echo "The NOS will be removed at the next reboot."
exit 0
