from os import listdir
from os.path import join

actions = {
    'fc-cache_flush': {
        'command': 'fc-cache -f',
        'triggered': True,
        'needs': {
            'pkg_pacman:fontconfig',
        },
    },
    'i3pystatus_create_virtualenv': {
        'command': '/usr/bin/python3 -m virtualenv -p python3 /opt/i3pystatus/venv/',
        'unless': 'test -d /opt/i3pystatus/venv/',
        'needs': {
            'directory:/opt/i3pystatus/src',
            'pkg_pacman:python-virtualenv',
        },
    },
    'i3pystatus_install': {
        'command': ' && '.join([
            'cd /opt/i3pystatus/src',
            '/opt/i3pystatus/venv/bin/pip install --upgrade pip colour netifaces basiciw pytz',
            '/opt/i3pystatus/venv/bin/pip install --upgrade -e .',
        ]),
        'needs': {
            'action:i3pystatus_create_virtualenv',
        },
        'triggered': True,
    },
}

directories = {
    '/etc/sddm.conf.d': {
        'purge': True,
    },
    '/opt/i3pystatus/src': {},
    '/usr/share/fonts/bundlewrap': {
        'purge': True,
        'triggers': {
            'action:fc-cache_flush',
        },
    },
}

svc_systemd = {
    'avahi-daemon': {
        'needs': {
            'pkg_pacman:avahi',
        },
    },
    'sddm': {
        'needs': {
            'pkg_pacman:sddm',
        },
    },
}

git_deploy = {
    '/opt/i3pystatus/src': {
        'repo': 'https://github.com/enkore/i3pystatus.git',
        'rev': 'current',
        'triggers': {
            'action:i3pystatus_install',
        },
    },
}

files['/etc/pipewire/pipewire-pulse.conf.d/50-network.conf'] = {}

for filename in listdir(join(repo.path, 'data', 'arch-with-gui', 'files', 'fonts')):
    if filename.startswith('.'):
        continue

    if filename.endswith('.vault'):
        # XXX remove this once we have a new bundlewrap release
        # https://github.com/bundlewrap/bundlewrap/commit/2429b153dd1ca6781cf3812e2dec9c2b646a546b
        from os import environ
        if environ.get('BW_VAULT_DUMMY_MODE', '0') == '1':
            continue

        font_name = filename[:-6]
        attrs = {
            'content': repo.vault.decrypt_file_as_base64(join('arch-with-gui', 'files', 'fonts', filename)),
            'content_type': 'base64',
        }
    else:
        font_name = filename
        attrs = {
            'source': join('fonts', filename),
            'content_type': 'binary',
        }

    files[f'/usr/share/fonts/bundlewrap/{font_name}'] = {
        'triggers': {
            'action:fc-cache_flush',
        },
        **attrs,
    }

if node.metadata.get('arch-with-gui/autologin_as', None):
    files['/etc/sddm.conf.d/autologin.conf'] = {
        'context': {
            'user': node.metadata.get('arch-with-gui/autologin_as'),
        },
        'content_type': 'mako',
        'before': {
            'svc_systemd:sddm',
        },
    }
