#!/usr/bin/env python3

from jinja2 import Template
import os

# Values: 6 (1.4 MHz), 15 (3MHz), 25 (5MHz), 50 (10MHz), 75 (15MHz), 100 (20MHz)
LTE_N_RB_DL=100
LTE_TX_GAIN=70
LTE_RX_GAIN=30

NR_BANDWIDTH=40
NR_TX_GAIN=70
NR_RX_GAIN=50

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': 'tdd1900',

            'default_dl_earfcn': 38350,
            'default_lte_dl_freq': 1890.0,
            'default_lte_band': 39,
            'default_lte_n_rb_dl': LTE_N_RB_DL,
            'default_lte_tx_gain': LTE_TX_GAIN,
            'default_lte_rx_gain': LTE_RX_GAIN,

            'default_dl_nr_arfcn': 378000,
            'default_nr_band': 39,
            'default_nr_dl_freq': 1890.0,
            'default_nr_bandwidth': NR_BANDWIDTH,
            'default_nr_tx_gain': NR_TX_GAIN,
            'default_nr_rx_gain': NR_RX_GAIN,
            'default_nr_ssb_pos_bitmap': "1000",

            'min_frequency': 1880.0,
            'max_frequency': 1920,
        }, {
            'rf_mode': 'tdd2600',

            'default_dl_earfcn': 38050,
            'default_lte_dl_freq': 2600.0,
            'default_lte_band': 38,
            'default_lte_n_rb_dl': LTE_N_RB_DL,
            'default_lte_tx_gain': LTE_TX_GAIN,
            'default_lte_rx_gain': LTE_RX_GAIN,

            'default_dl_nr_arfcn': 520000,
            'default_nr_band': 38,
            'default_nr_dl_freq': 2600.0,
            'default_nr_bandwidth': NR_BANDWIDTH,
            'default_nr_tx_gain': NR_TX_GAIN,
            'default_nr_rx_gain': NR_RX_GAIN,
            'default_nr_ssb_pos_bitmap': "10000000",

            'min_frequency': 2570,
            'max_frequency': 2620,
        }, {
            'rf_mode': 'tdd3500',

            'default_dl_earfcn': 42590,
            'default_lte_dl_freq': 3500.0,
            'default_lte_band': 42,
            'default_lte_n_rb_dl': LTE_N_RB_DL,
            'default_lte_tx_gain': LTE_TX_GAIN,
            'default_lte_rx_gain': LTE_RX_GAIN,

            'default_dl_nr_arfcn': 632628,
            'default_nr_band': 78,
            'default_nr_dl_freq': 3489.42,
            'default_nr_bandwidth': NR_BANDWIDTH,
            'default_nr_tx_gain': NR_TX_GAIN,
            'default_nr_rx_gain': NR_RX_GAIN,
            'default_nr_ssb_pos_bitmap': "10000000",

            'min_frequency': 3400,
            'max_frequency': 3600,
        }, {
            'rf_mode': 'tdd3700',

            'default_dl_earfcn': 44590,
            'default_lte_dl_freq': 3700.0,
            'default_lte_band': 43,
            'default_lte_n_rb_dl': LTE_N_RB_DL,
            'default_lte_tx_gain': LTE_TX_GAIN,
            'default_lte_rx_gain': LTE_RX_GAIN,

            'default_dl_nr_arfcn': 646666,
            'default_nr_band': 78,
            'default_nr_dl_freq': 3699.99,
            'default_nr_bandwidth': NR_BANDWIDTH,
            'default_nr_tx_gain': NR_TX_GAIN,
            'default_nr_rx_gain': NR_RX_GAIN,
            'default_nr_ssb_pos_bitmap': "10000000",

            'min_frequency': 3600,
            'max_frequency': 3800,
        }
]

with open('software.cfg.jinja2.json', 'r') as f:
    software_json_template = Template(f.read())
with open('instance-enb-input-schema.jinja2.json', 'r') as f:
    instance_enb_json_template = Template(f.read())
with open('instance-gnb-input-schema.jinja2.json', 'r') as f:
    instance_gnb_json_template = Template(f.read())
with open('software.jinja2.cfg', 'r') as f:
    software_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))
    with open('software-{}.cfg'.format(rf_mode_context['rf_mode']),
              'w+') as f:
        f.write(software_template.render(**rf_mode_context, **global_context))
    for software_type in ['enb', 'enb-epc', 'gnb', 'gnb-epc']:
        with open('instance-{}-{}-input-schema.json'.format(
                    rf_mode_context['rf_mode'],
                    software_type),
                  'w+') as f:
            if software_type in ['enb', 'enb-epc']:
                f.write(instance_enb_json_template.render(**rf_mode_context, **global_context))
            else:
                f.write(instance_gnb_json_template.render(**rf_mode_context, **global_context))
