#!/usr/bin/python3
import os
import subprocess
import sys
from pathlib import Path

args = list(sys.argv[1:])
temporary = None
for index, value in enumerate(args):
    path = Path(value)
    if path.suffix == ".ys" and path.is_file():
        temporary = path.with_name(path.stem + ".compat.ys")
        temporary.write_text(path.read_text(encoding="utf-8").replace("read_ilang", "read_rtlil"), encoding="utf-8")
        args[index] = str(temporary)
        break
try:
    raise SystemExit(subprocess.call(["/usr/bin/yosys", *args]))
finally:
    if temporary is not None:
        temporary.unlink(missing_ok=True)
