def remove_prefix(arg, opts, shell_opts):
    """ Remove prefix
    """

    # set up some basic variables
    remove_confirmed = shell_opts.force
    auth_src = set()
    recursive = False

    if opts.get('recursive') is True:
        recursive = True

    spec = { 'prefix': arg }
    v = get_vrf(opts.get('vrf_rt'), abort=True)
    if v.rt != 'all':
        spec['vrf_rt'] = v.rt

    res = Prefix.list(spec)

    if len(res) < 1:
        vrf_text = 'any VRF'
        if v.rt != 'all':
            vrf_text = vrf_format(v)
        print("Prefix %s not found in %s." % (arg, vrf_text), file=sys.stderr)
        sys.exit(1)

    p = res[0]

    if p.authoritative_source != 'nipap':
        auth_src.add(p.authoritative_source)

    if recursive is True or p.type == 'assignment':
        # recursive delete

        # get affected prefixes
        query = {
                'val1': 'prefix',
                'operator': 'contained_within_equals',
                'val2': p.prefix
        }

        # add VRF to query if we have one
        if 'vrf_rt' in spec:
            vrf_q = {
                'val1': 'vrf_rt',
                'operator': 'equals',
                'val2': spec['vrf_rt']
            }
            query = {
                'val1': query,
                'operator': 'and',
                'val2': vrf_q
            }
        pres = Prefix.search(query, { 'parents_depth': 0, 'max_result': 1200 })

    if not remove_confirmed:

        # if recursive is False, this delete will fail, ask user to do recursive
        # delete instead
        if p.type == 'assignment':
            if len(pres['result']) > 1:
                print("WARNING: %s in %s contains %s hosts." % (p.prefix, vrf_format(p.vrf), len(pres['result'])))
                res = input("Would you like to recursively delete %s and all hosts? [y/N]: " % (p.prefix))
                if res.lower() in [ 'y', 'yes' ]:
                    recursive = True
                else:
                    print("ERROR: Removal of assignment containing hosts is prohibited. Aborting removal of %s in %s." % (p.prefix, vrf_format(p.vrf)), file=sys.stderr)
                    sys.exit(1)

        if recursive is True:
            if len(pres['result']) <= 1:
                res = input("Do you really want to remove the prefix %s in %s? [y/N]: " % (p.prefix, vrf_format(p.vrf)))

                if res.lower() in [ 'y', 'yes' ]:
                    remove_confirmed = True

            else:
                print("Recursively deleting %s in %s will delete the following prefixes:" % (p.prefix, vrf_format(p.vrf)))

                # Iterate prefixes to print a few of them and check the prefixes'
                # authoritative source
                i = 0
                for rp in pres['result']:
                    if i <= 10:
                        print("%-29s %-2s %-19s %-14s %-14s %-40s" % ("".join("  " for i in
                            range(rp.indent)) + rp.display_prefix,
                            rp.type[0].upper(), rp.node, rp.order_id,
                            rp.customer_id, rp.description))

                    if i == 10:
                        print(".. and %s other prefixes" % (len(pres['result']) - 10))

                    if rp.authoritative_source != 'nipap':
                        auth_src.add(rp.authoritative_source)

                    i += 1

                if len(auth_src) == 0:
                    # Simple case; all prefixes were added from NIPAP
                    res = input("Do you really want to recursively remove %s prefixes in %s? [y/N]: " % (len(pres['result']),
                                vrf_format(vrf)))

                    if res.lower() in [ 'y', 'yes' ]:
                        remove_confirmed = True

                else:
                    # we have prefixes with authoritative source != nipap
                    auth_src = list(auth_src)
                    plural = ""

                    # format prompt depending on how many different sources we have
                    if len(auth_src) == 1:
                        systems = "'%s'" % auth_src[0]
                        prompt = "Enter the name of the managing system to continue or anything else to abort: "

                    else:
                        systems = ", ".join("'%s'" % x for x in auth_src[1:]) + " and '%s'" % auth_src[0]
                        plural = "s"
                        prompt = "Enter the name of the last managing system to continue or anything else to abort: "

                    print(("Prefix %s in %s contains prefixes managed by the system%s %s. " +
                        "Are you sure you want to remove them? ") % (p.prefix,
                                vrf_format(p.vrf), plural, systems))
                    res = input(prompt)

                    # Did the user provide the correct answer?
                    if res.lower() == auth_src[0].lower():
                        remove_confirmed = True
                    else:
                        print("System names did not match.", file=sys.stderr)
                        sys.exit(1)

        else:
            # non recursive delete
            if len(auth_src) > 0:
                auth_src = list(auth_src)
                print(("Prefix %s in %s is managed by the system '%s'. " +
                    "Are you sure you want to remove it? ") % (p.prefix,
                            vrf_format(p.vrf), auth_src[0]))
                res = input("Enter the name of the managing system to continue or anything else to abort: ")

                if res.lower() == auth_src[0].lower():
                    remove_confirmed = True

                else:
                    print("System names did not match.", file=sys.stderr)
                    sys.exit(1)

            else:
                res = input("Do you really want to remove the prefix %s in %s? [y/N]: " % (p.prefix, vrf_format(p.vrf)))
                if res.lower() in [ 'y', 'yes' ]:
                    remove_confirmed = True

    if remove_confirmed is True:
        p.remove(recursive = recursive)
        if recursive is True:
            print("Prefix %s and %s other prefixes in %s removed." % (p.prefix,
                    (len(pres['result']) - 1), vrf_format(p.vrf)))
        else:
            print("Prefix %s in %s removed." % (p.prefix, vrf_format(p.vrf)))

    else:
        print("Operation canceled.")
Remove prefix