#!/usr/bin/env python3

import argparse
import a2conf
import logging
import sys

def_file = '/etc/apache2/apache2.conf'

parser = argparse.ArgumentParser(description='Apache config parser')
parser.add_argument(dest='file', nargs='?', default=def_file, metavar='PATH',
                    help='Config file(s) path (def: {}). Either filename or directory'.format(def_file))
parser.add_argument('--cmd', default=list(), nargs='*', help='show all these commands', type=str.lower)
parser.add_argument('--filter', nargs=2, metavar=('Command', 'Argument'),
                    help='Process only sections with this command/argument (command can be "ServerName,ServerList")',
                    type=str.lower)
parser.add_argument('--negative', default=False, action='store_true', help='Negative filtering')
parser.add_argument('--args', default=False, action='store_true', help='show only arguments')
parser.add_argument('--uargs', default=False, action='store_true', help='show only unique arguments')
parser.add_argument('--vhost', default=None, nargs='?', const='', help='VHOST format prefix')
parser.add_argument('-v', '--verbose', default=False, action='store_true', help='verbose')
parser.add_argument('--no-includes', default=True, dest='includes', action='store_false',
                    help='Disable processing Include* directives')


args = parser.parse_args()

if args.verbose:
    loglevel = logging.DEBUG
else:
    loglevel = logging.INFO

log = logging.getLogger('a2conf')
log.setLevel(loglevel)
logh = logging.StreamHandler(stream=sys.stderr)
logh.setFormatter(logging.Formatter('%(message)s', '%Y-%m-%d %H:%M:%S'))
log.addHandler(logh)

log.debug("DEBUG")

if not args.file:
    print("Need filename")
    exit()

# read file
root = a2conf.Node(name='#root', includes=args.includes)
parent = root

root.read_file(args.file)

arg_list = list()

for vhost in root.all_nodes():
    if vhost.section and vhost.section.lower() == 'virtualhost':

        # skip or process?
        if args.filter:
            process = False
            for check_node in vhost.get_nodes_cmd(args.filter[0].split(',')):
                check_args = check_node.args.split(' ')
                if args.filter[1].lower() in map(str.lower, check_args):
                    process = True

            if args.negative:
                process = not process

            if not process:
                log.debug("Skipping VHost: {}".format(vhost))
                continue
            else:
                log.debug("Process VHost: {}".format(vhost))

        ctx = dict()
        ctx['vhostargs'] = vhost.args

        if args.vhost is not None:
            # reset arglist if we use per-vhost output
            arg_list = list()

        # now process all statements inside vhost
        for cnode in vhost.children():
            # f
            if cnode.cmd:
                ctx[cnode.cmd.lower()] = cnode.args

                if args.cmd:
                    if cnode.cmd.lower() in args.cmd:
                        if args.args or args.uargs:
                            # process only args
                            arg_list.extend(filter(None, cnode.args.split(" ")))
                        else:
                            print(cnode.cmd, cnode.args)

        ctx['args'] = ' '.join(arg_list)
        ctx['uargs'] = ' '.join(set(arg_list))

        if args.vhost is not None:
            # per-vhost
            log.debug(ctx)
            print(args.vhost.format(**ctx))


if args.vhost is None:

    # per-file summary
    if args.args:
        print(' '.join(arg_list))

    if args.uargs:
        uargs = set(arg_list)
        print(' '.join(uargs))
