#! /usr/bin/env python
from __future__ import print_function
import argparse
import requests
import requests_cache
import tempfile
import os
import sys
import hashlib
import requests.packages.urllib3
requests.packages.urllib3.disable_warnings()

tmpdir = tempfile.gettempdir()
m = hashlib.md5()
k = 'gitignorepy%d%d' % (sys.version_info.major, sys.version_info.minor)
m.update(k.encode('utf8'))
cache_file = os.path.join(tmpdir, m.hexdigest())
requests_cache.install_cache(cache_file, expire_after=60*60)


def fetch(keyword):
    lst = get_list(keyword)
    exact_match = [x for x in lst if x['name'].lower() == keyword.lower()]
    if len(exact_match) > 0:
        print(requests.get(exact_match[0]['download_url'], verify=False).text, end='')
    elif len(lst) == 0:
        print('Template not found:', keyword)
    elif len(lst) == 1:
        print(requests.get(lst[0]['download_url'], verify=False).text, end='')
    else:
        print('Ambiguous keyword:', keyword)
        _print_list(lst)


def get_list(keyword):
    l1 = _fetch_list('https://api.github.com/repos/github/gitignore/contents/')
    l2 = _fetch_list('https://api.github.com/repos/github/gitignore/contents/Global')
    return [x for x in sorted(l1 + l2, key=lambda x: x['name']) if keyword.lower() in x['name'].lower()]


def _fetch_list(url):
    lst = requests.get(url, verify=False).json()
    return [x for x in lst if x['type'] == 'file' and not x['name'].startswith('.')]


def _print_list(lst):
    for x in lst:
        print('-', x['name'].replace('.gitignore', ''))
    print('Found %d template' % len(lst) + ('s.' if len(lst) > 1 else '.'))

def main():
    parser = argparse.ArgumentParser(description='fetch gitignore from Github.com')
    subparsers = parser.add_subparsers(dest='command')
    # fetch
    parser_fetch = subparsers.add_parser('fetch', help='fetch a gitignore file')
    parser_fetch.add_argument('keyword')
    # list
    parser_list = subparsers.add_parser('list', help='list available gitignore files')
    parser_list.add_argument('keyword', default='', nargs='?')
    # clear_cache
    parser_cc = subparsers.add_parser('clear_cache', help='clear cache')

    args = parser.parse_args()
    if args.command:
        if args.command == 'fetch':
            fetch(args.keyword)
        elif args.command == 'list':
            _print_list(get_list(args.keyword))
        elif args.command == 'clear_cache':
            os.remove(cache_file + '.sqlite')
    else:
        parser.print_help()


if __name__ == '__main__':
    main()
