#!/usr/bin/env python3
"""Compile the production modal/button arbiter and shell navigation on host."""
from pathlib import Path
import re
import subprocess
import tempfile
repo=Path(__file__).resolve().parents[2]
source=(repo/'boards/m5stack_stopwatch/board.cpp').read_text()
names=('handle_gesture','poll_shell_button','board_next_page','board_previous_page')
with tempfile.TemporaryDirectory(prefix='shell-input-') as folder:
    temp=Path(folder)
    text='\n'.join(re.search(r'^[^\n]*\b'+name+r'\([^;]*?\)\s*\{.*?^}',source,re.M|re.S).group() for name in names)
    (temp/'shell_input.inc').write_text(text)
    subprocess.run(['c++','-std=c++17','-Wall','-Wextra','-Werror','-I',str(temp),
        '-I',str(repo/'firmware/tests/nvs_stub'),'-I',str(repo/'boards/common/include'),
        str(repo/'firmware/tests/shell_input_test.cpp'),'-o',str(temp/'test')],check=True)
    subprocess.run([str(temp/'test')],check=True)
