'Autoproxy to Pac, based on https://github.com/iamamac/autoproxy2pac'

@staticmethod
def autoproxy2pac_lite(content, func_name='FindProxyForURLByAutoProxy', proxy='127.0.0.1:8087', default='DIRECT', indent=4):

    direct_domain_set = set([])
    proxy_domain_set = set([])
    for line in content.splitlines()[1:]:
        if (line and (not line.startswith(('!', '|!', '||!')))):
            use_proxy = True
            if line.startswith('@@'):
                line = line[2:]
                use_proxy = False
            domain = ''
            if (line.startswith('/') and line.endswith('/')):
                line = line[1:(-1)]
                if (line.startswith('^https?:\\/\\/[^\\/]+') and re.match('^(\\w|\\\\\\-|\\\\\\.)+$', line[18:])):
                    domain = line[18:].replace('\\.', '.')
                else:
                    logging.warning('unsupport    gfwlist    regex:    %r', line)
            elif line.startswith('||'):
                domain = line[2:].lstrip('*').rstrip('/')
            elif line.startswith('|'):
                domain = urlparse.urlsplit(line[1:]).hostname.lstrip('*')
            elif line.startswith(('http://', 'https://')):
                domain = urlparse.urlsplit(line).hostname.lstrip('*')
            elif re.search('^([\\w\\-\\_\\.]+)([\\*\\/]|$)', line):
                domain = re.split('[\\*\\/]', line)[0]
            else:
                pass
            if ('*' in domain):
                domain = domain.split('*')[(-1)]
            if ((not domain) or re.match('^\\w+$', domain)):
                logging.debug('unsupport    gfwlist    rule:    %r', line)
                continue
            if use_proxy:
                proxy_domain_set.add(domain)
            else:
                direct_domain_set.add(domain)
    proxy_domain_list = sorted(set((x.lstrip('.') for x in proxy_domain_set)))
    autoproxy_host = ',\r\n'.join((('%s"%s":    1' % (('    ' * indent), x)) for x in proxy_domain_list))
    template = "                                                                                var    autoproxy_host    =    {\n                                                                                %(autoproxy_host)s\n                                                                                };\n                                                                                function    %(func_name)s(url,    host)    {\n                                                                                                var    lastPos;\n                                                                                                do    {\n                                                                                                                if    (autoproxy_host.hasOwnProperty(host))    {\n                                                                                                                                return    'PROXY    %(proxy)s';\n                                                                                                                }\n                                                                                                                lastPos    =    host.indexOf('.')    +    1;\n                                                                                                                host    =    host.slice(lastPos);\n                                                                                                }    while    (lastPos    >=    1);\n                                                                                                return    '%(default)s';\n                                                                                }"
    template = re.sub(('(?m)^\\s{%d}' % min((len(re.search('    +', x).group()) for x in template.splitlines()))), '', template)
    template_args = {'autoproxy_host': autoproxy_host, 'func_name': func_name, 'proxy': proxy, 'default': default}
    return (template % template_args)
