#!/bin/bash

#use this tool to generate images  https://github.com/khalim19/gimp-plugin-export-layers
pushd assets

echo "generate assets.cpp"

cat > assets.cpp << EOF
#include "assets.h"

const char* ImagePath(int symbol)
{
	switch(symbol)
	{
EOF

ls *.bmp -1 | sed 's/\([^.]*\).bmp/		case \1:	return "assets\/\0";/g' >> assets.cpp

cat >> assets.cpp << EOF
	}
	return "";
}

EOF

echo "generate assets.h"

cat > assets.h << EOF
#ifndef ASSETS
#define ASSETS


EOF

ls *.xbm -1 | sed 's/\([^.]*\).xbm/#include "\1.xbm"/g' >> assets.h

cat >> assets.h << EOF

enum _IMAGES
{
EOF

ls *.bmp -1 | sed 's/\([^.]*\).bmp/	\1,/g' >> assets.h

cat >> assets.h << EOF
	SumImages
};

const char* ImagePath(int);
#endif
EOF

popd


echo "to cerealize"

cat > cereal/cereal.h << EOF
#ifndef CEREAL
#define CEREAL

#include "../game.h"
EOF




cat >> cereal/cereal.h << EOF
void CerealizeGame(FILE * stream, Db cerealizable)
{
EOF

awk '/public:/,/}/' game.h | grep '[^ ]\+ [^;(]\+;' | sed 's/\([^ \t]\+\) \([^;(]\+\);/\tfprintf(stream, "\2:%i;\\n", cerealizable.\2);/g' >> cereal/cereal.h

cat >> cereal/cereal.h << EOF
}

Db DecerealizeGame(FILE * stream)
{
	const char * _int = "%i";
	const char * _char = "%s";
	Db cerealizable;
EOF

awk '/public:/,/}/' game.h | grep 'int [^;(]\+;' | sed 's/int \([A-Za-z0-9]*\);/\twhile(1==fscanf(stream, "\1:%i;", \&cerealizable.\1)){}/g' >> cereal/cereal.h

cat >> cereal/cereal.h << EOF
	return cerealizable;
}
EOF


cat >> cereal/cereal.h << EOF

#endif

EOF

./build.sh
