#include "str.h"
#include "types.h"
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

void cmake(char *dep, char *libpath, char *lib, char *cmake_dir, char *params);
bool file_exists(char *dir);
void sys(char *cmd);

int main(int argc, char *argv[]) {
	printf("SkunkWorks build system v0.0\n");
	fflush(stdout);
	sys("if not exist build md build");
	chdir("build");
	sys("if not exist deps md deps");
	cmake("glfw-3.3.8", "src\\Release\\glfw3.lib", "glfw3.lib", "", "");
	cmake("zlib-1.2.13", "Release\\zlibstatic.lib", "zlib.lib", "", "");
	cmake(
	    "lpng1639",
	    "Release\\libpng16_static.lib",
	    "png.lib",
	    "",
	    "-D ZLIB_INCLUDE_DIR=../../subprojects/zlib-1.2.13 "
	    "-D ZLIB_LIBRARY=.. "
	    "-D CMAKE_C_FLAGS=/I\"../zlib-1.2.13\""
	);
	cmake(
	    "glew-2.1.0",
	    "lib\\Release\\libglew32.lib",
	    "glew.lib",
	    "build/cmake",
	    ""
	);

	sys("cl /c /MD /O2 "
	    "../src/*.c "
	    "../src/net/*.c "
	    "/I../include "
	    "/I../subprojects/glew-2.1.0/include "
	    "/I../subprojects/glfw-3.3.8/include "
	    "/I../subprojects/lpng1639 "
	    "/I../subprojects/zlib-1.2.13 "
	    "/Ilpng1639 ");
	sys("del skunkworks.lib ");
	sys("lib "
	    "*.obj "
	    "deps/*.lib "
	    "/OUT:skunkworks.lib");

	sys("cl /MD /O2 "
	    "../tools/skip.c "
	    "skunkworks.lib "
	    "/I../include ");

	/* example */
	sys("cl /MD /O2 "
	    "../examples/simple.c "
	    "skunkworks.lib "
	    "/I../include "
	    "/I. ");

	/* gsa example */
	/*
	sys("c:\\dev\\_tools\\dxc\\bin\\x64\\dxc.exe "
	    "-fvk-use-gl-layout "
	    "-T vs_6_0 -spirv ..\\src\\gsa_vert.hlsl -Fo gsa_vert.spv");
	sys("c:\\dev\\_tools\\dxc\\bin\\x64\\dxc.exe "
	    "-fvk-use-gl-layout "
	    "-T ps_6_0 -spirv ..\\src\\gsa_frag.hlsl -Fo gsa_frag.spv");
	*/
	/*
	sys("C:\\dev\\_tools\\gslang\\bin\\glslangValidator.exe "
	    "-G -S vert -o gsa_vert.spv ..\\src\\gsa_vert.glsl");
	sys("C:\\dev\\_tools\\gslang\\bin\\glslangValidator.exe "
	    "-G -S frag -o gsa_frag.spv ..\\src\\gsa_frag.glsl");
	*/
	/* sys("copy *.spv ..\\example_data"); */
	sys("copy ..\\src\\*.glsl ..\\example_data");
	sys("cl /MD /O2 "
	    "../examples/gsa_simple.c "
	    "skunkworks.lib "
	    "/I../include "
	    "/I. ");
	sys("skip ca1 ../example_data gsa_simple.exe");

	sys("del *.obj");

	return 0;
}

void cmake(char *dep, char *libpath, char *lib, char *cmake_dir, char *params) {
	if(file_exists(sw_str_concat("deps\\", lib))) {
		printf("%s already exists, not compiling dependency\n", lib);
		return;
	}

	printf("compiling %s", lib);

	sys(sw_str_concat("md ", dep));
	chdir(dep);
	sys(sw_str_concat4(
	    "cmake ",
	    params,
	    " ../../subprojects/",
	    sw_str_concat3(dep, "/", cmake_dir)
	));
	sys("cmake --build . --config Release");
	chdir("..");
	sys(sw_str_concat4(
	    sw_str_concat3("copy ", dep, "\\"), libpath, " deps\\", lib
	));
}

bool file_exists(char *dir) {
	DWORD attrib = GetFileAttributes(dir);

	return attrib != INVALID_FILE_ATTRIBUTES;
}

void sys(char *cmd) {
	if(system(cmd) != 0) {
		fflush(stdout);
		exit(-1);
	}
}
