#coding:utf-8

import os
import sys
import json
import sunhpc
class Command(sunhpc.commands.report.command):
    """
    Output the path prefix for the location of the Rocks distribution.

    <example cmd='report distro'>
    Output the current path prefix to the distribution.
    </example>
    """
    def run(self, params, args):

        basepath = '/opt/sunhpc/lib'

        cmd     = args
        dotscmd = ''
        listcmd = []
        module  = None
        if len(cmd):
            s = 'sunhpc.commands.%s' % '.'.join(cmd)
            try:
                __import__(s)
                module, listcmd, dotscmd = eval(s), s.split('.'), '.'.join(cmd)
                i = 1
            except:
                module = None
        else:
            listcmd = ['sunhpc', 'commands']

        if not module:
            for i in range(len(args), 0, -1):
                s = 'sunhpc.commands.%s' % '.'.join(args[:i])
                try:
                    __import__(s)
                    module, listcmd, dotscmd = eval(s), s.split('.'), '.'.join(args[:i])
                    if module:
                        break
                except ImportError:
                    listcmd = s.split('.')[:-1]
                    continue

        cmdpath = os.path.join(basepath, '/'.join(listcmd))

        cmddirs = []
        for d in os.listdir(cmdpath):
            tmpdirs = os.path.join(cmdpath, d)

            if d.startswith('__'):
                continue

            if not os.path.isdir(tmpdirs):
                continue

            cmddirs.append(d)

        print (' '.join(cmddirs))

        try:
            o = getattr(module, 'Command')(None)
        except AttributeError:
            sys.exit(0)

        if o.MustBeRoot and not self.isRootUser():
            sys.exit(0)

        results = []
        for arg in o.usage().split():
            tmp = arg.split('=', 1)
            if len(tmp) != 2:
                continue

            tmpstr = arg.replace('[', '')
            tmpstr = tmpstr.replace(']', '')
            tmpstr = tmpstr.split('=')[0] + '='
            results.append(tmpstr)

        print (' '.join(results))

RollName = "base"



























