Import("env")  # pylint: disable=undefined-variable
env.Execute("\"$PYTHONEXE\" -m pip install jinja2")

import struct2json
import dtcs
from os import popen

git_revision = popen('git rev-parse --short HEAD').read().strip()

# Versionsnummern aus platformio.ini holen
custom_firmware_version = env.GetProjectOption("custom_firmware_version", "0.99")
custom_flash_version = env.GetProjectOption("custom_flash_version", "0.99")

# Versionsnummern aufteilen in Major und Minor
fw_major, fw_minor = custom_firmware_version.split('.')
fl_major, fl_minor = custom_flash_version.split('.')

fw_major_int = int(fw_major)
fw_minor_int = int(fw_minor)
fl_major_int = int(fl_major)
fl_minor_int = int(fl_minor)

# Version in Datei "version" im Ordner "data_src" überschreiben
with open('data_src/version', 'w') as version_file:
    version_file.write(custom_flash_version)
    
# Build-Flags setzen
env.Replace(PROGNAME="firmware_%s_%s.fw" % (custom_firmware_version, git_revision))
env.Append(CPPDEFINES=[
    ('GIT_REV', '\\"{}\\"'.format(git_revision)),
    ('FW_MAJOR', fw_major_int),
    ('FW_MINOR', fw_minor_int),
    ('FL_MAJOR', fl_major_int),
    ('FL_MINOR', fl_minor_int)
])

struct2json.struct2json()
dtcs.build_dtcs()