#!/usr/bin/env python
"""Blackhole.

Start a TCP server that will accept connections and then read and discard anything sent to it until it is stopped. Optionally can be run to echo any data received.

Usage:
  blackhole <host> <port> [--echo] [--debug] [--http]

Options:
  --echo        Send the data received back to client.
  --debug       Output any data received to STDOUT.
  --http       Respond with HTTP 200 OK. If echo is also on, contents will be included.
  -h --help     Show this screen.
  --version     Show version.
"""

from docopt import docopt
from tcp_blackhole import TcpBlackhole

# Parse args from the docblock
args = docopt(__doc__)

print("===== Blackhole =====")
print('| Host:', args['<host>'])
print('| Port:', args['<port>'])
print("| Echo:", args['--echo'])
print("| Debug:", args['--debug'])
print("| Respond with HTTP OK:", args['--http'])
print("=====================")

blackhole = TcpBlackhole(
        host=args['<host>'],
        port=args['<port>'],
        echoFlag=args['--echo'],
        debugFlag=args['--debug'],
        httpFlag=args['--http'])

try:
    blackhole.start()
except KeyboardInterrupt as e:
    print('\nKeyboard interrupt received. Shutting down. This might hang if there are'
          ' active connections. Another CTRL-C may be required.')
    exit(0)


