import re
from pathlib import Path
Import('env', 'arch', 'common')

# build the fonts
generator = File("#selfdrive/assets/fonts/process.py")
source_files = Glob("#selfdrive/assets/fonts/*.ttf") + Glob("#selfdrive/assets/fonts/*.otf")
output_files = [
  (f"#{Path(f.path).with_suffix('.fnt')}", f"#{Path(f.path).with_suffix('.png')}")
  for f in source_files
  if "NotoColor" not in f.name
]
env.Command(
  target=output_files,
  source=[generator, source_files],
  action=f"python3 {generator}",
)


if GetOption('extras') and arch == "larch64":
  # build installers
  if arch != "Darwin":
    raylib_env = env.Clone()
    raylib_env['LIBPATH'] += [f'#third_party/raylib/{arch}/']
    raylib_env['LINKFLAGS'].append('-Wl,-strip-debug')

    raylib_libs = common + ["raylib"]
    if arch == "larch64":
      raylib_libs += ["GLESv2", "EGL", "gbm", "drm"]
    else:
      raylib_libs += ["GL"]

    release = "release3"
    installers = [
      ("openpilot", release),
      ("openpilot_test", f"{release}-staging"),
      ("openpilot_nightly", "nightly"),
      ("openpilot_internal", "nightly-dev"),
    ]

    cont = raylib_env.Command("installer/continue_openpilot.o", "installer/continue_openpilot.sh",
                              "ld -r -b binary -o $TARGET $SOURCE")
    inter = raylib_env.Command("installer/inter_ttf.o", "installer/inter-ascii.ttf",
                               "ld -r -b binary -o $TARGET $SOURCE")
    inter_bold = raylib_env.Command("installer/inter_bold.o", "../assets/fonts/Inter-Bold.ttf",
                                "ld -r -b binary -o $TARGET $SOURCE")
    inter_light = raylib_env.Command("installer/inter_light.o", "../assets/fonts/Inter-Light.ttf",
                                "ld -r -b binary -o $TARGET $SOURCE")
    for name, branch in installers:
      d = {'BRANCH': f"'\"{branch}\"'"}
      if "internal" in name:
        d['INTERNAL'] = "1"

      obj = raylib_env.Object(f"installer/installers/installer_{name}.o", ["installer/installer.cc"], CPPDEFINES=d)
      f = raylib_env.Program(f"installer/installers/installer_{name}", [obj, cont, inter, inter_bold, inter_light], LIBS=raylib_libs)
      # keep installers small
      assert f[0].get_size() < 2500*1e3, f[0].get_size()
