import os
import subprocess

PORT = 22
USER = 'mspe'
ROOT = 'public_html'
PROTOCOL = 'https'
DIR = False


class PubNix:

    def __init__(self, dn,
                 ssh=None, port=PORT, user=USER,
                 root=ROOT, dir=DIR, web=None, sub=None):
        self.dn = dn
        self.ssh = f'{ssh}.{self.dn}' if ssh else self.dn
        self.port = port
        self.user = user
        self.root = root
        self.web = f'{web}.{self.dn}' if web else self.dn
        if sub:
            self.fqdn = f'{self.user}.{self.web}'
            self.context = None
        else:
            self.fqdn = self.web
            self.context = f'~{self.user}'
        self.path = os.path.join(self.root, self.fqdn) if dir else self.root
        self.target = f'{self.user}@{self.ssh}:{self.path}'
        self.url = [f'{PROTOCOL}:', str(), self.fqdn]
        if self.context:
            self.url.append(self.context)
        self.url = '/'.join(self.url)

    def run(self, *args):
        subprocess.call(['ssh',
                         '-o', 'LogLevel Error',
                         '-p', str(self.port),
                         f'{self.user}@{self.ssh}',
                         '--',
                         *args])

    def disk_free(self):
        self.run('df', '-h', os.curdir)

    def __str__(self):
        return os.linesep.join([self.target, self.url])
