#!/usr/bin/env python3

import sys
import fileinput
import uuid
from subprocess import check_output

import yaml

import lib_rchain_key
import lib_rnode_tls

nodes = []

for hostname in fileinput.input():
    hostname = hostname.strip()
    key, pub_key = lib_rchain_key.generate_key_pair_hex()
    tls_key, tls_cert, node_id = lib_rnode_tls.generate_node_tls_key_cert_id()
    rdoctor_apikey = check_output([
        'curl', '-fsSL', '-XPOST',
        'https://rdoctor.rchain-dev.tk/api/newkey?identity=' + hostname
    ], text=True)
    nodes.append({
        'hostname': hostname,
        'node_id': node_id,
        'key': key,
        'pub_key': pub_key,
        'tls_key': tls_key,
        'tls_cert': tls_cert,
        'rdoctor_apikey': rdoctor_apikey,
    })

yaml.add_representer(
    str, lambda dumper, data: dumper.represent_scalar(
        'tag:yaml.org,2002:str', data, style='|' if '\n' in data else None))
yaml.dump(nodes, sys.stdout, default_flow_style=False, sort_keys=False)
