#!/usr/bin/env python
# Copyright 2014-2019 CERN for the benefit of the ATLAS collaboration.
#
# 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
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Authors:
# - Cedric Serfon, <cedric.serfon@cern.ch>, 2019
# - Patrick Austin <patrick.austin@stfc.ac.uk>, 2020
#
# PY3K COMPATIBLE

'''
'''

import argparse
import signal

from rucio.daemons.reaper.reaper2 import run, stop


def get_parser():
    """
    Returns the argparse parser.
    """
    parser = argparse.ArgumentParser()
    parser.add_argument("--run-once", action="store_true", default=False,
                        help='Runs one loop iteration')
    parser.add_argument("--threads", action="store", default=1, type=int,
                        help='Concurrency control: number of threads')
    parser.add_argument("--chunk_size", action="store", default=100, type=int,
                        help='The size used for a bulk deletion on on RSE')
    parser.add_argument('--sleep-time', action="store", default=60, type=int,
                        help='Minimum time between 2 consecutive cycles')
    parser.add_argument('--greedy', action="store_true", default=False,
                        help='To enable greedy mode. Deprecated, kept for compatibility reasons. Recommended way is to use RSE attribute greedyDeletion=True')
    parser.add_argument('--exclude-rses', action="store", default=None, type=str,
                        help='RSE expression to exclude')
    parser.add_argument('--include-rses', action="store", default=None, type=str,
                        help='RSE expression to include')
    parser.add_argument('--rses', nargs='+', type=str,
                        help='Explicit list of RSEs to include. If empty, it considers all RSEs')
    parser.add_argument('--vos', nargs='+', type=str,
                        help='Optional list of VOs to consider. Only used in multi-VO mode.')
    parser.add_argument("--delay-seconds", action="store", default=600, type=int,
                        help='The delay (seconds) to query replicas in BEING_DELETED state.')
    parser.add_argument('--scheme', action="store", default=None, type=str,
                        help='Force the reaper to use a particular protocol/scheme, e.g., mock')

    return parser


if __name__ == "__main__":

    # Bind our callback to the SIGTERM signal and run the daemon:
    signal.signal(signal.SIGTERM, stop)

    parser = get_parser()
    args = parser.parse_args()
    try:
        run(threads=args.threads,
            chunk_size=args.chunk_size,
            include_rses=args.include_rses,
            exclude_rses=args.exclude_rses,
            rses=args.rses,
            vos=args.vos,
            once=args.run_once,
            greedy=args.greedy,
            scheme=args.scheme,
            delay_seconds=args.delay_seconds,
            sleep_time=args.sleep_time)
    except KeyboardInterrupt:
        stop()
