import re

def find(pac, user, helpers):
    sudo_get_output = helpers['sudo_get_output']
    get_domain = helpers['get_domain']
    get_usage = helpers['get_usage']
    get_pip_package_version = helpers['get_pip_package_version']
    get_js_package_version = helpers['get_js_package_version']

    result = []

    app = "Taiga Backend"
    filename = f"/home/pacs/{pac}/users/{user}/taiga-back/taiga/__init__.py"
    cmd = f'if [ -f {filename} ]; then cat {filename}; fi'
    lines = sudo_get_output(pac, user, cmd)
    if not lines:
        filename = f"/home/pacs/{pac}/users/{user}/taiga/taiga-back/taiga/__init__.py"
        cmd = f'if [ -f {filename} ]; then cat {filename}; fi'
        lines = sudo_get_output(pac, user, cmd)
    if lines:
        #print(filename)
        found = False
        version = None
        for line in lines:
            if '__version__' in line:
                found = True
                pattern = r'\'(.*)\''
                m = re.search(pattern, line)
                if m:
                    version = m.group(1)

        if found and version:
            (scheme, domain) = get_domain(pac, user)
            (ram, ssd, hdd) = get_usage(pac, user)
            result.append([app, version, pac, user, scheme, domain, ram, ssd, hdd])

    app = "Taiga Frontend"
    path = f"/home/pacs/{pac}/users/{user}/taiga-front-dist"
    cmd = f'if [ -d {path} ]; then cd {path} && git log --decorate=full | head -n 20; fi'
    lines = sudo_get_output(pac, user, cmd)
    if not lines:
        path = f"/home/pacs/{pac}/users/{user}/taiga/taiga-front-dist"
        cmd = f'if [ -d {path} ]; then cd {path} && git log --decorate=full | head -n 20; fi'
        lines = sudo_get_output(pac, user, cmd)
    if lines:
        found = False
        version = None
        for line in lines:
            if 'tag: refs/tags/' in line:
                found = True
                pattern = r'tag: refs\/tags\/(.*?), '
                m = re.search(pattern, line)
                if m:
                    version = m.group(1)
                    break

        if found and version:
            (scheme, domain) = get_domain(pac, user)
            (ram, ssd, hdd) = get_usage(pac, user)
            result.append([app, version, pac, user, scheme, domain, ram, ssd, hdd])


    return {'list': result}
