#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# backuppurge: Selectively purge daily full backups
#
# Copyright (c) 2013, 2015, 2022 Thomas Perl <m@thp.io>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

import logging
import argparse

import sys
import os.path

# Insert "lib" to path if we are running from a source checkout
prefix, bindir = os.path.split(os.path.dirname(os.path.abspath(sys.argv[0])))
if bindir != 'bin':
    sys.path.insert(0, os.path.join(prefix, bindir, 'lib'))

import backuppurge


if __name__ == '__main__':
    parser = argparse.ArgumentParser()

    parser.add_argument('DIRECTORY', type=str,
            help='Directory to look for backup files')
    parser.add_argument('-d', '--days', default=30, type=int,
            help='Number of days to keep')
    parser.add_argument('-m', '--months', default=6, type=int,
            help='Number of months to keep (0 to disable)')
    parser.add_argument('-y', '--years', default=5, type=int,
            help='Number of years to keep (0 to disable)')
    parser.add_argument('-0', '--print0', action='store_true', default=False,
            help='Output filenames separated by NUL (for use with xargs)')
    parser.add_argument('-D', '--include-directories', action='store_true', default=False,
            help='Include directories when searching for backups')
    parser.add_argument('-p', '--prefix', default=None, type=str,
            help='Specify prefix to use if multiple prefixes are found')
    parser.add_argument('-V', '--verbose', action='store_true', default=False,
            help='Verbose output of decisions to stderr')
    parser.add_argument('-v', '--version', action='version',
            version=backuppurge.__version__,
            help='show version number and exit')

    args = parser.parse_args()

    logging_level = (logging.DEBUG if args.verbose else logging.WARNING)
    sep = ('\0' if args.print0 else '\n')

    logging.basicConfig(level=logging_level)
    logger = logging.getLogger(__name__)

    logger.debug('Configuration: %r', args)
    backuppurge.main(args.DIRECTORY, args.days, args.months, args.years, sep,
                     args.include_directories, args.prefix)

