version = node.metadata.get('paperless/version')
workers = ('consumer', 'scheduler', 'taskqueue', 'webserver')

users['paperless'] = {
    'home': '/opt/paperless',
}

directories['/opt/paperless'] = {}

files['/opt/paperless/paperless.conf'] = {
    'content_type': 'mako',
    'triggers': {
        f'svc_systemd:paperless-{worker}:restart'
        for worker in workers
    },
}

files['/opt/paperless/pre-consume.sh'] = {
    'mode': '0755',
}

actions['paperless_create_virtualenv'] = {
    'command': '/usr/bin/python3 -m virtualenv -p python3 /opt/paperless/venv/',
    'unless': 'test -d /opt/paperless/venv/',
    'needs': {
        'directory:/opt/paperless',
        'pkg_apt:python3',
        'pkg_apt:python3-pip',
        'pkg_apt:python3-virtualenv',
    },
}

actions['paperless_install'] = {
    'command': ' && '.join([
        f'wget -qO /opt/paperless/{version}.tar.xz https://github.com/paperless-ngx/paperless-ngx/releases/download/{version}/paperless-ngx-{version}.tar.xz',
        'rm -rf /opt/paperless/src/',
        'mkdir -p /opt/paperless/src/',
        f'tar -C /opt/paperless/src -xf /opt/paperless/{version}.tar.xz',
        f'rm /opt/paperless/{version}.tar.xz',
        'cd /opt/paperless/src/paperless-ngx',
        '/opt/paperless/venv/bin/pip install --upgrade pip',
        '/opt/paperless/venv/bin/pip install --upgrade -r requirements.txt',
        f'echo "{version}" > /opt/paperless/version',
    ]),
    'unless': f'''bash -c '[[ "$(cat /opt/paperless/version)" == "{version}" ]]' ''',
    'after': {
        'pkg_apt:',
    },
    'needs': {
        'action:paperless_create_virtualenv',
    },
    'triggers': {
        'action:paperless_migrate_database',
        *{
            f'svc_systemd:paperless-{worker}:restart' for worker in workers
        }
    },
}

actions['paperless_migrate_database'] = {
    'command': ' && '.join([
        'cd /opt/paperless/src/paperless-ngx/src',
        'sudo -Hu paperless PAPERLESS_CONFIGURATION_PATH=/opt/paperless/paperless.conf /opt/paperless/venv/bin/python manage.py migrate',
    ]),
    'triggered': True,
    'needs': {
        # /mnt/paperless is NOT created by this bundle.
        'action:paperless_install',
        'directory:/mnt/paperless',
        'file:/opt/paperless/paperless.conf',
        'user:paperless',
        'postgres_db:paperless',
    },
}

for worker in workers:
    files[f'/etc/systemd/system/paperless-{worker}.service'] = {
        'delete': True,
        'triggers': {
            'action:systemd-reload',
        },
    }

    files[f'/usr/local/lib/systemd/system/paperless-{worker}.service'] = {
        'triggers': {
            'action:systemd-reload',
            f'svc_systemd:paperless-{worker}:restart',
        },
    }

    svc_systemd[f'paperless-{worker}'] = {
        'needs': {
            'action:paperless_install',
            'action:paperless_migrate_database',
            f'file:/usr/local/lib/systemd/system/paperless-{worker}.service',
        },
    }
