#!/usr/bin/env python
from __future__ import print_function
import argparse

try:
    from urllib.parse import urlencode
except ImportError:
    #fallback python 2.7
    from urllib import urlencode

try:
    import clipboard
except ImportError:
    import sys

    clipboard = None
    print("WARN: no clipboard support", file=sys.stderr)


SEARCH_ENGINES = dict(
    web='w', images='i', videos='v', maps='m', news='n', shopping='s',
    books='b', finance='f', schollar='sc'
)

## http://lmgtfy.com/?t=sc&q=a

def valid_engine(e):
    if e not in SEARCH_ENGINES:
        raise argparse.ArgumentTypeError("{} is not a valid search engine".format(e))
    else:
        return e

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description="")
    parser.add_argument(
        '-e','--engine',
        help="Search engine. valids are {}. Default:web".format(
            ",".join(SEARCH_ENGINES.keys())
        ),
        type=valid_engine,
        default='web'
    )
    parser.add_argument(
        '--no-clip',
        help="Don't copy url to the clipboard",
        action = 'store_true'
    )
    parser.add_argument('query', nargs="+")
    ops = parser.parse_args()

    query = urlencode({
        't':SEARCH_ENGINES[ops.engine],
        'q':" ".join(ops.query)
    })

    url = "http://lmgtfy.com/?{}".format(query)

    if clipboard and not ops.no_clip:
        clipboard.copy(url)

    print(url)