#!/usr/bin/env python
#
# Tool to backup RSBAC attributes.
# 
# Usage: rsbac-backup --help
#
# Copyright (C) 2014 Jens Kasten <jens@kasten-edv.de>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# 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 for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>
#

import sys
import argparse
import time
import logging

try:
    from rsbactools import backup
except ImportError as error:
    print(error)
    sys.exit(-1)


def main():
    time_start = time.time()
    b = backup.Backup()

    parser = argparse.ArgumentParser(
        description="RSBAC attributes backup.")

    parser.add_argument("-v", "--verbose", default=False, action="store_true",
        help="Make the script verbose.")
    parser.add_argument("-t", "--time", default=False, action="store_true",
        help="Print time to execute.")
   
    group_backup = parser.add_argument_group("Options for backup")
    group_backup.add_argument("-d", "--directory", type=str,
        metavar="backup directory",
        help="Set backup directory for storing backup. "
            "Default is CurrentWorkDirectory/backup/")

    group_restore = parser.add_argument_group("Options for restore")
    group_restore.add_argument("-r", "--restore", default=False, 
        action="store_true",
        help="Set this option then you can choose from which date.")

    group_module = parser.add_argument_group("module list")
    group_module.add_argument("--full", default=False, action="store_true",
        help="Set full backup.")

    # create console switch dynamic
    counter = 2 
    for key in sorted(b.get_cmd_modules()):
        help_group = "Set %s backup" % key.lower()
        group_module.add_argument("--%s"  % key.lower(), default=False,
            action="store_true", help=help_group)

    args = parser.parse_args()
    b.set_args(vars(args))

    status = False
    for i in b.args.values():
        if i:
            status = True
    if not status:
        parser.print_usage()
        return 

    if args.verbose:
        b.set_log_level(logging.DEBUG)

    if args.directory:
        if not os.path.isdir(args.directory):
            parser.error("Backup directory does not exist: %s" % args.directory)
    if b.run() is False:
        parser.print_usage()
        return 
    else:   
        if args.time:
            time_end = time.time()
            time_all = time_end - time_start
            if time_all > 61:
                seconds = time_all % 60
                minutes = int(time_all / 60)
                print("Time to execute: %0.f min  %d sec" % (minutes, seconds))
            else:
                print("Time to execute: %3.2f seconds" % time_all)


if __name__ == "__main__":
    main()
