#!/usr/bin/env python

import argparse
import os
from namanager.main import Driver

parser = argparse.ArgumentParser()

parser.add_argument("--version",
                    action='store_true',
                    help="print the Namanager version number and exit.")

parser.add_argument("--init",
                    action='store_true',
                    help="Output a initial settings file.")
parser.add_argument("--init-path",
                    default=os.getcwd(),
                    metavar="PATH",
                    help="Store initial settings file to PATH.")
parser.add_argument("-s", "--settings",
                    default=os.path.join(os.getcwd(), 'settings.json'),
                    help="Default is ./settings.json. Import settings.json.")

parser.add_argument("--required",
                    action='store_true',
                    help="If namanager fails it will exit 1.")

parser.add_argument("--count",
                    action='store_true',
                    help="Output information of error count.")
parser.add_argument("--with-readable",
                    action='store_true',
                    help="Default. Generate as dict (python) format.")
parser.add_argument("--with-xml",
                    action='store_true',
                    help="Generate as xml format.")
parser.add_argument("--with-json",
                    action='store_true',
                    help="Generate as json format.")
parser.add_argument("--pretty-dump",
                    action='store_true',
                    help="Only works with --with-xml or --with-json options")


parser.add_argument("--rename",
                    action='store_true',
                    help="Automatically rename no matched paths.")
parser.add_argument("--rename-backup",
                    action='store_true',
                    help="""
                    Default. create backup file
                    which is used to revert the rename action.
                    """)
parser.add_argument("--rename-no-backup",
                    action='store_true',
                    help="""
                    Do not create backup file.
                    Warning: if you use mode,
                    you can NOT revert paths
                    which are renamed by --rename option.
                    """)
parser.add_argument("--rename-backup-path",
                    default=os.getcwd(),
                    help="Save backup file into specify directory/file.")
parser.add_argument("--rename-recover",
                    action='store_true',
                    help="Recover paths after any rename failed.")

parser.add_argument("--revert",
                    action='store_true',
                    help="Revert paths which are renamed by --rename option."
                    )
parser.add_argument("--revert-last",
                    action='store_true',
                    help="""
                    Revert paths by lastest backup file in CWD
                    (override --revert-file option)."
                    """
                    )
parser.add_argument("--revert-file",
                    default=None,
                    metavar="FILE",
                    help="""
                    Revert paths by giving file
                    (which is generated by --rename-backup option).
                    """)

args = parser.parse_args()

if __name__ == '__main__':
    kwargs = {
        'version': args.version,
        'init': args.init,
        'init_path': args.init_path,
        'settings': args.settings,
        'required': args.required,
        'count': args.count,
        'fmt': (
            'xml' if args.with_xml else
            'json' if args.with_json else
            'readable'
        ),
        'pretty_dump': args.pretty_dump,
        'rename': args.rename,
        'rename_backup': (
            False if args.rename_no_backup else True
        ),
        'rename_backup_path': args.rename_backup_path,
        'rename_recover': args.rename_recover,
        'revert': args.revert,
        'revert_last': args.revert_last,
        'revert_file': args.revert_file if args.revert_last is False else None,
    }

    driver = Driver()
    driver.entry(**kwargs)
    exit(driver.exit_code)
