#!/usr/bin/env python

import os
import sys
import bson
import pprint
import datetime
import pymongo
import radical.utils       as ru
import radical.pilot       as rp
import radical.pilot.utils as rpu


# ------------------------------------------------------------------------------
#
def usage (msg=None, noexit=False) :

    if  msg :
        print("\n      Error: %s" % msg)

    print("""
      usage      : %s [<sid>] [<output>]
      example    : %s 5490ba7174df926284f8ef48 -

      arguments  :
        <sid>    : session id
        <output> : output file (default: <sid>.json)

      The tool will look for <sid>.*.bson files in the current pwd, and convert
      them to json, to write them to <sid>.json (default) or ot another output
      location (file or stdout [-]).

""" % (sys.argv[0], sys.argv[0]))

    if  msg :
        sys.exit (1)

    if  not noexit :
        sys.exit (0)


# ------------------------------------------------------------------------------
#
if __name__ == '__main__' :


    sid    = None
    fn_out = None

    if len(sys.argv) <= 1 : usage ("insufficient arguments -- need session ID")
    if len(sys.argv) >  1 : sid    = sys.argv[1]
    if len(sys.argv) >  2 : fn_out = sys.argv[2]
    if len(sys.argv) >  3 : usage ("incorrect number of arguments")

    if not fn_out :
        fn_out = "%s.json" % sid

    f_in_s  = open ("%s.bson"    % sid, 'r')
    f_in_p  = open ("%s.p.bson"  % sid, 'r')
    f_in_pm = open ("%s.pm.bson" % sid, 'r')
    f_in_um = open ("%s.um.bson" % sid, 'r')
    f_in_t  = open ("%s.t.bson"  % sid, 'r')

    bstr_s  = f_in_s .read ()
    bstr_p  = f_in_p .read ()
    bstr_pm = f_in_pm.read ()
    bstr_um = f_in_um.read ()
    bstr_t  = f_in_t .read ()

    bson_s  = bson.BSON (bstr_s )
    bson_p  = bson.BSON (bstr_p )
    bson_pm = bson.BSON (bstr_pm)
    bson_um = bson.BSON (bstr_um)
    bson_t  = bson.BSON (bstr_t )

    json_s  = bson_s .decode ()
    json_p  = bson_p .decode ()
    json_pm = bson_pm.decode ()
    json_um = bson_um.decode ()
    json_t  = bson_t .decode ()

    json_data = dict()
    json_data['session'] = json_s ['sessions'][0]
    json_data['pilot']   = json_p ['pilots']
    json_data['pmgr']    = json_pm['pilot_managers']
    json_data['tmgr']    = json_um['task_managers']
    json_data['task']    = json_t ['tasks']

    if fn_out == '-' :
        pprint.pprint (json_data)
    else :
        ru.write_json (json_data, fn_out)



# ------------------------------------------------------------------------------

