################################################################################
##
## Copyright (c) Advanced Micro Devices, Inc., or its affiliates. 
## 
## SPDX-License-Identifier: MIT
##
################################################################################
import sys

def GetSize(fileobject):
    fileobject.seek(0,2) # move the cursor to the end of the file
    size = fileobject.tell()
    return size

def DumpFile(header, input_name):
  try:
    with open(input_name, "rb") as binary_file:
        # Read the entire content of the file as bytes
        binary_data = binary_file.read()
        file_size = GetSize(binary_file)
        #print(f"Binary size: {file_size}")
        # Reset file pointer
        binary_file.seek(0)
        parts = input_name.split('.')
        file_name = parts[0]
        content = f"unsigned char {file_name}""[] = {\n  "

        header.write(content)
        line = 0
        count = 0
        for byte_value in binary_data:
          count += 1
          padded_hex = '{:02x}'.format(byte_value)
          if (count != file_size):
            header.write(f"0x{padded_hex},")
          else:
            header.write(f"0x{padded_hex}")
          line += 1
          if (line == 12):
            header.write(f"\n  ")
            line = 0
          else:
            header.write(f" ")

        header.write("\n};\nunsigned int "f"{file_name}_len = {file_size};\n")

  except FileNotFoundError:
    print(f"Error: The file {input_name} was not found.")
  except Exception as e:
    print(f"An error occurred: {e}")


if len(sys.argv) > 1:
  header_name = sys.argv[1];
  with open(header_name, 'w') as header:
    header.write("//==============================================================================\n")
    header.write("//  This file is automatically generated during build process, don't modify it\n")
    header.write("//==============================================================================\n\n")
    header.write("namespace rocr {\n")
    header.write("namespace AMD {\n\n")

    for i, arg in enumerate(sys.argv):
      if (i > 1):
        #print(f"File {i}: {arg}\n")
        DumpFile(header, arg)
    header.write("} // namespace AMD\n")
    header.write("} // namespace rocr\n\n")

else:
  print("Empty arguments!")
  