#!/bin/bash -ex

#
# Kangaroo Punch MultiPlayer Game Server Mark II
# Copyright (C) 2020-2021  Scott Duensing
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
#


function createEmbeddedBinary() {
	local BINFILE=$1
	local BINEXTENSION=$2
	local TARGETDIR=$3
	local SOURCEFILE="$3/${1,,}.h"
	local BLOCKER="${1^^}_${2^^}_H"

	outputLicense > ${SOURCEFILE}
	outputHeader ${BLOCKER} >> ${SOURCEFILE}
	printf "\n#ifdef EMBED_HERE\n\n" >> ${SOURCEFILE}
	xxd -i ${BINFILE}.${BINEXTENSION} >> ${SOURCEFILE}
	printf "\n#else // EMBED_HERE\n\n" >> ${SOURCEFILE}
	printf "extern unsigned char ${BINFILE}_${BINEXTENSION}[];\n" >> ${SOURCEFILE}
	printf "extern unsigned int ${BINFILE}_${BINEXTENSION}_len;\n" >> ${SOURCEFILE}
	printf "\n#endif // EMBED_HERE\n\n" >> ${SOURCEFILE}
	outputFooter ${BLOCKER} >> ${SOURCEFILE}
}


function outputFooter() {
	local BLOCKER=$1

	printf "\n#pragma GCC diagnostic pop\n\n\n#endif // ${BLOCKER}\n"
}


function outputHeader() {
	local BLOCKER=$1

	printf "\n\n#ifndef ${BLOCKER}\n#define ${BLOCKER}\n\n\n"
	printf "// ===== THIS FILE IS AUTOMATICALLY GENERATED - DO NOT EDIT =====\n\n\n"
	printf "#pragma GCC diagnostic push\n#pragma GCC diagnostic ignored \"-Wunused-variable\"\n"
}


function outputLicense() {
	cat <<-LICENSE
		/*
		 * Kangaroo Punch MultiPlayer Game Server Mark II
		 * Copyright (C) 2020-2021  Scott Duensing
		 *
		 * This program is free software: you can redistribute it and/or modify
		 * it under the terms of the GNU General Public License as published by
		 * the Free Software Foundation, either version 3 of the License, or
		 * (at your option) any later version.
		 *
		 * This program is distributed in the hope that it will be useful,
		 * but WITHOUT ANY WARRANTY; without even the implied warranty of
		 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
		 * GNU General Public License for more details.
		 *
		 * You should have received a copy of the GNU General Public License
		 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
		 *
		 */
	LICENSE
}


pushd font/data
createEmbeddedBinary vga8x14 dat ../../client/src/embedded
popd

pushd client/data
createEmbeddedBinary mouse png ../../client/src/embedded
popd

