#!/usr/bin/env python
# Copyright European Organization for Nuclear Research (CERN)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# You may not use this file except in compliance with the License.
# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
#
# Authors:
# - Martin Barisits, <martin.barisits@cern.ch>, 2016

"""
BB8 is a daemon to rebalance data.
"""

import argparse

# from rucio.daemons.bb8.bb8 import run, stop
from rucio.daemons.bb8.common import rebalance_rse

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    # parser.add_argument("--run-once", action="store_true", default=False, help='One iteration only')
    # parser.add_argument("--threads", action="store", default=1, type=int, help='Concurrency control: total number of threads on this process')
    parser.add_argument('rse', action='store', help='RSE to rebalance')
    parser.add_argument('bytes', action='store', type=int, help='Number of bytes to rebalance')
    parser.add_argument('--dry-run', action='store_true', default=False, help='Only run in dry-run mode')
    parser.add_argument('--exclude-expression', action='store', help='Exclude these rse_expression from being destinations')
    parser.add_argument('--comment', action='store', help='Add a comment to the new rules')
    parser.add_argument('--force-expression', action='store', help='For this rse_expression for rebalanced rules instead of letting BB8 decide')
    parser.add_argument('--decomission', action='store_true', help='Run BB8 in decomission mode')
    parser.add_argument('--priority', action='store', help='Priority for the newly created rules', type=int, default=3)
    parser.add_argument('--source-replica-expression', action='store', help='Source replica expression for the newly created rules')
    args = parser.parse_args()

    if args.decomission:
        rebalance_rse(rse=args.rse, max_bytes=args.bytes, dry_run=args.dry_run, comment=args.comment, force_expression=args.force_expression, priority=args.priority, source_replica_expression=args.source_replica_expression, mode='decomission')
    else:
        rebalance_rse(rse=args.rse, max_bytes=args.bytes, dry_run=args.dry_run, comment=args.comment, force_expression=args.force_expression, priority=args.priority, source_replica_expression=args.source_replica_expression)
