#!/usr/bin/env python3

# Apache License 2.0
#
# Read full license text at: http://www.apache.org/licenses/LICENSE-2.0
# Copyleft (c) 2020 RiotKit Tech Collective
#
# About RiotKit
#
# We are grassroot activists for social change, we respond to the needs of grassroot organizations,
# check out those fantastic mutual aid initiatives:

#   - International Workers Association (https://iwa-ait.org)
#   - Anarchistyczne FAQ (http://anarchizm.info) a translation of Anarchist FAQ
#     (https://theanarchistlibrary.org/library/the-anarchist-faq-editorial-collective-an-anarchist-faq)
#   - Federacja Anarchistyczna (http://federacja-anarchistyczna.pl)
#   - Związek Syndykalistów Polski (https://zsp.net.pl) (Polish section of IWA-AIT)
#   - Komitet Obrony Praw Lokatorów (https://lokatorzy.info.pl)
#   - Solidarity Federation (https://solfed.org.uk)
#   - Priama Akcia (https://priamaakcia.sk)
#

import re
import sys
import os
import subprocess
from typing import Tuple
from typing import Optional


def get_known_host(url: str) -> Tuple[Optional[str], Optional[int]]:
    if url.startswith('http'):
        return None, None

    match = re.findall('@([A-Za-z-0-9.\-_]+)(?:(:([0-9]+))?)', url)

    if not match:
        raise Exception('Cannot parse git remote "%s"' % url)

    if len(match[0]) >= 3:
        return match[0][0], int(match[0][2])

    return match[0][0], 22


def should_append_to_known_hosts(host: Optional[str], port: Optional[int]):
    if host is None:
        return False

    path = os.path.expanduser('~/.ssh/known_hosts')

    if not os.path.isfile(path):
        return True

    patterns = [
        host + ',',
        host + ' '
    ]

    if port != 22:
        patterns = [host + ']:' + str(port)]

    with open(path, 'rb') as f:
        content = f.read().decode('utf-8')

        for pattern in patterns:
            if pattern in content:
                return False

    return True


def main(url: str):
    host, port = get_known_host(url)

    if should_append_to_known_hosts(host, port):
        try:
            subprocess.check_call('ssh-keyscan -t rsa -p %i %s >> ~/.ssh/known_hosts' % (port, host), shell=True)
        except subprocess.CalledProcessError as e:
            print(str(e))
            sys.exit(1)


if __name__ == '__main__':
    main(sys.argv[1])
    sys.exit(0)
