#! /usr/bin/env python3

import os
import subprocess

import pubnix

ARGS = [
    {'dn':'blinkenshell.org',
     'ssh': 'ssh', 'port': 2222, 'web': 'u', 'sub': True},
    {'dn':'ctrl-c.club', 'sub': False},
    {'dn':'dimension.sh', 'sub': True},
    {'dn':'envs.net', 'sub': True},
    {'dn':'freeshell.de', 'sub': False},
    # {'dn':'hextilde.xyz', 'sub': True},
    {'dn':'insomnia247.nl', 'dir': True, 'sub': True},
    {'dn':'p.projectsegfau.lt', 'sub': True},
    # {'dn':'pubnix.pink', 'web': 'sites', 'sub': False},
    {'dn':'rawtext.club', 'sub': False},
    {'dn':'rw.rs', 'sub': False},
    {'dn':'thunix.net', 'sub': False},
    {'dn':'tilde.32bit.cafe', 'root': 'www', 'sub': False},
    {'dn':'tilde.cafe', 'sub': True},
    {'dn':'tilde.club', 'sub': False},
    {'dn':'tilde.fun', 'root': 'html', 'sub': False},
    {'dn':'tilde.green', 'sub': False},
    {'dn':'tilde.guru', 'sub': False},
    {'dn':'tilde.institute', 'sub': True},
    {'dn':'tilde.pink', 'sub': False},
    {'dn':'tilde.team', 'sub': True},
    {'dn':'tilde.town', 'sub': False},
    # {'dn':'trash.town', 'sub': False},
    # permissions
    # {'dn':'sdf.org', 'root': 'html', 'sub': True},
    # old
    {'dn':'fr.tild3.org', 'sub': True},
    {'dn':'remotes.club', 'port': 9022, 'root': 'web', 'sub': True},
    {'dn':'squiggle.city', 'sub': False},
    # down
    # {'dn':'aussies.space', 'sub': False},
    # {'dn':'heathens.club', 'root': 'www', 'sub': False},
    # {'dn':'vern.cc', 'sub': True},
]
PUBNIXES = [pubnix.PubNix(**args) for args in ARGS]


def sync(root, pubnix, exclude=None):
    args = [
        'rsync',
        '--archive',
        '--checksum',
        '--delete-before',
        '--rsh', f"ssh -o 'LogLevel Error' -p {pubnix.port}",
        '--partial',
        '--progress',
        '--verbose',
        os.path.join(root, str()),
    ]
    for item in exclude:
        args.extend(['--exclude', os.path.join(str(), item)])
    args.append(os.path.join(pubnix.target, str()))
    subprocess.call(args, stdout=subprocess.DEVNULL)


def main():
    root = os.path.dirname(os.path.realpath(__file__))
    root = os.path.join(root, 'out', 'web')
    dns_length = max([len(pubnix.dn) for pubnix in PUBNIXES])
    for pubnix in PUBNIXES:
        print()
        print(pubnix)
        # print(f'{pubnix.dn.rjust(dns_length)} → ', end=str(), flush=True)
        sync(root, pubnix, exclude=['__pycache__', 'pgp.asc'])
        # print(pubnix.disk_free())
        # print(pubnix.os())


if __name__ == '__main__':
    main()
