#!/usr/bin/env python

"""
Run the domainroot administration tool
"""

__copyright__ = """
Copyright 2013 Oracle.  All rights reserved.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2.0
as published by the Free Software Foundation.

This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License version 2.0 for more details.

A copy of the GNU General Public License version 2.0 is
available here:

    http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
"""

import sys
import os
import argparse
import logging as log

sys.path.insert(1, '${prefix}/lib/python2.7/site-packages')

try:
    from PyFedfs.run import EXIT_FAILURE
    from PyFedfs.domainroot.addremove import subcmd_add, subcmd_remove
    from PyFedfs.domainroot.clean import subcmd_clean
    from PyFedfs.domainroot.status import subcmd_status
except ImportError:
    print >> sys.stderr, \
        'Could not import a required Python module:', sys.exc_value
    sys.exit(1)



def main():
    """
    Domainroot helper main program

    Returns a shell exit status value
    """
    if os.getegid() != 0:
        print >> sys.stderr, 'You must be root to run fedfs-domainroot.'
        return EXIT_FAILURE

    parser = argparse.ArgumentParser(
        formatter_class=argparse.RawDescriptionHelpFormatter,
        description='Set up FedFS domain root infrastructure',
        epilog='''\
Copyright 2013 Oracle.  All rights reserved.

License GPLv2: <http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt>
This is free software.  You are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.''')
    parser.add_argument('--version',
                        help='Display the version of this command',
                        action='version',
                        version='fedfs-utils 0.10.5')
    parser.add_argument('--silent',
                        help='Display less output',
                        action='store_true')
    parser.add_argument('--statedir',
                        help='FedFS state dir (default /var/lib/fedfs)',
                        default='/var/lib/fedfs')
    subparsers = parser.add_subparsers(title='Sub-commands')

    add_parser = subparsers.add_parser('add',
                                       help='Add a new domain root directory')
    add_parser.add_argument('domainname',
                            help='Name of FedFS domain to add')
    add_parser.set_defaults(func=subcmd_add)

    remove_parser = subparsers.add_parser('remove',
                                          help='Remove a domain root '
                                               'directory')
    remove_parser.add_argument('--force',
                               help='Ignore errors', action='store_true')
    remove_parser.add_argument('domainname',
                               help='Name of FedFS domain to remove')
    remove_parser.set_defaults(func=subcmd_remove)

    status_parser = subparsers.add_parser('status',
                                          help='Display current FedFS domain '
                                               'root configuration')
    status_parser.set_defaults(func=subcmd_status)

    clean_parser = subparsers.add_parser('clean',
                                         help='Remove domain root '
                                              'infrastructure')
    clean_parser.add_argument('--force',
                              help='Ignore errors', action='store_true')
    clean_parser.set_defaults(func=subcmd_clean)

    args = parser.parse_args()

    if args.silent:
        log.basicConfig(format='%(levelname)s: %(message)s')
    else:
        log.basicConfig(format='%(levelname)s: %(message)s',
                        level=log.DEBUG)

    return args.func(args)


try:
    if __name__ == '__main__':
        sys.exit(main())
except (SystemExit, KeyboardInterrupt, RuntimeError):
    sys.exit(1)
