#!/usr/bin/python3
import os
import sys
def remove_ext(path: str) -> str:
    end = path.split('/')[-1]
    idx = end.find('.')
    if idx == -1:
        return path
    end = end[idx:]
    return path.removesuffix(end)
input = sys.argv[1]
if len(sys.argv) < 2:
    output = remove_ext(sys.argv[1]) + ".cmake.in"
else:
    output = sys.argv[2]
with open(input, 'rt') as inf:
    with open(output, 'wt+') as outf:
        inf.seek(0, os.SEEK_END)
        end = inf.tell()
        inf.seek(0, os.SEEK_SET)
        while inf.readable():
            if inf.tell() >= end:
                break
            if inf.closed:
                break
            line = inf.readline()
            if line.startswith("#"):
                tmp = line.removeprefix("#")
                tmp = tmp.lstrip(" ")
                if tmp.startswith("mesondefine"):
                    line = "#cmakedefine" + tmp.removeprefix("mesondefine")
            line = line.strip("\n\r")
            outf.write(line + "\n")