#!/bin/bash

debug_trap_handler_file=$2

cat > ${debug_trap_handler_file} <<EOF
//==============================================================================
//  This file is automatically generated during build process, don't modify it
//==============================================================================

EOF

filename=$(basename -- "$1")
filename="${filename%.*}"

# dump the numeric representation of the code objects
od --address-radix=x -v -w24 -t x $1 > ${filename}.od

# create file contains all the code object blobs
awk -v gfx="$3" \
    'BEGIN { printf "uint32_t HSATrapHandler_s_%s_co[] = {\n", gfx }
    {
      if (NF > 1) {
        printf "    ";
        for (i = 2; i <= NF; i++)
          printf "0x%s,", toupper($i);
        printf "\n"
      }
    }
    END { printf "};\n\n" }'  \
    ${filename}.od >> ${debug_trap_handler_file}

# remove the unwanted file
rm -f ${filename}.od
