#!/bin/bash

# Fixes apple bash not loading it's profile when run from xcode
source ~/.bash_profile

# Fixes apple bash not using the sed alias when run from cmake
SED_CMD=sed
if [[ "$(uname)" == "Darwin" ]]; then
  SED_CMD=gsed
fi

SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
GENERATED_DATA_PATH="$1"
SHADER_PATH="$2"
UNIQUE_ENTRY_NAME="$3"

HEADER_PATH="$1"/"GeneratedShaderData_${3}.h"
C_PATH="$1"/"GeneratedShaderData_${3}.c"
HEADER_GUARD="GeneratedShaderData_${UNIQUE_ENTRY_NAME}_H_INCLUDED"

if [[ "$OSTYPE" == "msys" || "$OSTYPE" == "cygwin" ]]; then
  SCRIPT_DIR=$(cygpath -w "$SCRIPT_DIR")
  GENERATED_DATA_PATH=$(cygpath -w "$GENERATED_DATA_PATH")
  SHADER_PATH=$(cygpath -w "$SHADER_PATH")
  HEADER_PATH=$(cygpath -w "$HEADER_PATH")
  C_PATH=$(cygpath -w "$C_PATH")
fi

check_return_code() {
  # return code of last executed command  
  retVal=$?
  if [ $retVal -ne 0 ]; then
    exit 1
  fi
}

mkdir -p "$GENERATED_DATA_PATH"
rm -f "$GENERATED_DATA_PATH/*.spv"
touch "$HEADER_PATH"
touch "$C_PATH"

echo "// Auto generated file" > "$HEADER_PATH"
echo -e "#ifndef ${HEADER_GUARD}\n#define ${HEADER_GUARD}" >> "$HEADER_PATH"
echo -e "#include \"GeneratedShaderData_${UNIQUE_ENTRY_NAME}.h\"" > "$C_PATH"

# put data from all files with same name into single file
unique_file_names=($(find $SHADER_PATH/ -type f ! -name '*.hpp' ! -name '*.cpp' | awk -F/ '{print $NF}' | awk -F. '{print $1}' | sort | uniq))
len=${#unique_file_names[@]}
i=0
entriesCountDef="\nconst unsigned int* entriesCount${UNIQUE_ENTRY_NAME}[${len}] = {"
entriesDef="const void* entries${UNIQUE_ENTRY_NAME}[${len}] = {"
entriesCountDecl="extern const unsigned int* entriesCount${UNIQUE_ENTRY_NAME}[${len}];"
entriesDecl="extern const void* entries${UNIQUE_ENTRY_NAME}[${len}];"
for unique_name in ${unique_file_names[@]} ; do
    same_files=$(find $SHADER_PATH/ -type f -name "${unique_name}.*")
    for same_file in ${same_files[@]} ; do
        filename_with_ext_no_path=$(basename -- "$same_file")
        glslangValidator -V $same_file -o "$GENERATED_DATA_PATH/$filename_with_ext_no_path.spv"
        check_return_code
    done

    (cd "$GENERATED_DATA_PATH" && bin2c -t -d $unique_name.h -o $unique_name.c *.spv)
    check_return_code
    rm "$GENERATED_DATA_PATH"/*.spv

    echo -e "#include \"${unique_name}.h\"" >> "$C_PATH"
    filename_no_ext_first_upper=$(echo $unique_name | awk '{print toupper(substr($0,1,1))substr($0,2)}')
    entriesCountDef+="&fileTable${filename_no_ext_first_upper}Size"
    entriesDef+="fileTable${filename_no_ext_first_upper}"

    if [ $i -ne $((len-1)) ]; then
        entriesCountDef+=",\n\t\t"
        entriesDef+=",\n\t\t"
    fi

    $SED_CMD -i "3s/.*/#include \"${unique_name}.h\"/" "$GENERATED_DATA_PATH/$unique_name.c"
    $SED_CMD -i 's/char \*entryName/const char *entryName/' "$GENERATED_DATA_PATH/$unique_name.h"
    $SED_CMD -i 's/const char \*data/const unsigned char *data/' "$GENERATED_DATA_PATH/$unique_name.h"
    # avoid ODR violation
    $SED_CMD -i "s/fileTable/fileTable${filename_no_ext_first_upper}/" "$GENERATED_DATA_PATH"/$unique_name.h $GENERATED_DATA_PATH/$unique_name.c
    $SED_CMD -i "s/typedef struct {/#ifndef TFileTableEntry_H_INCLUDED\n#define TFileTableEntry_H_INCLUDED\ntypedef struct {/" "$GENERATED_DATA_PATH/$unique_name.h"
    $SED_CMD -i "s/} TFileTableEntry;/} TFileTableEntry;\n#endif \/*TFileTableEntry_H_INCLUDED*\//" "$GENERATED_DATA_PATH/$unique_name.h"
    ((i++))
done

entriesCountDef+=" };"
entriesDef+=" };"
echo -e "${entriesCountDecl}\n${entriesDecl}\\n#endif /*${HEADER_GUARD}*/" >> "$HEADER_PATH"
echo -e "\n${entriesCountDef}\n${entriesDef}" >> "$C_PATH"
