#coding:utf-8

import os
import sys
import sunhpc
class command(sunhpc.commands.HostArgumentProcessor, 
        sunhpc.commands.set.command):
    pass

class Command(command):
    """
    Grub2 boot configure
    <param type='strings' name='default'>
    Set the default boot options
    </param>

    <example cmd='grub default=1'>
    Set the default boot options index 1
    </example>
    """

    def run(self, params, args):
        (default, ) = self.fillParams([
            ('default', None),
            ])

        if default:
            try:
                default_number = int(default)
                #self.msg('Please modify it manually by using the command: grubby --set-defautl-index=%s' % default)
                info = self.message('[*]', 'Please modify it manually by using the command:', 'grubby --set-default-index=%s' % default)
                print (info)
                sys.exit(0)
            except ValueError:
                self.msg('The "default" parameters must be the numbers.', 'a')


        current_kernel = os.popen('uname -r').read().strip()
        self.msg('--> Current kernel: %s' % current_kernel)

        default_kernel = os.popen('grubby --default-kernel').read().strip()
        self.msg('--> Default kernel: %s' % default_kernel)

        kernel_index = os.popen('grubby --default-index').read().strip()
        self.msg('--> Default kernel index: %s' % kernel_index)

        kernel_list = os.popen('grubby --info=ALL').readlines()
        self.msg('')
        self.msg('-------- All Kernel List begin----------')
        for i in kernel_list:
            self.msg(i.strip())
        self.msg('-------- All Kernel List ender----------')
        self.msg('')

        self.msg('Using index number, you can change the default boot kernel.e,g.. sunhpc set grub default=1', 'i')

RollName = "base"
