import os
import sys
import subprocess
import json
import datetime
import re #regex


input_log = []

input_log.append(">>> wallet_create default password")
input_log.append(">>> wallet_set_automatic_backups false ")

new_genesis = {
        "timestamp": datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S"),
        "market_assets": [],
        "names": [],
        "balances": []
}

for i in range(101):
    keys = json.loads(subprocess.check_output(["./programs/utils/bts_create_key"]))
    input_log.append(">>> wallet_import_private_key " + keys["wif_private_key"])

    acct = {
        "name": "init" + str(i),
        "owner": keys["public_key"],
        "delegate_pay_rate": 1
    }
    new_genesis["names"].append(acct)

    balances = [keys["pts_address"], 100]


input_log.append(">>> wallet_delegate_set_block_production ALL true")
input_log.append(">>> wallet_set_transaction_scanning true")

with open("libraries/blockchain/include/bts/blockchain/config.hpp") as config:
    with open("tmp_config", "w") as tmp:
        for line in config:
            if line.startswith("#define BTS_TEST_NETWORK_VERSION"):
                n = int(re.findall(r'\d+', line)[0])
                tmp.write("#define BTS_TEST_NETWORK_VERSION                            " + str(n+1) + " // autogenerated\n")
            else:
                tmp.write(line)

with open("libraries/blockchain/genesis.json", "w") as genesis:
    genesis.write(json.dumps(new_genesis, indent=4))


with open("testnet_setup_log.txt", "w") as log:
    for line in input_log:
        log.write(line + "\n")

os.rename("tmp_config", "libraries/blockchain/include/bts/blockchain/config.hpp")

subprocess.call(["make", "-j4"])
subprocess.call(["./programs/client/bitshares_client", "--input-log", "testnet_setup_log.txt"])


print "\n\n"
print "Setup complete. Delegates are in wallet 'default' with password 'password'."
print "You may wish to commit and push your changes so others can join your network."

import os
import sys
import subprocess
import json
import datetime
import re #regex


input_log = []

input_log.append(">>> wallet_create default password")
input_log.append(">>> wallet_set_automatic_backups false ")

new_genesis = {
        "timestamp": datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S"),
        "market_assets": [],
        "names": [],
        "balances": [],
        "bts_sharedrop": []
}

new_genesis["bts_sharedrop"].append({
    "raw_address": "XTSAY7NeKAAZ3n2B637SVFo8SzNoZ8DvoifX",
    "balance": 100000,
    "type": "bts_exodus_sharedrop"
})

for i in range(101):
    keys = json.loads(subprocess.check_output(["./programs/utils/bts_create_key"]))
    input_log.append(">>> wallet_import_private_key " + keys["wif_private_key"])

    acct = {
        "name": "init" + str(i),
        "owner": keys["public_key"],
        "delegate_pay_rate": 1
    }
    new_genesis["names"].append(acct)

    balances = [keys["pts_address"], 100]


input_log.append(">>> wallet_delegate_set_block_production ALL true")
input_log.append(">>> wallet_set_transaction_scanning true")

with open("libraries/blockchain/include/bts/blockchain/config.hpp") as config:
    with open("tmp_config", "w") as tmp:
        for line in config:
            if line.startswith("#define BTS_TEST_NETWORK_VERSION"):
                n = int(re.findall(r'\d+', line)[0])
                tmp.write("#define BTS_TEST_NETWORK_VERSION                            " + str(n+1) + " // autogenerated\n")
            else:
                tmp.write(line)
os.rename("tmp_config", "libraries/blockchain/include/bts/blockchain/config.hpp")


with open("libraries/net/include/bts/net/config.hpp") as config:
    with open("tmp_config", "w") as tmp:
        for line in config:
            if line.startswith("#define BTS_NET_TEST_SEED_IP"):
                n = int(re.findall(r'\d+', line)[0])
                tmp.write("#define BTS_NET_TEST_SEED_IP                            " + str("\"127.0.0.1\"") + " // autogenerated\n")
            else:
                tmp.write(line)
os.rename("tmp_config", "libraries/net/include/bts/net/config.hpp")

with open("libraries/blockchain/genesis.json", "w") as genesis:
    genesis.write(json.dumps(new_genesis, indent=4))


with open("testnet_setup_log.txt", "w") as log:
    for line in input_log:
        log.write(line + "\n")


subprocess.call(["make", "-j4"])
subprocess.call(["./programs/client/bitshares_client", "--input-log", "testnet_setup_log.txt", "--min-delegate-connection-count", "0"])


print "\n\n"
print "Setup complete. Delegates are in wallet 'default' with password 'password'."
print "You may wish to commit and push your changes so others can join your network."

update new_testnet