// File: /workspace/github_c_makefile_star2_O0/vlenhart[P]q3bsp/main.cpp
// Function: main
int  main(int argc,char **argv)
{

  VFormatOptions option;
  char *options=NULL;
  char *fileArg = NULL;
  
  int argi=1;	// the current argument 

  if (argc>=2 && argv[argi][0]== '-') { // we have options 
	  options = argv[argi];
	  argi++;
  }	

  if ( argi >= argc )
  {
    printf("Usage: q3bsp [-options] <name>.BSP\n");
    printf("Where <name> is the name of a valid Quake3 BSP file.\n\n");
    printf("This utility will convert a Quake3 BSP into a valid\n");
    printf("polygon mesh and output the results into two seperate VRML 1.0\n");
    printf("files.  The first VRML file contains all the U/V mapping and\n");
    printf("texture mapping information for channel #1, and the second\n");
    printf("VRML file will contain all of the U/V mapping and texture names\n");
    printf("for the second U/V channel, which contains all lightmap\n");
    printf("information.  You can then directly import these files into any\n");
    printf("number of 3d editing tools, including 3d Studio Max\n");

    printf("This tool also extracts the lightmap data and saves it out as a\n");
    printf("series of .BMP files.\n\n");
    printf("OpenSourced by John W. Ratcliff on December 5, 2000\n");
    printf("Merry Christmas!\n\n");
    printf("Contact Id Software about using Quake 3 data files in and\n");
    printf("Quake 3 editing tools for commercial software development\n");
    printf("projects.\n");
    printf("Extended Options: \n");
    printf("-1		VRML 1 output\n");
    printf("-2		VRML 2 output 2 files \n");
    printf("-2me	VRML 2 output with MultiTexture extension nodes & effects\n");
    exit(1);
  }

  fileArg = argv[argi];
  if (options) {
	  
	  if (strchr(options,'2'))
			option.vrml2 = true;
	  else if (strchr(options,'1'))
			option.vrml2 = false;
	  
	  if (strchr(options,'m'))
			option.useMultiTexturing = true;

	  if (strchr(options,'M'))
			option.useMat = true;
	  
	  if (strchr(options,'l'))
			option.useLighting = true;
	  
	  if (strchr(options,'b'))
			option.useBsp = true;

	  if (strchr(options,'e'))
			option.useEffects = true;

	  if (strchr(options,'v'))
			option.verbose = true;

	  if (strchr(options,'n'))
			option.noTextureCoordinates = true;

  }	
  
  Quake3BSP q( SGET(fileArg), SGET("a") );

  VertexMesh *mesh = q.GetVertexMesh();
  

  if ( mesh )
  {
    String str = fileArg;
	
	char * del = strrchr(fileArg,'\\');
	if (del) str = (del+1); // use file name part only
	
    String name1 = str + "1";
    String name2 = str + "2";


	if (option.vrml2) { // VRML 2 style 

		if (!option.useMultiTexturing) {
		    // save VRML file using channel #1
			// save VRML file using channel #2
			name1+=".wrl";

			FILE *fph = fopen(name1.c_str(),"wb");
			fprintf(fph,"#VRML V2.0 utf8 generated by QBSP from %s\n",fileArg);

			printf("Saving U/V channel #1 to VRML2 file %s\n",name1.c_str());
			option.tex1= true;
			mesh->SaveVRML2(fph,option);
			
			q.SaveEntitiesVRML2(fph,option);
			
			fclose(fph);


			printf("Saving U/V channel #2 to VRML2 file %s.wrl\n",name2.c_str());
			name2+=".wrl";
			fph = fopen(name2.c_str(),"wb");
			fprintf(fph,"#VRML V2.0 utf8 generated by QBSP from %s\n",fileArg);
			option.tex1=false;
			option.matDefined=false;
			option.useMat = false;
			mesh->SaveVRML2(fph,option);
			fclose(fph);

		} else {
		    name1 = str;
			printf("Saving MultiTexture extended VRML file %s.wrl\n",name1.c_str());

		    String oname = name1+".wrl";
			FILE *fph = fopen(oname.c_str(),"wb");
			fprintf(fph,"#VRML V2.0 utf8 generated by Q3BSP 1.1 from %s\n",fileArg);
			fprintf(fph,"WorldInfo { title \"%s\" }\n\n",fileArg);
			
			fprintf(fph,"NavigationInfo { headlight TRUE avatarSize [0.25 1.3333 2 ]} \n");

			// write some PROTO's for effects 
			if (option.useEffects)
			{
				Fload effects("q3effects.wrl");
				if (!effects.GetData()) {
					printf("***********q3effects.wrl NOT FOUND\n");
				}
				else {
					fprintf(fph,"%s\n",(char *)effects.GetData());
					option.matDefined = true; // assume MAT is defined in this file
				}
			}
  

			option.tex1= true;
			if (option.useBsp) 
				q.SaveNodesBsp(fph,option);
			else mesh->SaveVRML2(fph,option);
			
			q.SaveEntitiesVRML2(fph,option);

			fclose(fph);
		}

	} else {	// VRML 1 style 
		printf("Saving U/V channel #1 to file %s.wrl\n",name1.c_str());
		mesh->SaveVRML(name1,true);
		printf("Saving U/V channel #2 to file %s.wrl\n",name2.c_str());
		mesh->SaveVRML(name2,false);
	}
  }
  else
  {
    printf("Failed to crunch %s\n", fileArg);
	return -1;
  }

  return 0;
  
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x460, %rsp            # imm = 0x460
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
leaq -0xc8(%rbp), %rdi
callq 0x90b0
movq $0x0, -0xd0(%rbp)
movq $0x0, -0xd8(%rbp)
movl $0x1, -0xdc(%rbp)
cmpl $0x2, -0x8(%rbp)
jl 0x6bf7
movq -0x10(%rbp), %rax
movslq -0xdc(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbl (%rax), %eax
cmpl $0x2d, %eax
jne 0x6bf7
movq -0x10(%rbp), %rax
movslq -0xdc(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0xd0(%rbp)
movl -0xdc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xdc(%rbp)
movl -0xdc(%rbp), %eax
cmpl -0x8(%rbp), %eax
jl 0x6d77
leaq 0x2276e(%rip), %rdi     # 0x2937b
xorl %eax, %eax
callq 0x3060
jmp 0x6c16
leaq 0x22782(%rip), %rdi     # 0x2939f
xorl %eax, %eax
callq 0x3060
jmp 0x6c26
leaq 0x227a9(%rip), %rdi     # 0x293d6
xorl %eax, %eax
callq 0x3060
jmp 0x6c36
leaq 0x227ce(%rip), %rdi     # 0x2940b
xorl %eax, %eax
callq 0x3060
jmp 0x6c46
leaq 0x227fe(%rip), %rdi     # 0x2944b
xorl %eax, %eax
callq 0x3060
jmp 0x6c56
leaq 0x2282c(%rip), %rdi     # 0x29489
xorl %eax, %eax
callq 0x3060
jmp 0x6c66
leaq 0x22858(%rip), %rdi     # 0x294c5
xorl %eax, %eax
callq 0x3060
jmp 0x6c76
leaq 0x22889(%rip), %rdi     # 0x29506
xorl %eax, %eax
callq 0x3060
jmp 0x6c86
leaq 0x228b2(%rip), %rdi     # 0x2953f
xorl %eax, %eax
callq 0x3060
jmp 0x6c96
leaq 0x228e3(%rip), %rdi     # 0x29580
xorl %eax, %eax
callq 0x3060
jmp 0x6ca6
leaq 0x22908(%rip), %rdi     # 0x295b5
xorl %eax, %eax
callq 0x3060
jmp 0x6cb6
leaq 0x22939(%rip), %rdi     # 0x295f6
xorl %eax, %eax
callq 0x3060
jmp 0x6cc6
leaq 0x22941(%rip), %rdi     # 0x2960e
xorl %eax, %eax
callq 0x3060
jmp 0x6cd6
leaq 0x22966(%rip), %rdi     # 0x29643
xorl %eax, %eax
callq 0x3060
jmp 0x6ce6
leaq 0x22969(%rip), %rdi     # 0x29656
xorl %eax, %eax
callq 0x3060
jmp 0x6cf6
leaq 0x22994(%rip), %rdi     # 0x29691
xorl %eax, %eax
callq 0x3060
jmp 0x6d06
leaq 0x229bf(%rip), %rdi     # 0x296cc
xorl %eax, %eax
callq 0x3060
jmp 0x6d16
leaq 0x229ba(%rip), %rdi     # 0x296d7
xorl %eax, %eax
callq 0x3060
jmp 0x6d26
leaq 0x229be(%rip), %rdi     # 0x296eb
xorl %eax, %eax
callq 0x3060
jmp 0x6d36
leaq 0x229c1(%rip), %rdi     # 0x296fe
xorl %eax, %eax
callq 0x3060
jmp 0x6d46
leaq 0x229cd(%rip), %rdi     # 0x2971a
xorl %eax, %eax
callq 0x3060
jmp 0x6d56
movl $0x1, %edi
callq 0x3390
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xe8(%rbp)
movl %eax, -0xec(%rbp)
jmp 0x760d
movq -0x10(%rbp), %rax
movslq -0xdc(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0xd8(%rbp)
cmpq $0x0, -0xd0(%rbp)
je 0x6e94
movq -0xd0(%rbp), %rdi
movl $0x32, %esi
callq 0x3110
cmpq $0x0, %rax
je 0x6db8
movb $0x1, -0x60(%rbp)
jmp 0x6dd5
movq -0xd0(%rbp), %rdi
movl $0x31, %esi
callq 0x3110
cmpq $0x0, %rax
je 0x6dd3
movb $0x0, -0x60(%rbp)
jmp 0x6dd5
movq -0xd0(%rbp), %rdi
movl $0x6d, %esi
callq 0x3110
cmpq $0x0, %rax
je 0x6df0
movb $0x1, -0x5e(%rbp)
movq -0xd0(%rbp), %rdi
movl $0x4d, %esi
callq 0x3110
cmpq $0x0, %rax
je 0x6e0b
movb $0x1, -0x52(%rbp)
movq -0xd0(%rbp), %rdi
movl $0x6c, %esi
callq 0x3110
cmpq $0x0, %rax
je 0x6e26
movb $0x1, -0x53(%rbp)
movq -0xd0(%rbp), %rdi
movl $0x62, %esi
callq 0x3110
cmpq $0x0, %rax
je 0x6e41
movb $0x1, -0x5d(%rbp)
movq -0xd0(%rbp), %rdi
movl $0x65, %esi
callq 0x3110
cmpq $0x0, %rax
je 0x6e5c
movb $0x1, -0x51(%rbp)
movq -0xd0(%rbp), %rdi
movl $0x76, %esi
callq 0x3110
cmpq $0x0, %rax
je 0x6e77
movb $0x1, -0x5f(%rbp)
movq -0xd0(%rbp), %rdi
movl $0x6e, %esi
callq 0x3110
cmpq $0x0, %rax
je 0x6e92
movb $0x1, -0x4f(%rbp)
jmp 0x6e94
callq 0x91c0
movq %rax, -0x3f8(%rbp)
jmp 0x6ea2
movq -0x3f8(%rbp), %rsi
movq -0xd8(%rbp), %rdx
leaq -0x2f0(%rbp), %rdi
callq 0x9240
jmp 0x6ebe
callq 0x91c0
movq %rax, -0x400(%rbp)
jmp 0x6ecc
movq -0x400(%rbp), %rsi
leaq 0x238dd(%rip), %rdx     # 0x2a7b7
leaq -0x2f8(%rbp), %rdi
callq 0x9240
jmp 0x6ee8
leaq -0x2e8(%rbp), %rdi
leaq -0x2f0(%rbp), %rsi
leaq -0x2f8(%rbp), %rdx
callq 0xeb40
jmp 0x6f04
leaq -0x2e8(%rbp), %rdi
callq 0x9290
movq %rax, -0x408(%rbp)
jmp 0x6f19
movq -0x408(%rbp), %rax
movq %rax, -0x300(%rbp)
cmpq $0x0, -0x300(%rbp)
je 0x75a2
movq -0xd8(%rbp), %rax
movq %rax, -0x418(%rbp)
leaq -0x321(%rbp), %rdi
movq %rdi, -0x410(%rbp)
callq 0x3500
movq -0x418(%rbp), %rsi
movq -0x410(%rbp), %rdx
leaq -0x320(%rbp), %rdi
callq 0x92b0
jmp 0x6f72
leaq -0x321(%rbp), %rdi
callq 0x32e0
movq -0xd8(%rbp), %rdi
movl $0x5c, %esi
callq 0x3330
movq %rax, -0x330(%rbp)
cmpq $0x0, -0x330(%rbp)
je 0x700b
movq -0x330(%rbp), %rsi
incq %rsi
leaq -0x320(%rbp), %rdi
callq 0x3400
jmp 0x6fb8
jmp 0x700b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xe8(%rbp)
movl %eax, -0xec(%rbp)
jmp 0x7601
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xe8(%rbp)
movl %eax, -0xec(%rbp)
leaq -0x321(%rbp), %rdi
callq 0x32e0
jmp 0x7601
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xe8(%rbp)
movl %eax, -0xec(%rbp)
jmp 0x7594
leaq 0x2217a(%rip), %rdx     # 0x2918c
leaq -0x350(%rbp), %rdi
leaq -0x320(%rbp), %rsi
callq 0x9360
jmp 0x7027
leaq 0x2272c(%rip), %rdx     # 0x2975a
leaq -0x370(%rbp), %rdi
leaq -0x320(%rbp), %rsi
callq 0x9360
jmp 0x7043
testb $0x1, -0x60(%rbp)
je 0x74e5
testb $0x1, -0x5e(%rbp)
jne 0x7218
leaq 0x2283f(%rip), %rsi     # 0x2989d
leaq -0x350(%rbp), %rdi
callq 0x3270
jmp 0x706c
leaq -0x350(%rbp), %rdi
callq 0x3130
movq %rax, %rdi
leaq 0x221b1(%rip), %rsi     # 0x29233
callq 0x3370
movq %rax, -0x420(%rbp)
jmp 0x7090
movq -0x420(%rbp), %rax
movq %rax, -0x378(%rbp)
movq -0x378(%rbp), %rdi
movq -0xd8(%rbp), %rdx
leaq 0x226a9(%rip), %rsi     # 0x2975c
xorl %eax, %eax
movb %al, -0x421(%rbp)
callq 0x3430
leaq -0x350(%rbp), %rdi
callq 0x3130
movq %rax, %rsi
movb -0x421(%rbp), %al
leaq 0x226ab(%rip), %rdi     # 0x29787
callq 0x3060
jmp 0x70e3
movb $0x1, -0x5c(%rbp)
movq -0x300(%rbp), %rdi
movq -0x378(%rbp), %rsi
leaq -0xc8(%rbp), %rdx
callq 0x226b0
jmp 0x7103
movq -0x378(%rbp), %rsi
leaq -0x2e8(%rbp), %rdi
leaq -0xc8(%rbp), %rdx
callq 0x117d0
jmp 0x711f
movq -0x378(%rbp), %rdi
callq 0x3290
jmp 0x712d
leaq -0x370(%rbp), %rdi
callq 0x3130
movq %rax, %rsi
leaq 0x2266c(%rip), %rdi     # 0x297af
xorl %eax, %eax
callq 0x3060
jmp 0x714c
leaq 0x2274a(%rip), %rsi     # 0x2989d
leaq -0x370(%rbp), %rdi
callq 0x3270
jmp 0x7161
leaq -0x370(%rbp), %rdi
callq 0x3130
movq %rax, %rdi
leaq 0x220bc(%rip), %rsi     # 0x29233
callq 0x3370
movq %rax, -0x430(%rbp)
jmp 0x7185
movq -0x430(%rbp), %rax
movq %rax, -0x378(%rbp)
movq -0x378(%rbp), %rdi
movq -0xd8(%rbp), %rdx
leaq 0x225b4(%rip), %rsi     # 0x2975c
xorl %eax, %eax
callq 0x3430
movb $0x0, -0x5c(%rbp)
movb $0x0, -0x18(%rbp)
movb $0x0, -0x52(%rbp)
movq -0x300(%rbp), %rdi
movq -0x378(%rbp), %rsi
leaq -0xc8(%rbp), %rdx
callq 0x226b0
jmp 0x71d7
movq -0x378(%rbp), %rdi
callq 0x3290
jmp 0x71e5
jmp 0x74e3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xe8(%rbp)
movl %eax, -0xec(%rbp)
jmp 0x7588
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xe8(%rbp)
movl %eax, -0xec(%rbp)
jmp 0x757c
leaq -0x350(%rbp), %rdi
leaq -0x320(%rbp), %rsi
callq 0x3050
jmp 0x722d
leaq -0x350(%rbp), %rdi
callq 0x3130
movq %rax, %rsi
leaq 0x22598(%rip), %rdi     # 0x297db
xorl %eax, %eax
callq 0x3060
jmp 0x724c
leaq 0x2264a(%rip), %rdx     # 0x2989d
leaq -0x398(%rbp), %rdi
leaq -0x350(%rbp), %rsi
callq 0x9360
jmp 0x7268
leaq -0x398(%rbp), %rdi
callq 0x3130
movq %rax, %rdi
leaq 0x21fb5(%rip), %rsi     # 0x29233
callq 0x3370
movq %rax, -0x438(%rbp)
jmp 0x728c
movq -0x438(%rbp), %rax
movq %rax, -0x3a0(%rbp)
movq -0x3a0(%rbp), %rdi
movq -0xd8(%rbp), %rdx
leaq 0x2255b(%rip), %rsi     # 0x2980a
movb $0x0, %al
callq 0x3430
movq -0x3a0(%rbp), %rdi
movq -0xd8(%rbp), %rdx
leaq 0x2256f(%rip), %rsi     # 0x2983a
movb $0x0, %al
callq 0x3430
movq -0x3a0(%rbp), %rdi
leaq 0x22575(%rip), %rsi     # 0x29855
movb $0x0, %al
callq 0x3430
testb $0x1, -0x51(%rbp)
je 0x7454
leaq -0x3e9(%rbp), %rdi
movq %rdi, -0x440(%rbp)
callq 0x3500
movq -0x440(%rbp), %rdx
leaq 0x22582(%rip), %rsi     # 0x29894
leaq -0x3e8(%rbp), %rdi
callq 0x92b0
jmp 0x7320
leaq -0x3c8(%rbp), %rdi
leaq -0x3e8(%rbp), %rsi
callq 0xd8d0
jmp 0x7335
leaq -0x3e8(%rbp), %rdi
callq 0x3180
leaq -0x3e9(%rbp), %rdi
callq 0x32e0
leaq -0x3c8(%rbp), %rdi
callq 0x93e0
movq %rax, -0x448(%rbp)
jmp 0x7362
movq -0x448(%rbp), %rax
cmpq $0x0, %rax
jne 0x7405
leaq 0x22528(%rip), %rdi     # 0x298a2
xorl %eax, %eax
callq 0x3060
jmp 0x7383
jmp 0x7448
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xe8(%rbp)
movl %eax, -0xec(%rbp)
jmp 0x74d2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xe8(%rbp)
movl %eax, -0xec(%rbp)
jmp 0x73d1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xe8(%rbp)
movl %eax, -0xec(%rbp)
leaq -0x3e8(%rbp), %rdi
callq 0x3180
leaq -0x3e9(%rbp), %rdi
callq 0x32e0
jmp 0x74d2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xe8(%rbp)
movl %eax, -0xec(%rbp)
leaq -0x3c8(%rbp), %rdi
callq 0xda80
jmp 0x74d2
movq -0x3a0(%rbp), %rax
movq %rax, -0x458(%rbp)
leaq -0x3c8(%rbp), %rdi
callq 0x93e0
movq %rax, -0x450(%rbp)
jmp 0x7428
movq -0x450(%rbp), %rdx
movq -0x458(%rbp), %rdi
leaq 0x23876(%rip), %rsi     # 0x2acb3
movb $0x0, %al
callq 0x3430
movb $0x1, -0x18(%rbp)
leaq -0x3c8(%rbp), %rdi
callq 0xda80
movb $0x1, -0x5c(%rbp)
testb $0x1, -0x5d(%rbp)
je 0x747c
movq -0x3a0(%rbp), %rsi
leaq -0x2e8(%rbp), %rdi
leaq -0xc8(%rbp), %rdx
callq 0x11bf0
jmp 0x747a
jmp 0x749a
movq -0x300(%rbp), %rdi
movq -0x3a0(%rbp), %rsi
leaq -0xc8(%rbp), %rdx
callq 0x226b0
jmp 0x7498
jmp 0x749a
movq -0x3a0(%rbp), %rsi
leaq -0x2e8(%rbp), %rdi
leaq -0xc8(%rbp), %rdx
callq 0x117d0
jmp 0x74b6
movq -0x3a0(%rbp), %rdi
callq 0x3290
jmp 0x74c4
leaq -0x398(%rbp), %rdi
callq 0x3180
jmp 0x74e3
leaq -0x398(%rbp), %rdi
callq 0x3180
jmp 0x757c
jmp 0x7556
leaq -0x350(%rbp), %rdi
callq 0x3130
movq %rax, %rsi
leaq 0x223cb(%rip), %rdi     # 0x298c6
xorl %eax, %eax
callq 0x3060
jmp 0x7504
movq -0x300(%rbp), %rdi
leaq -0x350(%rbp), %rsi
movl $0x1, %edx
callq 0x23860
jmp 0x751e
leaq -0x370(%rbp), %rdi
callq 0x3130
movq %rax, %rsi
leaq 0x223b8(%rip), %rdi     # 0x298ec
xorl %eax, %eax
callq 0x3060
jmp 0x753d
movq -0x300(%rbp), %rdi
leaq -0x370(%rbp), %rsi
xorl %edx, %edx
callq 0x23860
jmp 0x7554
jmp 0x7556
leaq -0x370(%rbp), %rdi
callq 0x3180
leaq -0x350(%rbp), %rdi
callq 0x3180
leaq -0x320(%rbp), %rdi
callq 0x3180
jmp 0x75cc
leaq -0x370(%rbp), %rdi
callq 0x3180
leaq -0x350(%rbp), %rdi
callq 0x3180
leaq -0x320(%rbp), %rdi
callq 0x3180
jmp 0x7601
movq -0xd8(%rbp), %rsi
leaq 0x22362(%rip), %rdi     # 0x29912
xorl %eax, %eax
callq 0x3060
jmp 0x75b9
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x3f0(%rbp)
jmp 0x75dd
movl $0x0, -0x4(%rbp)
movl $0x1, -0x3f0(%rbp)
leaq -0x2e8(%rbp), %rdi
callq 0xfe70
leaq -0xc8(%rbp), %rdi
callq 0x9400
movl -0x4(%rbp), %eax
addq $0x460, %rsp            # imm = 0x460
popq %rbp
retq
leaq -0x2e8(%rbp), %rdi
callq 0xfe70
leaq -0xc8(%rbp), %rdi
callq 0x9400
movq -0xe8(%rbp), %rdi
callq 0x34f0
nopw %cs:(%rax,%rax)