#!/usr/bin/env python3

from jinja2 import Template
import argparse
import os

parser = argparse.ArgumentParser()
parser.add_argument('-d', '--delete', action='store_true')
args = parser.parse_args()

if args.delete:
  directory = os.path.dirname(os.path.realpath(__file__))
  test_directory = os.path.join(directory, 'test')
  for f in os.listdir(directory):
    if 'tdd' in f:
      os.remove(os.path.join(directory, f))
  for f in os.listdir(test_directory):
    if 'testTDD' in f:
      os.remove(os.path.join(test_directory, f))
  exit()

global_context = {
        'generated_file_message': "This file was generated using a jinja2 template and the render-templates script, don't modify directly."
}

rf_mode_context_list = [
        {
            'rf_mode'                      : 'tdd',
        }, {
            'rf_mode'                      : 'fdd',
        }
]

for i in range (len(rf_mode_context_list)):
    rf_mode_context_list[i].update({
            'default_lte_n_rb_dl'          : 100,
            'default_lte_imsi'             : "001010123456789",
            'default_lte_k'                : "00112233445566778899aabbccddeeff",
            'default_lte_inactivity_timer' : 10000,
            'default_nr_bandwidth'         : 40,
            'default_nr_imsi'              : "001010123456789",
            'default_nr_k'                 : "00112233445566778899aabbccddeeff",
            'default_nr_ssb_pos_bitmap'    : "10000000",
            'default_n_antenna_dl'         : 2,
            'default_n_antenna_ul'         : 2,
            'default_nr_inactivity_timer' : 10000,
    })

with open('software.cfg.json.jinja2', 'r') as f:
    software_json_template = Template(f.read())

instance_json_template_map = {}
for software_type in ['enb', 'gnb', 'ue-lte', 'ue-nr']:
    with open('instance-{}-input-schema.json.jinja2'.format(software_type), 'r') as f:
        instance_json_template_map[software_type] = Template(f.read())

with open('software.jinja2.cfg', 'r') as f:
    software_template = Template(f.read())
with open('test/test.jinja2.py', 'r') as f:
    test_template = Template(f.read())

for rf_mode_context in rf_mode_context_list:
    with open('software-{}.cfg.json'.format(rf_mode_context['rf_mode']),
              'w+') as f:
        f.write(software_json_template.render(**rf_mode_context, **global_context) + '\n')
    with open('test/test{}.py'.format(rf_mode_context['rf_mode'].upper()),
              'w+') as f:
        f.write(test_template.render(**rf_mode_context, **global_context) + '\n')
    with open('software-{}.cfg'.format(rf_mode_context['rf_mode']),
              'w+') as f:
        f.write(software_template.render(**rf_mode_context, **global_context) + '\n')
    for software_type in ['enb', 'gnb', 'ue-lte', 'ue-nr']:
        with open('instance-{}-{}-input-schema.json'.format(
                    rf_mode_context['rf_mode'],
                    software_type),
                  'w+') as f:
            f.write(instance_json_template_map[software_type].render(**rf_mode_context, **global_context) + '\n')
