#!/usr/bin/env python3

import json
import os
import subprocess

def mkdir_p(path):
    if path == "":
        return
    try:
        os.makedirs(path)
    except FileExistsError:
        pass
    return

def create_keys(seed, count):
    keyout = subprocess.check_output(
    [
     "programs/utils/bts_create_key",
     "--count="+str(count),
     "--seed="+seed,
    ],
    )
    return json.loads(keyout.decode())

genesis_filename = "tmp/genesis.json"

init_keys = create_keys("init", 101)
angel_keys = create_keys("angel", 10)

names = ["alice", "bob", "charlie", "dan", "emily", "fox", "galt", "henry", "isabelle", "janet", "kate", "lisa", "matt", "nate", "otto", "pam", "quixote", "ron", "stan", "tony", "ulysses", "vik", "walt", "xavier", "yoshi", "zack"]

name_keys = []

for name in names:
    name_keys.extend(create_keys(name, 10))

init_balance  = 300000000000
angel_balance = 2000000000000
name_balance  = 10000000000

balances = [[k["pts_address"],  init_balance] for k in  init_keys
         ]+[[k["pts_address"], angel_balance] for k in angel_keys
         ]+[[k["pts_address"],  name_balance] for k in  name_keys]

genesis_json = {
  "timestamp" : "2014-02-02T18:00:00",

  "market_assets" : [
    {
        "symbol" : "USD",
        "name" : "United States Dollar",
        "descrption" : "Federally Reserved, Inflation Guaranteed",
        "precision" : 10000
    },
    {
        "symbol" : "BTC",
        "name" : "Bitcoin",
        "description" : "This is an internet cryptocurrency.  All craftdwarfship is of the highest quality.  On the item is an image of Urist Nakamoto the dwarf in silicon.",
        "precision" : 100000000
    },
    {
        "symbol" : "GAS",
        "name" : "Gasoline",
        "description" : "Secondary fuel source used by some electric vehicles",
        "precision" : 10000
    }
  ],
  
  "names" :
  [
    {
      "name" : "init"+str(i),
      "owner" : init_keys[i]["public_key"],
      "delegate_pay_rate" : 1,
    }
    for i in range(101)
  ],

  "balances" : balances,
}

bts_sharedrop = []

mkdir_p(os.path.dirname(genesis_filename))
with open(genesis_filename, "w") as f:
    json.dump(genesis_json, f, indent=4)

@@ -1128,16 +1128,361 @@
 _keys%5D%0A%0A
+BTS_BLOCKCHAIN_MAX_SHARES = (1000*1000*1000*1000*1000)%0ABTS_BLOCKCHAIN_INITIAL_SHARES = (BTS_BLOCKCHAIN_MAX_SHARES // 5)%0A%0Atotal_balance = sum(b%5B1%5D for b in balances)%0A%0Aballast_keys = create_keys(%22ballast%22, 1)%0Aballast_balance = BTS_BLOCKCHAIN_INITIAL_SHARES - total_balance%0A%0Abalances += %5B%5Bk%5B%22pts_address%22%5D, ballast_balance%5D for k in ballast_keys%5D%0A%0A
 genesis_

Add "ballast" to tscript genesis so balances in genesis file equal XTS balances