#!/usr/bin/python3
# pylint: disable=missing-module-docstring,invalid-name
# -*- coding: utf-8 -*-
#
# Copyright (C) 2014 Novell, Inc.
#   This library is free software; you can redistribute it and/or modify
# it only under the terms of version 2.1 of the GNU Lesser General Public
# License as published by the Free Software Foundation.
#
#   This library 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 Lesser General Public License for more
# details.
#
#   You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
from __future__ import print_function

import sys
import textwrap
from optparse import OptionParser
from spacewalk.common.rhnConfig import CFG, initCFG
from spacewalk.susemanager import mgr_delete_patch


def main():
    initCFG("server.susemanager")

    description = """\
Remove patch and its clones from all the channels. All the packages
referenced by the patch are removed from their channels and from the
database; rpm files are removed from disk. If PATCH is a clone of another
patch the tool will propose the removal of the original patch and all its
clones.
"""

    parser = OptionParser(
        version="%prog 0.1",
        description=textwrap.dedent(description),
        usage="%prog [options] PATCH",
    )
    parser.add_option(
        "-d",
        "--debug",
        action="store",
        type="int",
        dest="debug",
        default=CFG.DEBUG,
        help="enable debugging",
    )

    (options, args) = parser.parse_args()

    if len(args) == 0:
        sys.stderr.write("You have to specify at least a patch\n")
        sys.exit(1)

    cleaner = mgr_delete_patch.Cleaner(debug=options.debug)

    for patch in args:
        cleaner.remove(patch)


if __name__ == "__main__":
    try:
        main()
    except IOError as e:
        # pylint: disable-next=consider-using-f-string
        print("ERROR: %s" % e)
