#!/usr/bin/env python3
#
# Copyright (C) 2014-2016 DNAnexus, Inc.
#
# This file is part of dx-toolkit (DNAnexus platform client libraries).
#
#   Licensed under the Apache License, Version 2.0 (the "License"); you may not
#   use this file except in compliance with the License. You may obtain a copy
#   of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#   WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#   License for the specific language governing permissions and limitations
#   under the License.

from __future__ import print_function, unicode_literals, division, absolute_import

import sys, json, argparse
import dxpy
from dxpy.utils import file_load_utils
from dxpy.utils.printing import fill, refill_paragraphs, BOLD, RED


description = '''Parses $HOME/job_input.json and prints the bash
variables that would be available in the execution environment.'''

parser = argparse.ArgumentParser(description=refill_paragraphs(description),
                                 formatter_class=argparse.RawTextHelpFormatter)
args = parser.parse_args()

job_input_file = file_load_utils.get_input_json_file()

var_defs_hash = file_load_utils.gen_bash_vars(job_input_file)
for key, val in var_defs_hash.items():
    val_len = len(val)

    # If the bash variable is very long, this can cause problems down
    # the road.
    #
    # https://groups.google.com/forum/#!topic/comp.unix.programmer/FwYX32Vsjv8
    if val_len >= (16 * 1024):
        msg = "\"warning: variable {} is long ({} bytes)\"".format(key, val_len)
        print("echo {}".format(msg))
    print("export {}={}".format(key, val))
