#!/usr/bin/env python3

#
# Create a PrivateStorage.io-enabled Tahoe-LAFS client node and run it as a
# daemon.  Exit with success when we think we've started it.
#

from os import environ
from sys import argv
from shutil import which
from subprocess import check_output
from configparser import ConfigParser

def main():
    (nodePath, introducerFURL, issuerURL) = argv[1:]

    run(["tahoe", "--version"])
    run([
        "tahoe", "create-client",
        "--shares-needed", "1",
        "--shares-happy", "1",
        "--shares-total", "1",
        "--introducer", introducerFURL,
        nodePath,
    ])

    # Add necessary ZKAPAuthorizer configuration bits.
    config = ConfigParser()
    with open("/tmp/client/tahoe.cfg") as cfg:
        config.read_file(cfg)

    config.set(u"client", u"storage.plugins", u"privatestorageio-zkapauthz-v1")
    config.add_section(u"storageclient.plugins.privatestorageio-zkapauthz-v1")
    config.set(u"storageclient.plugins.privatestorageio-zkapauthz-v1", u"redeemer", u"ristretto")
    config.set(u"storageclient.plugins.privatestorageio-zkapauthz-v1", u"ristretto-issuer-root-url", issuerURL)
    # This has to agree with the PaymentServer configuration at the configured
    # issuer location.  Presently PaymentServer has 50000 hard-coded as the
    # correct value.
    config.set(u"storageclient.plugins.privatestorageio-zkapauthz-v1", u"default-token-count", u"50000")

    with open("/tmp/client/tahoe.cfg", "wt") as cfg:
        config.write(cfg)

    run([
        "daemonize",
        "-o", "/tmp/stdout",
        "-e", "/tmp/stderr",
        which("tahoe"), "run", "/tmp/client",
    ])

def run(argv):
    print("{}: {}".format(argv, check_output(argv)))


if __name__ == '__main__':
    main()