// File: /workspace/github_c_makefile_star2_O2/sonicse[P]dbf2mysql/mysql2dbf.c
// Function: main
int main(int argc, char **argv) {
	int             i;
	MYSQL		*SQLsock,mysql;
	extern int      optind;
	extern char     *optarg;
	char            *query = NULL;

	dbhead          *dbh;
	field			*rec;
	MYSQL_RES		*qres;
	MYSQL_ROW			qrow;
	MYSQL_FIELD			*qfield;
	u_long			numfields, numrows, t;

	while ((i = getopt(argc, argv, "lucvq:h:d:t:p:U:P:")) != EOF) {
		switch(i) {
			case 'q':
				query = strdup(optarg);
				break;
			case 'v':
				verbose++;
				break;
			case 'c':
				create = 1;
				break;
			case 'l':
				if (upper) {
					usage();
					printf("Can't use -u and -l at the same time!\n");
					exit(1);
				}
				lower = 1;
				break;
			case 'u':
				if (lower) {
					usage();
					printf("Can't use -u and -l at the same time!\n");
					exit(1);
				}
				upper = 1;
				break;
			case 'h':
				host = (char *)strdup(optarg);
				break;
			case 'd':
				dbase = (char *)strdup(optarg);
				break;
			case 't':
				table = (char *)strdup(optarg);
				break;
		        case 'P':
				pass = (char *)strdup(optarg);
				break;
		        case 'U':
			        user = (char *)strdup(optarg);
				break;
			case ':':
				usage();
				printf("Missing argument!\n");
				exit(1);
			case '?':
				usage();
				printf("Unknown argument: %s\n", argv[0]);
				exit(1);
			default:
				break;
		}
	}

	argc -= optind;
	argv = &argv[optind];

	if (argc != 1) {
		usage();
		exit(1);
	}

	if ((dbase == NULL) || ((table == NULL) && (query == NULL))) {
		usage();
		printf("Dbase and table *must* be specified!\n");
		exit(1);
	}

	if (query == NULL) {
		query = (char *)malloc(14+strlen(table));
		sprintf(query, "SELECT * FROM %s", table);
	}

	if (verbose > 1) {
		printf("Opening %s for writing (previous contents will be lost)\n",
																	argv[0]);
	}

	if ((dbh = dbf_open_new(argv[0], O_WRONLY | O_CREAT | O_TRUNC)) == (dbhead *)DBF_ERROR) {
		fprintf(stderr, "Couldn't open xbase-file for writing: %s\n", argv[0]);
		exit(1);
	}

	if (verbose > 1) {
		printf("Making connection with mySQL-server\n");
	}

        mysql_init(&mysql);
        
    if (!(SQLsock = mysql_real_connect(&mysql,host,user,pass, NULL, 0, NULL, 0))) {
        fprintf(stderr, "Couldn't get a connection with the ");
        fprintf(stderr, "designated host!\n");
        fprintf(stderr, "Detailed report: %s\n", mysql_error(&mysql));
        close(dbh->db_fd);
        free(dbh);
        exit(1);
    }

    if (verbose > 1) {
        printf("Selecting database\n");
    }

    if ((mysql_select_db(SQLsock, dbase)) == -1) {
        fprintf(stderr, "Couldn't select database %s.\n", dbase);
        fprintf(stderr, "Detailed report: %s\n", mysql_error(SQLsock));
        close(dbh->db_fd);
        free(dbh);
        mysql_close(SQLsock);
        exit(1);
    }

	if (verbose > 1) {
		printf("Sending query\n");
	}
	

	if (mysql_query(SQLsock, query) == -1) {
		fprintf(stderr, "Error sending query.\nDetailed report: %s\n",
				mysql_error(SQLsock));
		if (verbose > 1) {
			fprintf(stderr, "%s\n", query);
		}
		mysql_close(SQLsock);
		close(dbh->db_fd);
		free(dbh);
		exit(1);
	}

	qres            = mysql_store_result(SQLsock);
	numfields       = mysql_num_fields(qres);
	numrows			= mysql_num_rows(qres);

	while ((qfield = mysql_fetch_field(qres))) {
	  
	  strtoupper(qfield->name);
	  switch (qfield->type) {
	    /* assume INT's have a max. of 10 digits (4^32 has 10 digits) */
	  case FIELD_TYPE_CHAR:
	  case FIELD_TYPE_SHORT:
	  case FIELD_TYPE_LONG:
	  
	    dbf_add_field(dbh, qfield->name, 'N', 10, 0);
	    if (verbose > 1) {
	      printf("Adding field: %s, INT_TYPE, %ld\n", qfield->name,
		     qfield->length);
	    
	    }
	    break;
				/* do we have to make the same assumption here? */
	  case FIELD_TYPE_DOUBLE:
	  case FIELD_TYPE_FLOAT:
	  case FIELD_TYPE_DECIMAL:
	    dbf_add_field(dbh, qfield->name, 'N', qfield->length,
			  qfield->decimals);
	    if (verbose > 1) {
	      printf("Adding field: %s, INT_REAL, %ld\n", qfield->name,
		     qfield->length);
	    }
	    break;
	  case FIELD_TYPE_STRING:
	  case FIELD_TYPE_VAR_STRING:
	  case FIELD_TYPE_BLOB:
	  case FIELD_TYPE_TINY_BLOB:
	  case FIELD_TYPE_MEDIUM_BLOB:
	  case FIELD_TYPE_LONG_BLOB:
	    dbf_add_field(dbh, qfield->name, 'C', qfield->length, 0);
	    if (verbose > 1) {
	      printf("Adding field: %s, INT_CHAR, %ld\n", qfield->name,
		     qfield->length);
	    }
	    break;
	  case FIELD_TYPE_DATETIME:
	  case FIELD_TYPE_TIMESTAMP:
	    dbf_add_field(dbh, qfield->name, 'T', qfield->length, 0);
	    if (verbose > 1) {
	     printf("Adding field: %s, FIELD_TYPE_DATETIME, %ld\n", qfield->name,
	        qfield->length);
	    }
	    break;
	  case FIELD_TYPE_DATE:
	  case FIELD_TYPE_NEWDATE:
	    //dbf_add_field(dbh, qfield->name, 'D', qfield->length, 0);
	    dbf_add_field(dbh, qfield->name, 'D', 8, 0); // date is 8 in dbf, Mysql returns 10 ?!
	    if (verbose > 1) {
	      printf("Adding field: %s, FIELD_TYPE_DATE, %d\n", qfield->name,8);
	      //qfield->length);
	    }
	    break;
	  default:
	    break;
	  }
	}
	
	if (verbose > 1) {
	  printf("Writing out header\n");
	}
	
	dbf_write_head(dbh);
	if (verbose > 1) {
	  printf("Writing out field-descriptions\n");
	}
	dbf_put_fields(dbh);
	if (verbose > 1) {
	  printf("Writing out data\n");
	}
	if (numrows) {
	  while ((qrow = mysql_fetch_row(qres)) != NULL) {

	    if ((rec = dbf_build_record(dbh)) != (field *)DBF_ERROR) {
	       
	      for (t = 0; t < numfields; t++) {
		//printf("ROW: %s\n",qrow[t]);
		if (qrow[t] != 0) {
		  strcpy(rec[t].db_contents, qrow[t]);
		} else {
		  rec[t].db_contents[0] = '\0';
		}
		if (upper) {
		  strtoupper(rec[t].db_contents);
		}
		if (lower) {
		  strtolower(rec[t].db_contents);
		}
	      }
	 
	      dbf_put_record(dbh, rec, dbh->db_records + 1);
	    
	      dbf_free_record(dbh, rec);
	  
	    }
	  }
	}
	
	if (verbose > 1) {
		printf("Writing out header\n");
	}

	dbf_write_head(dbh);

	if (verbose > 1) {
		printf("Closing up\n");
	}
				
	mysql_free_result(qres);
	mysql_close(SQLsock);
	close(dbh->db_fd);
	free(dbh);
	exit(0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4b8, %rsp            # imm = 0x4B8
movq %rsi, %r14
movl %edi, %ebx
xorl %r15d, %r15d
leaq 0x2c78(%rip), %r12      # 0x50a3
movq 0x5b96(%rip), %r13      # 0x7fc8
leaq 0x2bcb(%rip), %rbp      # 0x5004
movl %ebx, %edi
movq %r14, %rsi
movq %r12, %rdx
callq 0x2080
leal -0x63(%rax), %ecx
cmpl $0x13, %ecx
ja 0x2464
movslq (%rbp,%rcx,4), %rax
addq %rbp, %rax
jmpq *%rax
movl $0x1, 0x5d3a(%rip)      # 0x819c
jmp 0x2439
cmpl $0x50, %eax
je 0x2534
cmpl $0x55, %eax
je 0x251f
cmpl $-0x1, %eax
je 0x2549
cmpl $0x3a, %eax
je 0x2701
cmpl $0x3f, %eax
jne 0x2439
jmp 0x2714
movq (%r13), %rdi
callq 0x22a0
movq %rax, %r15
jmp 0x2439
movq (%r13), %rdi
callq 0x22a0
movq %rax, 0x5cf0(%rip)      # 0x81a0
jmp 0x2439
cmpl $0x0, 0x5cdb(%rip)      # 0x8194
jne 0x26f3
movl $0x1, 0x5ccf(%rip)      # 0x8198
jmp 0x2439
incl 0x5cbc(%rip)            # 0x8190
jmp 0x2439
movq (%r13), %rdi
callq 0x22a0
movq %rax, 0x5cbf(%rip)      # 0x81a8
jmp 0x2439
movq (%r13), %rdi
callq 0x22a0
movq %rax, 0x5cb2(%rip)      # 0x81b0
jmp 0x2439
cmpl $0x0, 0x5c8e(%rip)      # 0x8198
jne 0x26f3
movl $0x1, 0x5c7a(%rip)      # 0x8194
jmp 0x2439
movq (%r13), %rdi
callq 0x22a0
movq %rax, 0x5c91(%rip)      # 0x81c0
jmp 0x2439
movq (%r13), %rdi
callq 0x22a0
movq %rax, 0x5c74(%rip)      # 0x81b8
jmp 0x2439
movq 0x5a90(%rip), %rax      # 0x7fe0
movslq (%rax), %r12
subl %r12d, %ebx
cmpl $0x1, %ebx
jne 0x273a
cmpq $0x0, 0x5c41(%rip)      # 0x81a8
je 0x272c
movq 0x5c3c(%rip), %rbx      # 0x81b0
movq %rbx, %rax
orq %r15, %rax
je 0x272c
testq %r15, %r15
jne 0x25ad
movq %rbx, %rdi
callq 0x20b0
leaq 0xe(%rax), %rdi
callq 0x2220
movq %rax, %r15
leaq 0x2b2c(%rip), %rsi      # 0x50cc
movq %rax, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2070
cmpl $0x2, 0x5bdc(%rip)      # 0x8190
jl 0x25c8
movq (%r14,%r12,8), %rsi
leaq 0x2b1c(%rip), %rdi      # 0x50dd
xorl %eax, %eax
callq 0x2040
movq (%r14,%r12,8), %rdi
movl $0x241, %esi            # imm = 0x241
callq 0x3485
cmpq $-0x1, %rax
je 0x2741
movq %rax, %rbx
cmpl $0x2, 0x5ba6(%rip)      # 0x8190
jl 0x25f8
leaq 0x2cef(%rip), %rdi      # 0x52e2
callq 0x2260
leaq 0x30(%rsp), %r14
movq %r14, %rdi
callq 0x20c0
movq 0x5b94(%rip), %rsi      # 0x81a0
movq 0x5bad(%rip), %rdx      # 0x81c0
movq 0x5b9e(%rip), %rcx      # 0x81b8
xorps %xmm0, %xmm0
movups %xmm0, (%rsp)
movq %r14, %rdi
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x20d0
testq %rax, %rax
je 0x275f
movq %rax, %r13
cmpl $0x2, 0x5b4e(%rip)      # 0x8190
jl 0x2650
leaq 0x2cbb(%rip), %rdi      # 0x5306
callq 0x2260
movq 0x5b51(%rip), %rsi      # 0x81a8
movq %r13, %rdi
callq 0x2180
cmpl $-0x1, %eax
je 0x27ba
cmpl $0x2, 0x5b21(%rip)      # 0x8190
jl 0x267d
leaq 0x2ca1(%rip), %rdi      # 0x5319
callq 0x2260
movq %r13, %rdi
movq %r15, %rsi
callq 0x2120
cmpl $-0x1, %eax
jne 0x2814
movq 0x5940(%rip), %r14      # 0x7fd8
movq (%r14), %r12
movq %r13, %rdi
callq 0x20a0
leaq 0x2aea(%rip), %rsi      # 0x5194
movq %r12, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2240
cmpl $0x2, 0x5ad2(%rip)      # 0x8190
jl 0x26d4
movq (%r14), %rdi
leaq 0x2a72(%rip), %rsi      # 0x513c
movq %r15, %rdx
xorl %eax, %eax
callq 0x2240
movq %r13, %rdi
callq 0x2230
movl (%rbx), %edi
callq 0x22d0
movq %rbx, %rdi
callq 0x21e0
pushq $0x1
popq %rdi
callq 0x2200
callq 0x23dc
leaq 0x2cad(%rip), %rdi      # 0x53ac
jmp 0x270d
callq 0x23dc
leaq 0x2c8d(%rip), %rdi      # 0x539a
callq 0x2260
jmp 0x26eb
callq 0x23dc
movq (%r14), %rsi
leaq 0x2993(%rip), %rdi      # 0x50b6
xorl %eax, %eax
callq 0x2040
jmp 0x26eb
callq 0x23dc
leaq 0x2c3d(%rip), %rdi      # 0x5375
jmp 0x270d
callq 0x23dc
jmp 0x26eb
movq 0x5890(%rip), %rax      # 0x7fd8
movq (%rax), %rdi
movq (%r14,%r12,8), %rdx
leaq 0x29c0(%rip), %rsi      # 0x5116
xorl %eax, %eax
callq 0x2240
jmp 0x26eb
movq 0x5872(%rip), %r15      # 0x7fd8
movq (%r15), %rcx
leaq 0x29d0(%rip), %rdi      # 0x5140
pushq $0x23
popq %rsi
pushq $0x1
popq %r14
movq %r14, %rdx
callq 0x22c0
movq (%r15), %rcx
leaq 0x29db(%rip), %rdi      # 0x5164
pushq $0x11
popq %rsi
movq %r14, %rdx
callq 0x22c0
movq (%r15), %r14
leaq 0x30(%rsp), %rdi
callq 0x20a0
leaq 0x2a01(%rip), %rsi      # 0x51a9
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2240
jmp 0x26dc
movq 0x5817(%rip), %r14      # 0x7fd8
movq (%r14), %rdi
movq 0x59dd(%rip), %rdx      # 0x81a8
leaq 0x29a4(%rip), %rsi      # 0x5176
xorl %eax, %eax
callq 0x2240
movq (%r14), %r15
movq %r13, %rdi
callq 0x20a0
leaq 0x29be(%rip), %rsi      # 0x51a9
movq %r15, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2240
movl (%rbx), %edi
callq 0x22d0
movq %rbx, %rdi
callq 0x21e0
movq %r13, %rdi
callq 0x2230
jmp 0x26eb
movq %r13, 0x28(%rsp)
movq %r13, %rdi
callq 0x20f0
movq %rax, %r15
movq %rax, %rdi
callq 0x2270
movl %eax, 0x1c(%rsp)
movq %r15, %rdi
callq 0x2170
movq %rax, 0x20(%rsp)
pushq $0x43
popq %r13
leaq 0x280c(%rip), %r14      # 0x5054
pushq $0x4e
popq %r12
movq %r15, %rdi
callq 0x2110
testq %rax, %rax
je 0x299d
movq %rax, %rbp
movq (%rax), %rdi
callq 0x40b0
movl 0x70(%rbp), %eax
cmpq $0xe, %rax
ja 0x28af
movslq (%r14,%rax,4), %rax
addq %r14, %rax
jmpq *%rax
movq (%rbp), %rsi
movzbl 0x38(%rbp), %ecx
movzbl 0x68(%rbp), %r8d
movq %rbx, %rdi
movl %r12d, %edx
callq 0x33d2
cmpl $0x2, 0x58f7(%rip)      # 0x8190
jl 0x284c
movq (%rbp), %rsi
movq 0x38(%rbp), %rdx
leaq 0x2935(%rip), %rdi      # 0x51df
jmp 0x2991
addl $0xffffff07, %eax       # imm = 0xFFFFFF07
cmpl $0x6, %eax
jae 0x284c
movq (%rbp), %rsi
movzbl 0x38(%rbp), %ecx
movq %rbx, %rdi
movl %r13d, %edx
xorl %r8d, %r8d
callq 0x33d2
cmpl $0x2, 0x58ba(%rip)      # 0x8190
jl 0x284c
movq (%rbp), %rsi
movq 0x38(%rbp), %rdx
leaq 0x2915(%rip), %rdi      # 0x5200
jmp 0x2991
movq (%rbp), %rsi
movq %rbx, %rdi
movl %r12d, %edx
pushq $0xa
popq %rcx
xorl %r8d, %r8d
callq 0x33d2
cmpl $0x2, 0x5884(%rip)      # 0x8190
jl 0x284c
movq (%rbp), %rsi
movq 0x38(%rbp), %rdx
leaq 0x289d(%rip), %rdi      # 0x51be
jmp 0x2991
movq (%rbp), %rsi
movq %rbx, %rdi
pushq $0x44
popq %rdx
pushq $0x8
popq %rcx
xorl %r8d, %r8d
callq 0x33d2
cmpl $0x2, 0x5851(%rip)      # 0x8190
jl 0x284c
movq (%rbp), %rsi
leaq 0x28fd(%rip), %rdi      # 0x524d
pushq $0x8
popq %rdx
xorl %eax, %eax
callq 0x2040
jmp 0x284c
movq (%rbp), %rsi
movzbl 0x38(%rbp), %ecx
movq %rbx, %rdi
pushq $0x54
popq %rdx
xorl %r8d, %r8d
callq 0x33d2
cmpl $0x2, 0x5814(%rip)      # 0x8190
jl 0x284c
movq (%rbp), %rsi
movq 0x38(%rbp), %rdx
leaq 0x2890(%rip), %rdi      # 0x5221
xorl %eax, %eax
callq 0x2040
jmp 0x284c
cmpl $0x2, 0x57ec(%rip)      # 0x8190
jl 0x29b2
leaq 0x29aa(%rip), %rdi      # 0x5357
callq 0x2260
movq %rbx, %rdi
callq 0x3248
cmpl $0x2, 0x57cf(%rip)      # 0x8190
jl 0x29cf
leaq 0x295d(%rip), %rdi      # 0x5327
callq 0x2260
movq %rbx, %rdi
callq 0x3306
cmpl $0x2, 0x57b2(%rip)      # 0x8190
jl 0x29ec
leaq 0x295f(%rip), %rdi      # 0x5346
callq 0x2260
cmpq $0x0, 0x20(%rsp)
jne 0x2a4e
cmpl $0x2, 0x5795(%rip)      # 0x8190
jl 0x2a09
leaq 0x2953(%rip), %rdi      # 0x5357
callq 0x2260
movq %rbx, %rdi
callq 0x3248
cmpl $0x2, 0x5778(%rip)      # 0x8190
jl 0x2a26
leaq 0x2949(%rip), %rdi      # 0x536a
callq 0x2260
movq %r15, %rdi
callq 0x21b0
movq 0x28(%rsp), %rdi
callq 0x2230
movl (%rbx), %edi
callq 0x22d0
movq %rbx, %rdi
callq 0x21e0
xorl %edi, %edi
callq 0x2200
movl 0x1c(%rsp), %r14d
shlq $0x3, %r14
movq %r15, %rdi
callq 0x21c0
testq %rax, %rax
je 0x29f4
movq %rax, %r13
movq %rbx, %rdi
callq 0x3d0b
cmpq $-0x1, %rax
je 0x2a57
movq %rax, %r12
xorl %ebp, %ebp
cmpq %rbp, %r14
je 0x2ac4
movq (%r13,%rbp), %rsi
movq 0x18(%r12,%rbp,4), %rdi
testq %rsi, %rsi
je 0x2a95
callq 0x21a0
jmp 0x2a98
movb $0x0, (%rdi)
cmpl $0x0, 0x56f5(%rip)      # 0x8194
je 0x2aab
movq 0x18(%r12,%rbp,4), %rdi
callq 0x40b0
cmpl $0x0, 0x56e6(%rip)      # 0x8198
je 0x2abe
movq 0x18(%r12,%rbp,4), %rdi
callq 0x40db
addq $0x8, %rbp
jmp 0x2a7a
movq 0x20(%rbx), %rdx
incq %rdx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x3e6f
movq %rbx, %rdi
movq %r12, %rsi
callq 0x3e2c
jmp 0x2a57
nop