#!/bin/bash

# ideone boilerplate - we can't write files in the home directory;
# so create a temporary directory for our files instead
t=$(mktemp -d -t ideone.XXXXXXXXXXXX) || exit
trap 'rm -rf "$t"' ERR EXIT
cd "$t"

cat <<\: >model.mo
unmodified line
you could say that moduleName = "control" and you would be right
fnord.CombiTimeTable solar_data(columns = {2}', 1000!)
fnord.CombiTimeTable ev_data(columns = {2}', 9999!)
Storage.Battery BESS(EMax = 2000000000, SOC_start = 0.5, pf = 0.9)
:

cat <<\:> prog.py
with open('model.mo') as modelica:
    script = modelica.read()
# did you really want a space instead of an underscore in 'control 4'?
for i in ['control_1', 'control_2', 'control_3', 'control 4']:
    ivar = script.replace(
        'moduleName = "control"',
        # guessing you wanted to keep the double quotes around the name here
        f'moduleName = "control_{i}"')
    for j in [3, 7]:
        jvar = ivar.replace(
            '.CombiTimeTable solar_data(columns = {2}',
            f'.CombiTimeTable solar_data(columns = {{{j}}}')
        for k in [2000000000, 4000000000, 6000000000]:
            kvar = jvar.replace(
                'Storage.Battery BESS(EMax = 2000000000, SOC_start = 0.5, pf = 0.9)',
                f'Storage.Battery BESS(EMax = {k}, SOC_start = 0.5, pf = 0.9)')
            for l in ['4', '8']:
                lvar = kvar.replace(
                    '.CombiTimeTable ev_data(columns = {2}',
                    # guessing you meant to keep the braces, and you wanted l, not i?
                    f'.CombiTimeTable ev_data(columns = {{{l}}}')
                # Guessing as to what exactly the file name should be
                with open(f'child_model_{i}_{j}_{k}_{l}.mo', 'w') as child:
                    child.write(lvar)
:

python3 prog.py

tail child_model_*
