// File: corecodec[P]coremake/coremake.c
// Function: main
int main(int argc, char** argv)
{
	int n;
	int dumppost = 0;
	int dump = 0;
	item* i;
	char path[MAX_PATH];
	char root_forced = 0;

	item* root = item_get(NULL,"ROOT",0);
	getconfig(root);

    getcwd(proj_root,sizeof(proj_root));
    pathunix(proj_root);
    addendpath(proj_root);
    proj_root_len = strlen(proj_root);

	for (n=1;n<argc;++n)
	{
		if (argv[n][0]=='-')
		{
			switch (argv[n][1])
			{
			case 'v':
				verbose = 1;
				break;
			case 'h':
				usage();
				return 0;
			case 'd':
				dump = 1;
				break;
			case 'p':
				dumppost = 1;
				break;
			case 'f':
				strcpy(proj,argv[++n]);
				root_forced = 1;
				break;
            case 'r':
                strcpy(src_root,argv[++n]);
                pathunix(src_root);
                if (!ispathabs(src_root))
                {
                    strins(src_root,proj_root,NULL);
                    simplifypath(src_root,0);
                }
                addendpath(src_root);
                src_root_len = strlen(src_root);
                break;
			}
		}
		else
		if (platform[0]==0)
			strcpy(platform,argv[n]);
	}

	strcpy(path,src_root);
    addendpath(path);
	strcat(path,proj);

    if (!src_root_len)
    {
        strcpy(src_root, proj_root);
        src_root_len = proj_root_len;
    }

    if (!load_file(root,path,FLAG_PATH_SOURCE,NULL) && !root_forced)
	{
        // try <directory>.proj
		strcpy(proj,src_root);
        delendpath(proj);
        truncfilename(proj);
		strcat(proj,".proj");
        strins(proj,src_root,NULL);
		if (!load_file(root,proj,FLAG_PATH_SOURCE,NULL))
        {
            // search for *.proj
			DIR* dir = opendir(src_root);
			if (dir)
			{
                int found = 0;
				struct dirent* entry;
				while ((entry = readdir(dir)) != NULL)
					if (entry->d_name[0]!='.' && strrchr(entry->d_name,'.') &&
                        stricmp(strrchr(entry->d_name,'.'),".proj")==0 && load_file(root,entry->d_name,FLAG_PATH_SOURCE,NULL))
                    {
                        found = 1;
                        strcpy(proj,entry->d_name);
                        pathunix(proj);
                        truncfilename(proj);
                        break;
                    }

				closedir(dir);
                if (!found)
                {
                    printf("No root .proj file found !");
                    exit(1);
                }
			}
        }
	}

    if (!getvalue(item_find(root,"workspace")))
    {
        // add default workspace
        item* w = item_get(root,"workspace",0);
        item* i = NULL;
        i=default_workspace(w,i,item_find(root,"exe"));
        i=default_workspace(w,i,item_find(root,"dll"));
        i=default_workspace(w,i,item_find(root,"con"));
        i=default_workspace(w,i,item_find(root,"exe_android"));
    }

	preprocess_project(item_find(root,"project"));

	if (dump)
		dumpitem(root,1);

	if (!platform[0])
	{
		if (!dump)
			usage();
		return 1;
	}

	item_get(item_get(root,"platformname",1),platform,1);

    i = item_get(root,"rootpath",1);
	i = item_get(i,src_root,1);
    set_path_type(i,FLAG_PATH_SOURCE);

    i = item_get(root,"builddir",1);
	i = item_get(i,proj_root,1);
    set_path_type(i,FLAG_PATH_GENERATED);

	sprintf(path,"%srelease/%s/",proj_root,platform);
    i = item_get(root,"outputpath",1);
	i = item_get(i,path,1);
    set_path_type(i,FLAG_PATH_GENERATED);

	sprintf(path,"%sbuild/%s/",proj_root,platform);
    i = item_get(root,"buildpath",1);
	i = item_get(i,path,1);
    set_path_type(i,FLAG_PATH_GENERATED);

	i = getvalue(getroot(root,"platform_files"));
	if (i)
	{
		if (ispathabs(i->value) || !ispathabs(src_root))
			strcpy(coremake_root,i->value);
		else
		{
			strcpy(coremake_root,src_root);
			strcat(coremake_root,i->value);
		}
		simplifypath(coremake_root,1);
	}
	else
    {
#ifdef _WIN32
        HMODULE this1 = GetModuleHandleA("coremake.exe");
        if (this1)
        {
            GetModuleFileNameA(this1,coremake_root,MAX_PATH);
            FreeLibrary(this1);
            pathunix(coremake_root);
            getfilename(coremake_root)[0] = 0;
            strcat(coremake_root,"coremake");
        }
#else
        /// \todo this shouldn't work on UNIX platforms
        ///       since the platform files should be in a dir like /usr/share
        ///       and the argv most likely doesn't contain the path to the exe
        ///       We probably need a --prefix during compilation to hardcode that /usr/share path
        strcpy(coremake_root,"/usr/local/share/coremake");
#endif
        i = item_get(root,"platform_files",1);
        i = item_get(i,coremake_root,1);
        set_path_type(i,FLAG_PATH_COREMAKE);
        strcpy(coremake_root,i->value);
    }
	addendpath(coremake_root);
    coremake_root_len = strlen(coremake_root);

    strcpy(path,coremake_root);
	strcat(path,platform);
	strcat(path,".build");

	strcpy(buildpath[0],proj_root); //safety
    buildflags[0] = FLAG_PATH_GENERATED;
	build = NULL;
	build_file(root,path,FLAG_PATH_COREMAKE);
	if (build)
	{
		fclose(build);
		file_finalize(buildpath[curr_build]);
	}

	if (dumppost)
		dumpitem(root,1);

	item_delete(root);
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x4b0, %rsp            # imm = 0x4B0
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x0, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movb $0x0, -0x431(%rbp)
xorl %eax, %eax
movl %eax, %edi
leaq 0x2733(%rip), %rsi      # 0x175bc
xorl %edx, %edx
callq 0x28d0
movq %rax, -0x440(%rbp)
movq -0x440(%rbp), %rdi
callq 0x2960
leaq 0x5b46(%rip), %rdi      # 0x1a9f0
movl $0x400, %esi            # imm = 0x400
callq 0x2170
leaq 0x5b35(%rip), %rdi      # 0x1a9f0
callq 0x4e10
leaq 0x5b29(%rip), %rdi      # 0x1a9f0
callq 0x4c90
leaq 0x5b1d(%rip), %rdi      # 0x1a9f0
callq 0x2100
movq %rax, 0x6319(%rip)      # 0x1b1f8
movl $0x1, -0x14(%rbp)
movl -0x14(%rbp), %eax
cmpl -0x8(%rbp), %eax
jge 0x15063
movq -0x10(%rbp), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbl (%rax), %eax
cmpl $0x2d, %eax
jne 0x1502d
movq -0x10(%rbp), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbl 0x1(%rax), %eax
addl $-0x64, %eax
movl %eax, %ecx
movq %rcx, -0x470(%rbp)
subl $0x12, %eax
ja 0x1502b
movq -0x470(%rbp), %rax
leaq 0x1107(%rip), %rcx      # 0x16044
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0x1, 0x5690(%rip)      # 0x1a5e0
jmp 0x1502b
callq 0x7910
movl $0x0, -0x4(%rbp)
jmp 0x157e5
movl $0x1, -0x1c(%rbp)
jmp 0x1502b
movl $0x1, -0x18(%rbp)
jmp 0x1502b
movq -0x10(%rbp), %rax
movl -0x14(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, -0x14(%rbp)
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rsi
leaq 0x5227(%rip), %rdi      # 0x1a1c0
callq 0x2090
movb $0x1, -0x431(%rbp)
jmp 0x1502b
movq -0x10(%rbp), %rax
movl -0x14(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, -0x14(%rbp)
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rsi
leaq 0x562b(%rip), %rdi      # 0x1a5f0
callq 0x2090
leaq 0x561f(%rip), %rdi      # 0x1a5f0
callq 0x4e10
leaq 0x5613(%rip), %rdi      # 0x1a5f0
callq 0x50d0
cmpl $0x0, %eax
jne 0x1500c
leaq 0x5602(%rip), %rdi      # 0x1a5f0
leaq 0x59fb(%rip), %rsi      # 0x1a9f0
xorl %eax, %eax
movl %eax, %edx
callq 0x3f50
leaq 0x55eb(%rip), %rdi      # 0x1a5f0
xorl %esi, %esi
callq 0x5490
leaq 0x55dd(%rip), %rdi      # 0x1a5f0
callq 0x4c90
leaq 0x55d1(%rip), %rdi      # 0x1a5f0
callq 0x2100
movq %rax, 0x61c5(%rip)      # 0x1b1f0
jmp 0x15053
movsbl 0x61dc(%rip), %eax      # 0x1b210
cmpl $0x0, %eax
jne 0x15051
movq -0x10(%rbp), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
leaq 0x61c4(%rip), %rdi      # 0x1b210
callq 0x2090
jmp 0x15053
jmp 0x15055
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x14ee6
leaq -0x430(%rbp), %rdi
leaq 0x557f(%rip), %rsi      # 0x1a5f0
callq 0x2090
leaq -0x430(%rbp), %rdi
callq 0x4c90
leaq -0x430(%rbp), %rdi
leaq 0x5130(%rip), %rsi      # 0x1a1c0
callq 0x22f0
cmpq $0x0, 0x6153(%rip)      # 0x1b1f0
jne 0x150c0
leaq 0x554a(%rip), %rdi      # 0x1a5f0
leaq 0x5943(%rip), %rsi      # 0x1a9f0
callq 0x2090
movq 0x613f(%rip), %rax      # 0x1b1f8
movq %rax, 0x6130(%rip)      # 0x1b1f0
movq -0x440(%rbp), %rdi
leaq -0x430(%rbp), %rsi
movl $0x1, %edx
xorl %eax, %eax
movl %eax, %ecx
callq 0x7510
cmpl $0x0, %eax
jne 0x152a8
cmpb $0x0, -0x431(%rbp)
jne 0x152a8
leaq 0x50c7(%rip), %rdi      # 0x1a1c0
leaq 0x54f0(%rip), %rsi      # 0x1a5f0
callq 0x2090
leaq 0x50b4(%rip), %rdi      # 0x1a1c0
callq 0x4cf0
leaq 0x50a8(%rip), %rdi      # 0x1a1c0
callq 0x4a10
leaq 0x509c(%rip), %rdi      # 0x1a1c0
leaq 0x119a(%rip), %rsi      # 0x162c5
callq 0x22f0
leaq 0x5089(%rip), %rdi      # 0x1a1c0
leaq 0x54b2(%rip), %rsi      # 0x1a5f0
xorl %eax, %eax
movl %eax, %edx
callq 0x3f50
movq -0x440(%rbp), %rdi
leaq 0x506b(%rip), %rsi      # 0x1a1c0
movl $0x1, %edx
xorl %eax, %eax
movl %eax, %ecx
callq 0x7510
cmpl $0x0, %eax
jne 0x152a6
leaq 0x547d(%rip), %rdi      # 0x1a5f0
callq 0x20e0
movq %rax, -0x448(%rbp)
cmpq $0x0, -0x448(%rbp)
je 0x152a4
movl $0x0, -0x44c(%rbp)
movq -0x448(%rbp), %rdi
callq 0x2230
movq %rax, -0x458(%rbp)
cmpq $0x0, %rax
je 0x15275
movq -0x458(%rbp), %rax
movsbl 0x13(%rax), %eax
cmpl $0x2e, %eax
je 0x15270
movq -0x458(%rbp), %rdi
addq $0x13, %rdi
movl $0x2e, %esi
callq 0x2130
cmpq $0x0, %rax
je 0x15270
movq -0x458(%rbp), %rdi
addq $0x13, %rdi
movl $0x2e, %esi
callq 0x2130
movq %rax, %rdi
leaq 0x10bf(%rip), %rsi      # 0x162c5
callq 0x2050
cmpl $0x0, %eax
jne 0x15270
movq -0x440(%rbp), %rdi
movq -0x458(%rbp), %rsi
addq $0x13, %rsi
movl $0x1, %edx
xorl %eax, %eax
movl %eax, %ecx
callq 0x7510
cmpl $0x0, %eax
je 0x15270
movl $0x1, -0x44c(%rbp)
movq -0x458(%rbp), %rsi
addq $0x13, %rsi
leaq 0x4f6f(%rip), %rdi      # 0x1a1c0
callq 0x2090
leaq 0x4f63(%rip), %rdi      # 0x1a1c0
callq 0x4e10
leaq 0x4f57(%rip), %rdi      # 0x1a1c0
callq 0x4a10
jmp 0x15275
jmp 0x15197
movq -0x448(%rbp), %rdi
callq 0x2180
cmpl $0x0, -0x44c(%rbp)
jne 0x152a2
leaq 0x2330(%rip), %rdi      # 0x175c1
movb $0x0, %al
callq 0x2120
movl $0x1, %edi
callq 0x2310
jmp 0x152a4
jmp 0x152a6
jmp 0x152a8
movq -0x440(%rbp), %rdi
leaq 0x1037(%rip), %rsi      # 0x162ed
callq 0x27d0
movq %rax, %rdi
callq 0x2990
cmpq $0x0, %rax
jne 0x15424
movq -0x440(%rbp), %rdi
leaq 0x1012(%rip), %rsi      # 0x162ed
xorl %edx, %edx
callq 0x28d0
movq %rax, -0x460(%rbp)
movq $0x0, -0x468(%rbp)
movq -0x460(%rbp), %rax
movq %rax, -0x4b0(%rbp)
movq -0x468(%rbp), %rax
movq %rax, -0x4a8(%rbp)
movq -0x440(%rbp), %rdi
leaq 0x1f14(%rip), %rsi      # 0x17232
callq 0x27d0
movq -0x4b0(%rbp), %rdi
movq -0x4a8(%rbp), %rsi
movq %rax, %rdx
callq 0x14d90
movq %rax, -0x468(%rbp)
movq -0x460(%rbp), %rax
movq %rax, -0x4a0(%rbp)
movq -0x468(%rbp), %rax
movq %rax, -0x498(%rbp)
movq -0x440(%rbp), %rdi
leaq 0x1f27(%rip), %rsi      # 0x17291
callq 0x27d0
movq -0x4a0(%rbp), %rdi
movq -0x498(%rbp), %rsi
movq %rax, %rdx
callq 0x14d90
movq %rax, -0x468(%rbp)
movq -0x460(%rbp), %rax
movq %rax, -0x490(%rbp)
movq -0x468(%rbp), %rax
movq %rax, -0x488(%rbp)
movq -0x440(%rbp), %rdi
leaq 0x1ed0(%rip), %rsi      # 0x17286
callq 0x27d0
movq -0x490(%rbp), %rdi
movq -0x488(%rbp), %rsi
movq %rax, %rdx
callq 0x14d90
movq %rax, -0x468(%rbp)
movq -0x460(%rbp), %rax
movq %rax, -0x480(%rbp)
movq -0x468(%rbp), %rax
movq %rax, -0x478(%rbp)
movq -0x440(%rbp), %rdi
leaq 0xd5e(%rip), %rsi       # 0x16160
callq 0x27d0
movq -0x480(%rbp), %rdi
movq -0x478(%rbp), %rsi
movq %rax, %rdx
callq 0x14d90
movq %rax, -0x468(%rbp)
movq -0x440(%rbp), %rdi
leaq 0x12ad(%rip), %rsi      # 0x166df
callq 0x27d0
movq %rax, %rdi
callq 0xe230
cmpl $0x0, -0x1c(%rbp)
je 0x15456
movq -0x440(%rbp), %rdi
movl $0x1, %esi
callq 0x7840
cmpb $0x0, 0x5db3(%rip)      # 0x1b210
jne 0x15476
cmpl $0x0, -0x1c(%rbp)
jne 0x1546a
callq 0x7910
movl $0x1, -0x4(%rbp)
jmp 0x157e5
movq -0x440(%rbp), %rdi
leaq 0x2158(%rip), %rsi      # 0x175dc
movl $0x1, %edx
callq 0x28d0
movq %rax, %rdi
leaq 0x5d78(%rip), %rsi      # 0x1b210
movl $0x1, %edx
callq 0x28d0
movq -0x440(%rbp), %rdi
leaq 0x2139(%rip), %rsi      # 0x175e9
movl $0x1, %edx
callq 0x28d0
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
leaq 0x5127(%rip), %rsi      # 0x1a5f0
movl $0x1, %edx
callq 0x28d0
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
movl $0x1, %esi
callq 0x2570
movq -0x440(%rbp), %rdi
leaq 0x20ff(%rip), %rsi      # 0x175f2
movl $0x1, %edx
callq 0x28d0
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
leaq 0x54e4(%rip), %rsi      # 0x1a9f0
movl $0x1, %edx
callq 0x28d0
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
movl $0x2, %esi
callq 0x2570
leaq -0x430(%rbp), %rdi
leaq 0x20c5(%rip), %rsi      # 0x175fb
leaq 0x54b3(%rip), %rdx      # 0x1a9f0
leaq 0x5ccc(%rip), %rcx      # 0x1b210
movb $0x0, %al
callq 0x2300
movq -0x440(%rbp), %rdi
leaq 0x1238(%rip), %rsi      # 0x16791
movl $0x1, %edx
callq 0x28d0
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
leaq -0x430(%rbp), %rsi
movl $0x1, %edx
callq 0x28d0
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
movl $0x2, %esi
callq 0x2570
leaq -0x430(%rbp), %rdi
leaq 0x206d(%rip), %rsi      # 0x17609
leaq 0x544d(%rip), %rdx      # 0x1a9f0
leaq 0x5c66(%rip), %rcx      # 0x1b210
movb $0x0, %al
callq 0x2300
movq -0x440(%rbp), %rdi
leaq 0x2056(%rip), %rsi      # 0x17615
movl $0x1, %edx
callq 0x28d0
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
leaq -0x430(%rbp), %rsi
movl $0x1, %edx
callq 0x28d0
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
movl $0x2, %esi
callq 0x2570
movq -0x440(%rbp), %rdi
leaq 0xd2f(%rip), %rsi       # 0x16331
callq 0x2890
movq %rax, %rdi
callq 0x2990
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0x1568a
movq -0x28(%rbp), %rax
movq (%rax), %rdi
callq 0x50d0
cmpl $0x0, %eax
jne 0x1563c
leaq 0x4fbe(%rip), %rdi      # 0x1a5f0
callq 0x50d0
cmpl $0x0, %eax
jne 0x15651
movq -0x28(%rbp), %rax
movq (%rax), %rsi
leaq 0x57a6(%rip), %rdi      # 0x1adf0
callq 0x2090
jmp 0x15677
leaq 0x5798(%rip), %rdi      # 0x1adf0
leaq 0x4f91(%rip), %rsi      # 0x1a5f0
callq 0x2090
movq -0x28(%rbp), %rax
movq (%rax), %rsi
leaq 0x577e(%rip), %rdi      # 0x1adf0
callq 0x22f0
leaq 0x5772(%rip), %rdi      # 0x1adf0
movl $0x1, %esi
callq 0x5490
jmp 0x156f3
leaq 0x575f(%rip), %rdi      # 0x1adf0
leaq 0x1f87(%rip), %rsi      # 0x1761f
callq 0x2090
movq -0x440(%rbp), %rdi
leaq 0xc86(%rip), %rsi       # 0x16331
movl $0x1, %edx
callq 0x28d0
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
leaq 0x572c(%rip), %rsi      # 0x1adf0
movl $0x1, %edx
callq 0x28d0
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
movl $0x3, %esi
callq 0x2570
movq -0x28(%rbp), %rax
movq (%rax), %rsi
leaq 0x5702(%rip), %rdi      # 0x1adf0
callq 0x2090
leaq 0x56f6(%rip), %rdi      # 0x1adf0
callq 0x4c90
leaq 0x56ea(%rip), %rdi      # 0x1adf0
callq 0x2100
movq %rax, 0x5aee(%rip)      # 0x1b200
leaq -0x430(%rbp), %rdi
leaq 0x56d0(%rip), %rsi      # 0x1adf0
callq 0x2090
leaq -0x430(%rbp), %rdi
leaq 0x5add(%rip), %rsi      # 0x1b210
callq 0x22f0
leaq -0x430(%rbp), %rdi
leaq 0x1ef3(%rip), %rsi      # 0x17639
callq 0x22f0
leaq 0x5ece(%rip), %rdi      # 0x1b620
leaq 0x5297(%rip), %rsi      # 0x1a9f0
callq 0x2090
movl $0x2, 0x7eb8(%rip)      # 0x1d620
movq $0x0, 0x7ecd(%rip) # 0x1d640
movq -0x440(%rbp), %rdi
leaq -0x430(%rbp), %rsi
movl $0x3, %edx
callq 0x14ce0
cmpq $0x0, 0x7ead(%rip)      # 0x1d640
je 0x157bb
movq 0x7ea4(%rip), %rdi      # 0x1d640
callq 0x20d0
movslq 0x5e68(%rip), %rax      # 0x1b610
leaq 0x5e71(%rip), %rdi      # 0x1b620
shlq $0xa, %rax
addq %rax, %rdi
callq 0x7e90
cmpl $0x0, -0x18(%rbp)
je 0x157d2
movq -0x440(%rbp), %rdi
movl $0x1, %esi
callq 0x7840
movq -0x440(%rbp), %rdi
callq 0x3150
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x4b0, %rsp            # imm = 0x4B0
popq %rbp
retq