repo.libs.tools.require_bundle(node, 'nodejs')

directories = {
    '/opt/mx-puppet-discord': {},
    '/opt/mx-puppet-discord/.npm': {
        'owner': 'mx-puppet-discord',
        'group': 'mx-puppet-discord',
    },
    '/opt/mx-puppet-discord/.yarn': {
        'owner': 'mx-puppet-discord',
        'group': 'mx-puppet-discord',
    },
    '/opt/mx-puppet-discord/src': {
        'owner': 'mx-puppet-discord',
        'group': 'mx-puppet-discord',
    },
    '/var/log/mx-puppet-discord': {
        'owner': 'mx-puppet-discord',
    },
}

users = {
    'mx-puppet-discord': {
        'home': '/opt/mx-puppet-discord',
    },
}

files = {
    '/opt/mx-puppet-discord/config.yaml': {
        'owner': 'mx-puppet-discord',
        'content_type': 'mako',
        'triggers': {
            'svc_systemd:mx-puppet-discord:restart',
        },
    },
    '/opt/mx-puppet-discord/registration.yaml': {
        'owner': 'mx-puppet-discord',
        'content_type': 'mako',
        'triggers': {
            'svc_systemd:mx-puppet-discord:restart',
        },
    },
    '/etc/systemd/system/mx-puppet-discord.service': {
        'source': 'service',
        'triggers': {
            'action:systemd-reload',
            'svc_systemd:mx-puppet-discord:restart',
        },
    }
}

git_deploy = {
    '/opt/mx-puppet-discord/src': {
        'repo': 'https://gitlab.com/mx-puppet/discord/mx-puppet-discord.git',
        'rev': node.metadata.get('mx-puppet-discord/version', 'main'),
        'triggers': {
            'action:mx-puppet-discord_chown',
            'action:mx-puppet-discord_yarn_install',
            'action:mx-puppet-discord_yarn_build',
        },
    },
}

actions = {
    'mx-puppet-discord_chown': {
        'command': ' && '.join([
            'chown -R mx-puppet-discord:mx-puppet-discord /opt/mx-puppet-discord/.npm',
            'chown -R mx-puppet-discord:mx-puppet-discord /opt/mx-puppet-discord/.yarn',
            'chown -R mx-puppet-discord:mx-puppet-discord /opt/mx-puppet-discord/src',
        ]),
        'needs': {
            'directory:/opt/mx-puppet-discord/.npm',
            'user:mx-puppet-discord',
        },
        'triggered': True,
    },
    'mx-puppet-discord_yarn_install': {
        'command': 'cd /opt/mx-puppet-discord/src && sudo -u mx-puppet-discord yarn install',
        'needs': {
            'action:mx-puppet-discord_chown',
            'pkg_apt:libcairo2-dev',
            'pkg_apt:libgif-dev',
            'pkg_apt:libpango1.0-dev',
            'pkg_apt:libpixman-1-dev',
            'pkg_apt:nodejs',
            'pkg_apt:pkg-config',
            'user:mx-puppet-discord',
        },
        'triggered': True,
    },
    'mx-puppet-discord_yarn_build': {
        'command': 'cd /opt/mx-puppet-discord/src && sudo -u mx-puppet-discord yarn build',
        'needs': {
            'action:mx-puppet-discord_yarn_install',
        },
        'triggers': {
            'svc_systemd:mx-puppet-discord:restart',
        },
        'triggered': True,
    },
}

svc_systemd = {
    'mx-puppet-discord': {
        'needs': {
            'action:mx-puppet-discord_yarn_build',
            'file:/etc/systemd/system/mx-puppet-discord.service',
            'file:/opt/mx-puppet-discord/config.yaml',
            'file:/opt/mx-puppet-discord/registration.yaml',
        },
    },
}

if node.has_bundle('matrix-synapse'):
    files['/opt/mx-puppet-discord/registration.yaml']['triggers'].add('svc_systemd:matrix-synapse:restart')
