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

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

    jsLines = []
    for line in content.splitlines()[1:]:
        if (line and (not line.startswith('!'))):
            use_proxy = True
            if line.startswith('@@'):
                line = line[2:]
                use_proxy = False
            return_proxy = (('PROXY    %s' % proxy) if use_proxy else default)
            if (line.startswith('/') and line.endswith('/')):
                jsLine = ('if    (/%s/i.test(url))    return    "%s";' % (line[1:(-1)], return_proxy))
            elif line.startswith('||'):
                domain = line[2:].lstrip('.')
                if ((len(jsLines) > 0) and ((('host.indexOf(".%s")    >=    0' % domain) in jsLines[(-1)]) or (('host.indexOf("%s")    >=    0' % domain) in jsLines[(-1)]))):
                    jsLines.pop()
                jsLine = ('if    (dnsDomainIs(host,    ".%s")    ||    host    ==    "%s")    return    "%s";' % (domain, domain, return_proxy))
            elif line.startswith('|'):
                jsLine = ('if    (url.indexOf("%s")    ==    0)    return    "%s";' % (line[1:], return_proxy))
            elif ('*' in line):
                jsLine = ('if    (shExpMatch(url,    "*%s*"))    return    "%s";' % (line.strip('*'), return_proxy))
            elif ('/' not in line):
                jsLine = ('if    (host.indexOf("%s")    >=    0)    return    "%s";' % (line, return_proxy))
            else:
                jsLine = ('if    (url.indexOf("%s")    >=    0)    return    "%s";' % (line, return_proxy))
            jsLine = (('    ' * indent) + jsLine)
            if use_proxy:
                jsLines.append(jsLine)
            else:
                jsLines.insert(0, jsLine)
    function = ('function    %s(url,    host)    {\r\n%s\r\n%sreturn    "%s";\r\n}' % (func_name, '\n'.join(jsLines), ('    ' * indent), default))
    return function
