#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2017, NORDUnet A/S.
# See LICENSE for licensing information.
#
import sys
import json
import urllib2
import time
import requests
import base64
from base64 import b64encode
from mergetools import parse_args, get_nfetched, hexencode, hexdecode, \
     get_logorder, get_sth
from certtools import create_ssl_context, get_public_key_from_file, \
     timing_point, create_sth_signature, write_file, check_sth_signature, \
     build_merkle_tree

def main():
    args, config, localconfig = parse_args()
    paths = localconfig["paths"]
    mergenodes = config.get("mergenodes", [])
    mergedb = paths["mergedb"]
    sthfile = mergedb + "/sth"
    currentsizefile = mergedb + "/fetched"

    sth = json.loads(open(sthfile, "r").read())
    currentsize = json.loads(open(currentsizefile, "r").read())
    
    print >>sys.stderr, currentsize["index"]+1,
    
    for mergenode in mergenodes:
        if mergenode["name"] == config["primarymergenode"]:
            continue
        verifiedfile = mergedb + "/verified." + mergenode["name"]
        try:
            tree = json.loads(open(verifiedfile, "r").read())
            print >>sys.stderr, tree["tree_size"],
        except (IOError, ValueError):
            pass
    print >>sys.stderr, sth["tree_size"],
    print

if __name__ == '__main__':
    sys.exit(main())