#!/bin/bash
# $1 scripts folder
# $2 file to generate OUTPUTH C
# $3 file to generate OUTPUTH H
# $4 prefix

generate() {
	local LUAFILES="$1/*"
	local OUTPUTH="$2"
	local OUTPUTC="$3"
	local PREFIX="$4"

	local outname="$(basename "$OUTPUTH")"
	outname="${outname%.*}"
	
	local guard=$(echo "$outname" | tr '[:lower:]' '[:upper:]')
	guard="$guard""_H"

	

	for file in $LUAFILES
	do
		filename="$(basename "$file")"
        if [ -d "$file" ]
		then
			generate $file $2 $3 ${PREFIX}${filename}_
		else
			filename="${filename%.*}"
			echo "extern char $PREFIX$filename[];"    >> "$OUTPUTH"


			echo "char $PREFIX$filename[] = {"        >> "$OUTPUTC"
			echo "    " $(xxd -i < "$file") ",0x00"   >> "$OUTPUTC"
			echo "};"                                 >> "$OUTPUTC"
		fi
	done
}

OUTPUTH="$2"

outname="$(basename "$OUTPUTH")"
outname="${outname%.*}"
guard=$(echo "$outname" | tr '[:lower:]' '[:upper:]')
guard="$guard""_H"

printf "#ifndef %s\n" "$guard"            >> "$OUTPUTH"
printf "#define %s\n" "$guard"            >> "$OUTPUTH"

generate "$1" "$2" "$3" "$4"

echo "#endif"                             >> "$OUTPUTH"
exit 0