#!/usr/bin/env python
# SPDX-License-Identifier: GPL-2.0

import argparse

import os
os.sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import sys

import _damo_python2_support

import damo_adjust
import damo_features
import damo_fmt_json
import damo_lru_sort
import damo_monitor
import damo_reclaim
import damo_record
import damo_report
import damo_schemes
import damo_start
import damo_stat
import damo_stop
import damo_tune
import damo_validate

import _damo_subcmds

def pr_damo_version(args_not_use):
    bindir = os.path.dirname(os.path.abspath(__file__))
    with open(os.path.join(bindir, 'damo_version.py'), 'r') as f:
        print(f.read().strip())

subcmds = [
        _damo_subcmds.DamoSubCmd(name='record', module=damo_record,
            msg='record data accesses'),
        _damo_subcmds.DamoSubCmd(name='schemes', module=damo_schemes,
            msg='apply operation schemes'),
        _damo_subcmds.DamoSubCmd(name='report', module=damo_report,
            msg='report the recorded data accesses in the specified form'),
        _damo_subcmds.DamoSubCmd(name='monitor', module=damo_monitor,
            msg='repeat the recording and the reporting of data accesses'),
        _damo_subcmds.DamoSubCmd(name='adjust', module=damo_adjust,
            msg='adjust the record results with different monitoring attributes'),
        _damo_subcmds.DamoSubCmd(name='reclaim', module=damo_reclaim,
            msg='control DAMON_RECLAIM'),
        _damo_subcmds.DamoSubCmd(name='lru_sort', module=damo_lru_sort,
            msg='control DAMON_LRU_SORT'),
        _damo_subcmds.DamoSubCmd(name='features', module=damo_features,
            msg='list supported DAMON features in the kernel'),
        _damo_subcmds.DamoSubCmd(name='validate', module=damo_validate,
            msg='validate a given record result file'),
        _damo_subcmds.DamoSubCmd(name='stat', module=damo_stat,
            msg='print status of DAMON and the system'),
        _damo_subcmds.DamoSubCmd(name='start', module=damo_start,
            msg='start DAMON with given parameters'),
        _damo_subcmds.DamoSubCmd(name='stop', module=damo_stop,
            msg='stop running DAMON'),
        _damo_subcmds.DamoSubCmd(name='tune', module=damo_tune,
            msg='update input parameters of ongoing DAMON'),
        _damo_subcmds.DamoSubCmd(name='fmt_json', module=damo_fmt_json,
            msg='convert damo-start cmdline option to DAMON json input'),
        _damo_subcmds.DamoSubCmd(name='version',
            module=_damo_subcmds.DamoSubCmdModule(None, pr_damo_version),
            msg='print the version number'),
        ]

class SubCmdHelpFormatter(argparse.RawDescriptionHelpFormatter):
    def _format_action(self, action):
        parts = super(argparse.RawDescriptionHelpFormatter,
                self)._format_action(action)
        # skip sub parsers help
        if action.nargs == argparse.PARSER:
            parts = '\n'.join(parts.split('\n')[1:])
        return parts

def main():
    parser = argparse.ArgumentParser(formatter_class=SubCmdHelpFormatter)

    subparser = parser.add_subparsers(title='command', dest='command',
            metavar='<command>')
    subparser.required = True

    for subcmd in subcmds:
        subcmd.add_parser(subparser)

    subparser.add_parser('version', help='print the version number')

    args = parser.parse_args()

    for subcmd in subcmds:
        if subcmd.name == args.command:
            subcmd.execute(args)

if __name__ == '__main__':
    main()
