#!/usr/bin/env python3
"""FTP-upload of build/. Never --delete.

The path guard (deploy_guard) is the only hard abort: wrong target,
FTP root, missing allianties leaf. Extra files already on the remote are
logged and left alone. favicon.ico is never uploaded, so a host- or
Tim-provided icon is not overwritten.
"""
import os
import subprocess
import sys
import tempfile
from pathlib import Path
from urllib.parse import urlsplit

sys.path.insert(0, str(Path(__file__).resolve().parent))
from deploy_guard import DeployGuardError, project_dir  # noqa: E402

ROOT = Path(__file__).resolve().parents[1]

# Files this project publishes. Used only to label remote leftovers in the
# log; foreign names no longer abort the deploy (we never --delete).
OURS = {
    'index.html', 'game.js', 'style.css', 'images',
    'api.php', 'auth.php', 'callback.php', 'logout.php', '.htaccess',
    'lib', 'data', 'router.php',
    '.', '..',
}

# Never upload this name: the remote icon may belong to Tim or the host.
LEAVE_ON_REMOTE = ('favicon.ico',)

# OAuth file written from CI secrets. Names match BlueGit project secrets.
# The group and sibling repos (verhalenboek, microchess, …) have no
# auth.timfalken.com client secret — only FTP credentials — so this is a
# allianties project secret. client_id is public and optional to override.
AUTH_SECRET_ENV = 'auth_client_secret'
AUTH_CLIENT_ID_ENV = 'auth_client_id'
DEFAULT_AUTH_URL = 'https://auth.timfalken.com'
DEFAULT_BASE_URL = 'https://projects.timfalken.com/allianties'
DEFAULT_CLIENT_ID = 'projects.timfalken.com'


def quoted(value):
    if any(c in value for c in '\r\n\x00'):
        raise ValueError('Deployment variables must not contain line breaks or NUL bytes.')
    return '"' + value.replace('\\', '\\\\').replace('"', '\\"') + '"'


def php_single_quoted(value):
    """A PHP single-quoted string. Raises if the value has a NUL or newline."""
    if any(c in value for c in '\r\n\x00'):
        raise ValueError('auth config values must not contain line breaks or NUL bytes.')
    return "'" + value.replace('\\', '\\\\').replace("'", "\\'") + "'"


def auth_secret(environ):
    """Return the stripped secret, or '' if unset/empty. Never print this."""
    raw = environ.get(AUTH_SECRET_ENV)
    if raw is None:
        return ''
    return str(raw).strip()


def auth_config_php(environ):
    """PHP source for data/config.php, or None if the secret is empty.

    The secret itself is only placed inside a single-quoted PHP string.
    Callers must not log the return value.
    """
    secret = auth_secret(environ)
    if not secret:
        return None
    client_id = (environ.get(AUTH_CLIENT_ID_ENV) or '').strip() or DEFAULT_CLIENT_ID
    auth_url = DEFAULT_AUTH_URL
    base_url = DEFAULT_BASE_URL
    # Built as a list so the secret is only interpolated via php_single_quoted.
    return (
        '<?php\n'
        '// Written by CI from the auth_client_secret project secret. Not in git.\n'
        'return [\n'
        f"    'auth_url' => {php_single_quoted(auth_url)},\n"
        f"    'base_url' => {php_single_quoted(base_url)},\n"
        f"    'client_id' => {php_single_quoted(client_id)},\n"
        f"    'client_secret' => {php_single_quoted(secret)},\n"
        '];\n'
    )


def host_url(environ):
    for key in ['timftphost', 'timftpuser', 'timftppassword']:
        if not environ.get(key):
            raise ValueError(f'CI variable {key} is not set.')
    host = environ['timftphost']
    if '://' not in host:
        host = 'ftp://' + host
    parsed = urlsplit(host)
    if parsed.scheme not in ['ftp', 'ftps', 'sftp'] or not parsed.hostname:
        raise ValueError('timftphost must be an FTP, FTPS or SFTP host URL.')
    if parsed.username or parsed.password or parsed.query or parsed.fragment:
        raise ValueError('Keep login credentials in timftpuser/timftppassword; use a plain host URL.')
    return host


def lftp_preamble(environ, host):
    return [
        'set cmd:fail-exit yes',
        'set net:timeout 20',
        'set net:max-retries 2',
        'set net:reconnect-interval-base 3',
        'set ssl:verify-certificate no',
        'set ftp:list-options -a',
        'set sftp:connect-program "ssh -a -x -o StrictHostKeyChecking=accept-new"',
        'set sftp:auto-confirm yes',
        'set xfer:use-temp-file yes',
        f'open --user {quoted(environ["timftpuser"])} --env-password {quoted(host)}',
    ]


def run_lftp(commands, environ):
    subprocess.run(
        ['lftp', '--norc'],
        input='\n'.join(commands) + '\n',
        text=True,
        env={**environ, 'LFTP_PASSWORD': environ['timftppassword']},
        check=True,
    )


def list_remote(environ, target):
    """Return (exists, names). exists=False means the folder is not there yet."""
    host = host_url(environ)
    # Probe with cls; a missing folder makes `cd` fail.
    probe = lftp_preamble(environ, host) + [
        'set cmd:fail-exit no',
        f'cd {quoted(target)} && cls -1 && echo __ALLIANTIES_DIR_OK__ || echo __ALLIANTIES_DIR_MISSING__',
        'bye',
    ]
    result = subprocess.run(
        ['lftp', '--norc'],
        input='\n'.join(probe) + '\n',
        text=True,
        env={**environ, 'LFTP_PASSWORD': environ['timftppassword']},
        check=True,
        capture_output=True,
    )
    out = (result.stdout or '') + (result.stderr or '')
    if '__ALLIANTIES_DIR_MISSING__' in out and '__ALLIANTIES_DIR_OK__' not in out:
        return False, []
    names = []
    for line in out.splitlines():
        name = line.strip().rstrip('/')
        if not name or name.startswith('__ALLIANTIES_') or name.startswith('lftp'):
            continue
        # lftp may print status; keep path-like tokens
        if ' ' in name and not name.startswith('.'):
            # long listing leftover; take the last field
            name = name.split()[-1]
        names.append(posix_basename(name))
    return True, names


def posix_basename(name):
    return name.rstrip('/').split('/')[-1]


def assert_safe_target(exists, names):
    """Path safety lives in deploy_guard.project_dir. Remote leftovers are fine."""
    if not exists:
        print('Remote allianties folder does not exist yet; it will be created.')
        return
    foreign = []
    for name in names:
        leaf = posix_basename(name)
        if leaf in OURS or leaf == '' or leaf in LEAVE_ON_REMOTE:
            continue
        foreign.append(leaf)
    if foreign:
        print(
            'Opmerking: de remote map bevat ook bestanden die niet bij deze '
            'deploy horen: '
            + ', '.join(sorted(set(foreign))[:12])
            + '. Die blijven staan (geen --delete) en worden niet aangeraakt.'
        )
    reserved = [posix_basename(n) for n in names if posix_basename(n) in LEAVE_ON_REMOTE]
    if reserved:
        print(
            'Opmerking: '
            + ', '.join(sorted(set(reserved)))
            + ' blijft ongemoeid; we uploaden dat bestand niet.'
        )
    if names:
        print('Remote allianties folder already holds files; updating our own.')
    else:
        print('Remote allianties folder is empty; deploying.')


def deploy_commands(environ, target, webroot, auth_config_path=None):
    host = host_url(environ)
    commands = lftp_preamble(environ, host) + [
        f'mkdir -p {quoted(target)} || echo "Using existing project directory"',
        f'cd {quoted(target)}',
        # GEEN --delete. Never upload favicon.ico (leave Tim's / the host's).
        # Never mirror data/: that folder holds the sqlite database and
        # sessions. We only ensure data/.htaccess afterwards, and optionally
        # write data/config.php from auth_client_secret (never from the
        # build tree, which must not contain a secret).
        'mirror --reverse --no-perms --no-symlinks --max-errors=1 '
        '--exclude-glob ".git/" --exclude-glob ".git/**" '
        '--exclude-glob "blue-ci-cd.yml" --exclude-glob "README.md" '
        '--exclude-glob "favicon.ico" --exclude-glob "**/favicon.ico" '
        '--exclude-glob "router.php" '
        '--exclude-glob "data/" --exclude-glob "data/**" '
        '--exclude-glob "**/config.php" --exclude-glob "**/*.sqlite" '
        f'{quoted(str(webroot))} .',
        # Ensure the deny-all htaccess exists; do not delete any data file.
        'mkdir -p data || echo "data exists"',
        f'put {quoted(str(webroot / "data" / ".htaccess"))} -o data/.htaccess',
    ]
    if auth_config_path:
        # The lftp script only has the local path, never the secret.
        commands.append(
            f'put {quoted(str(auth_config_path))} -o data/config.php'
        )
    commands.append('bye')
    return commands


def main():
    environ = os.environ
    try:
        target = project_dir(environ)
        webroot = ROOT / 'build'
        if not (webroot / 'index.html').is_file() or not (webroot / 'game.js').is_file():
            raise ValueError('build/ is incomplete (need index.html and game.js).')
        if not (webroot / 'style.css').is_file() or not (webroot / 'images').is_dir():
            raise ValueError('build/ is incomplete (need style.css and images/).')
        exists, names = list_remote(environ, target)
        assert_safe_target(exists, names)
        php = auth_config_php(environ)
        auth_path = None
        tmpdir = None
        try:
            if php is not None:
                tmpdir = tempfile.TemporaryDirectory(prefix='allianties-auth-')
                auth_path = Path(tmpdir.name) / 'config.php'
                # 0o600: the secret lives in this file only until lftp puts it.
                fd = os.open(auth_path, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600)
                try:
                    os.write(fd, php.encode('utf-8'))
                finally:
                    os.close(fd)
                print('Writing data/config.php from CI secret auth_client_secret.')
            else:
                print(
                    'CI secret auth_client_secret is empty or unset; '
                    'leaving data/config.php alone.'
                )
            run_lftp(
                deploy_commands(environ, target, webroot, auth_config_path=auth_path),
                environ,
            )
        finally:
            if tmpdir is not None:
                tmpdir.cleanup()
        print('Deploy complete (no --delete).')
        return 0
    except (ValueError, DeployGuardError, subprocess.CalledProcessError) as error:
        print(str(error), file=sys.stderr)
        return 1


if __name__ == '__main__':
    sys.exit(main())
