# update the files platform_version.h and platform_version.cpp in platform/
import argparse
import subprocess
import platform
import os

parser = argparse.ArgumentParser()
parser.add_argument('--host_prefix', required=True, help='Host platform prefix')
parser.add_argument('--target_prefix', required=True, help='Target platform prefix')
args = vars(parser.parse_args())

# get current version from the only recognized version source (/version.txt)
with open('../version.txt', encoding='utf-8') as f:
	version_string = f.read().strip()

# retrieve the current commit SHA
if platform.system() == "Windows":
	sha = subprocess.check_output(["git", "rev-parse", "HEAD"], shell=True).decode()
else:
	sha = subprocess.check_output(["git", "rev-parse", "HEAD"]).decode()
sha = ''.join(sha.split())

# output build_info.cpp
with open('build_info.cpp', 'w', encoding='utf-8') as f:
	f.write('// This file is generated by the build system (gen_build_info_cpp.py).\n')
	f.write('\n')
	f.write('#include "foundation/build_info.h"\n')
	f.write('\n')
	f.write('#define VERSION "%s"\n' % version_string)
	f.write('#define BUILD_SHA "%s"\n' % sha)
	f.write('#define HG_HOST_PREFIX "%s"\n' % args['host_prefix'])
	f.write('#define HG_TARGET_PREFIX "%s"\n' % args['target_prefix'])
	f.write('\n')
	f.write('namespace hg {\n')
	f.write('\n')
	f.write("const char *get_version_string() { return VERSION; }\n")
	f.write("const char *get_build_sha() { return BUILD_SHA; }\n")
	f.write('\n')
	f.write("const char *get_host_string() { return HG_HOST_PREFIX; }\n")
	f.write("const char *get_target_string() { return HG_TARGET_PREFIX; }\n")
	f.write('\n')
	f.write('} // namespace hg\n')
