#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Copyright (c) 2015, NORDUnet A/S.
# See LICENSE for licensing information.

import sys
import argparse
import readconfig
from certtools import create_ssl_context, get_sth_retry

def main():
    parser = argparse.ArgumentParser(description='')
    parser.add_argument('--raw', action='store_true',
                        help="Print all")
    parser.add_argument('--timestamp', action='store_true',
                        help="Print timestamp")
    parser.add_argument('--roothash', action='store_true',
                        help="Print root hash")
    parser.add_argument('--treesize', action='store_true',
                        help="Print tree size")
    parser.add_argument('--signature', action='store_true',
                        help="Print signature")
    parser.add_argument('--localconfig', help="Local configuration",
                        required=True)
    parser.add_argument('baseurl', help="Log base URL")
    args = parser.parse_args()
    localconfig = readconfig.read_config(args.localconfig)
    paths = localconfig["paths"]

    create_ssl_context(cafile=paths["https_cacertfile"])
    sth = get_sth_retry(args.baseurl, tries=10)

    if args.raw:
        print sth
    if args.timestamp:
        print sth['timestamp']
    if args.roothash:
        print sth['sha256_root_hash']
    if args.treesize:
        print sth['tree_size']
    if args.signature:
        print sth['tree_head_signature']

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