#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2019 Philippe Grégoire <pg@pgregoire.xyz>
#
# Permission to use, copy, modify, and/or distribute this software for
# any purpose with or without fee is hereby granted, provided that the
# above copyright notice and this permission notice appear in all
# copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
# WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
# AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
# DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
# PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
# TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE.
#

from __future__ import print_function

import getopt
import os
import sys

from exploit_patterns import create


def usage(file=sys.stdout):
    u = '{} [-h] [-l len] [-s set1,set2,set3]'.format(os.path.basename(sys.argv[0]))
    print('usage: {}'.format(u), file=file)


def main(args):
    try:
        opts, args = getopt.getopt(args, '-hl:s:', [
            'help', 'length=', 'sets='
        ])
    except getopt.GetoptError as e:
        print(e, file=sys.stderr)
        usage(sys.stderr)
        return 2


    o_length = -1
    o_sets   = None

    for k, v in opts:
        if k in ['-h', '--help']:
            usage()
            return 0
        if k in ['-l', '--length']:
            o_length = int(v)
        if k in ['-s', '--sets']:
            o_sets = v.split(',')


    if 0 != len(args):
        usage(sys.stderr)
        return 2

    print(create(o_length, o_sets))
    return 0


if '__main__' == __name__:
    sys.exit(main(sys.argv[1:]))
