// File: /workspace/github_c_makefile_star2_O1/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 $0x4a8, %rsp            # imm = 0x4A8
movq %rsi, %r14
movl %edi, %ebx
xorl %r15d, %r15d
leaq 0x2b68(%rip), %r12      # 0x50a3
movq 0x4a86(%rip), %r13      # 0x6fc8
leaq 0x2abb(%rip), %rbp      # 0x5004
movl %ebx, %edi
movq %r14, %rsi
movq %r12, %rdx
callq 0x2080
cmpl $0x54, %eax
jle 0x2579
leal -0x63(%rax), %ecx
cmpl $0x13, %ecx
ja 0x2599
movslq (%rbp,%rcx,4), %rax
addq %rbp, %rax
jmpq *%rax
movl $0x1, 0x4c25(%rip)      # 0x719c
jmp 0x2549
cmpl $0x4f, %eax
jle 0x2640
cmpl $0x50, %eax
jne 0x2549
movq (%r13), %rdi
callq 0x22a0
movq %rax, 0x4c21(%rip)      # 0x71b8
jmp 0x2549
cmpl $0x55, %eax
jne 0x2549
movq (%r13), %rdi
callq 0x22a0
movq %rax, 0x4c12(%rip)      # 0x71c0
jmp 0x2549
movq (%r13), %rdi
callq 0x22a0
movq %rax, %r15
jmp 0x2549
movq (%r13), %rdi
callq 0x22a0
movq %rax, 0x4bd2(%rip)      # 0x71a0
jmp 0x2549
cmpl $0x0, 0x4bba(%rip)      # 0x7194
jne 0x281e
movl $0x1, 0x4bae(%rip)      # 0x7198
jmp 0x2549
incl 0x4b9b(%rip)            # 0x7190
jmp 0x2549
movq (%r13), %rdi
callq 0x22a0
movq %rax, 0x4b9e(%rip)      # 0x71a8
jmp 0x2549
movq (%r13), %rdi
callq 0x22a0
movq %rax, 0x4b91(%rip)      # 0x71b0
jmp 0x2549
cmpl $0x0, 0x4b6d(%rip)      # 0x7198
jne 0x281e
movl $0x1, 0x4b59(%rip)      # 0x7194
jmp 0x2549
cmpl $-0x1, %eax
je 0x2672
cmpl $0x3a, %eax
je 0x282c
cmpl $0x3f, %eax
jne 0x2549
callq 0x24ec
movq (%r14), %rsi
leaq 0x2a50(%rip), %rdi      # 0x50b6
xorl %eax, %eax
callq 0x2040
jmp 0x2814
movq 0x4967(%rip), %rax      # 0x6fe0
movslq (%rax), %r12
subl %r12d, %ebx
cmpl $0x1, %ebx
jne 0x284d
cmpq $0x0, 0x4b18(%rip)      # 0x71a8
je 0x283f
movq 0x4b13(%rip), %rbx      # 0x71b0
movq %rbx, %rax
orq %r15, %rax
je 0x283f
testq %r15, %r15
jne 0x26d6
movq %rbx, %rdi
callq 0x20b0
leaq 0xe(%rax), %rdi
callq 0x2220
movq %rax, %r15
leaq 0x2a03(%rip), %rsi      # 0x50cc
movq %rax, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2070
cmpl $0x2, 0x4ab3(%rip)      # 0x7190
jl 0x26f1
movq (%r14,%r12,8), %rsi
leaq 0x29f3(%rip), %rdi      # 0x50dd
xorl %eax, %eax
callq 0x2040
movq (%r14,%r12,8), %rdi
movl $0x241, %esi            # imm = 0x241
callq 0x363f
cmpq $-0x1, %rax
je 0x2854
movq %rax, %rbx
cmpl $0x2, 0x4a7d(%rip)      # 0x7190
jl 0x2721
leaq 0x2bc6(%rip), %rdi      # 0x52e2
callq 0x2260
leaq 0x20(%rsp), %r14
movq %r14, %rdi
callq 0x20c0
movq 0x4a6b(%rip), %rsi      # 0x71a0
movq 0x4a84(%rip), %rdx      # 0x71c0
movq 0x4a75(%rip), %rcx      # 0x71b8
xorps %xmm0, %xmm0
movups %xmm0, (%rsp)
movq %r14, %rdi
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x20d0
testq %rax, %rax
je 0x2872
movq %rax, %r13
cmpl $0x2, 0x4a25(%rip)      # 0x7190
jl 0x2779
leaq 0x2b92(%rip), %rdi      # 0x5306
callq 0x2260
movq 0x4a28(%rip), %rsi      # 0x71a8
movq %r13, %rdi
callq 0x2180
cmpl $-0x1, %eax
je 0x28d1
cmpl $0x2, 0x49f8(%rip)      # 0x7190
jl 0x27a6
leaq 0x2b78(%rip), %rdi      # 0x5319
callq 0x2260
movq %r13, %rdi
movq %r15, %rsi
callq 0x2120
cmpl $-0x1, %eax
jne 0x292b
movq 0x4817(%rip), %r14      # 0x6fd8
movq (%r14), %r12
movq %r13, %rdi
callq 0x20a0
leaq 0x29c1(%rip), %rsi      # 0x5194
movq %r12, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2240
cmpl $0x2, 0x49a9(%rip)      # 0x7190
jl 0x27fd
movq (%r14), %rdi
leaq 0x2949(%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
movl $0x1, %edi
callq 0x2200
callq 0x24ec
leaq 0x2b82(%rip), %rdi      # 0x53ac
jmp 0x2838
callq 0x24ec
leaq 0x2b62(%rip), %rdi      # 0x539a
callq 0x2260
jmp 0x2814
callq 0x24ec
leaq 0x2b2a(%rip), %rdi      # 0x5375
jmp 0x2838
callq 0x24ec
jmp 0x2814
movq 0x477d(%rip), %rax      # 0x6fd8
movq (%rax), %rdi
movq (%r14,%r12,8), %rdx
leaq 0x28ad(%rip), %rsi      # 0x5116
xorl %eax, %eax
callq 0x2240
jmp 0x2814
movq 0x475f(%rip), %r14      # 0x6fd8
movq (%r14), %rcx
leaq 0x28bd(%rip), %rdi      # 0x5140
movl $0x23, %esi
movl $0x1, %edx
callq 0x22c0
movq (%r14), %rcx
leaq 0x28c8(%rip), %rdi      # 0x5164
movl $0x11, %esi
movl $0x1, %edx
callq 0x22c0
movq (%r14), %r14
leaq 0x20(%rsp), %rdi
callq 0x20a0
leaq 0x28ea(%rip), %rsi      # 0x51a9
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2240
jmp 0x2805
movq 0x4700(%rip), %r14      # 0x6fd8
movq (%r14), %rdi
movq 0x48c6(%rip), %rdx      # 0x71a8
leaq 0x288d(%rip), %rsi      # 0x5176
xorl %eax, %eax
callq 0x2240
movq (%r14), %r15
movq %r13, %rdi
callq 0x20a0
leaq 0x28a7(%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 0x2814
movq %r13, 0x18(%rsp)
movq %r13, %rdi
callq 0x20f0
movq %rax, %r15
movq %rax, %rdi
callq 0x2270
movl %eax, %r14d
movq %r15, %rdi
callq 0x2170
movq %rax, 0x10(%rsp)
movq %r15, %rdi
callq 0x2110
testq %rax, %rax
je 0x2acb
movq %rax, %rbp
leaq 0x2892(%rip), %r13      # 0x5200
leaq 0x26df(%rip), %r12      # 0x5054
movq (%rbp), %rdi
callq 0x4380
movl 0x70(%rbp), %eax
cmpq $0xe, %rax
ja 0x29cb
movslq (%r12,%rax,4), %rax
addq %r12, %rax
jmpq *%rax
movq (%rbp), %rsi
movzbl 0x38(%rbp), %ecx
movzbl 0x68(%rbp), %r8d
movq %rbx, %rdi
movl $0x4e, %edx
callq 0x357e
cmpl $0x2, 0x47df(%rip)      # 0x7190
jl 0x2ab7
movq (%rbp), %rsi
movq 0x38(%rbp), %rdx
leaq 0x2819(%rip), %rdi      # 0x51df
jmp 0x2ab0
addl $0xffffff07, %eax       # imm = 0xFFFFFF07
cmpl $0x6, %eax
jae 0x2ab7
movq (%rbp), %rsi
movzbl 0x38(%rbp), %ecx
movq %rbx, %rdi
movl $0x43, %edx
xorl %r8d, %r8d
callq 0x357e
cmpl $0x2, 0x4798(%rip)      # 0x7190
jl 0x2ab7
movq (%rbp), %rsi
movq 0x38(%rbp), %rdx
movq %r13, %rdi
jmp 0x2ab0
movq (%rbp), %rsi
movq %rbx, %rdi
movl $0x4e, %edx
movl $0xa, %ecx
xorl %r8d, %r8d
callq 0x357e
cmpl $0x2, 0x4762(%rip)      # 0x7190
jl 0x2ab7
movq (%rbp), %rsi
movq 0x38(%rbp), %rdx
leaq 0x277b(%rip), %rdi      # 0x51be
jmp 0x2ab0
movq (%rbp), %rsi
movq %rbx, %rdi
movl $0x44, %edx
movl $0x8, %ecx
xorl %r8d, %r8d
callq 0x357e
cmpl $0x2, 0x472b(%rip)      # 0x7190
jl 0x2ab7
movq (%rbp), %rsi
leaq 0x27db(%rip), %rdi      # 0x524d
movl $0x8, %edx
xorl %eax, %eax
callq 0x2040
jmp 0x2ab7
movq (%rbp), %rsi
movzbl 0x38(%rbp), %ecx
movq %rbx, %rdi
movl $0x54, %edx
xorl %r8d, %r8d
callq 0x357e
cmpl $0x2, 0x46f1(%rip)      # 0x7190
jl 0x2ab7
movq (%rbp), %rsi
movq 0x38(%rbp), %rdx
leaq 0x2771(%rip), %rdi      # 0x5221
xorl %eax, %eax
callq 0x2040
movq %r15, %rdi
callq 0x2110
movq %rax, %rbp
testq %rax, %rax
jne 0x2975
cmpl $0x2, 0x46be(%rip)      # 0x7190
jl 0x2ae0
leaq 0x287c(%rip), %rdi      # 0x5357
callq 0x2260
movq %rbx, %rdi
callq 0x33dd
cmpl $0x2, 0x46a1(%rip)      # 0x7190
jl 0x2afd
leaq 0x282f(%rip), %rdi      # 0x5327
callq 0x2260
movq %rbx, %rdi
callq 0x34a7
cmpl $0x2, 0x4684(%rip)      # 0x7190
jl 0x2b1a
leaq 0x2831(%rip), %rdi      # 0x5346
callq 0x2260
cmpq $0x0, 0x10(%rsp)
je 0x2bd9
movq %r15, %rdi
callq 0x21c0
testq %rax, %rax
je 0x2bd9
movq %rax, %r12
movl %r14d, %ebp
movq %rbp, 0x10(%rsp)
shlq $0x3, %rbp
movq %rbx, %rdi
callq 0x3f81
cmpq $-0x1, %rax
je 0x2bc5
movq %rax, %r13
cmpl $0x0, 0x10(%rsp)
je 0x2ba8
xorl %r14d, %r14d
movq (%r12,%r14), %rsi
movq 0x18(%r13,%r14,4), %rdi
testq %rsi, %rsi
je 0x2b76
callq 0x21a0
jmp 0x2b79
movb $0x0, (%rdi)
cmpl $0x0, 0x4614(%rip)      # 0x7194
je 0x2b8c
movq 0x18(%r13,%r14,4), %rdi
callq 0x4380
cmpl $0x0, 0x4605(%rip)      # 0x7198
je 0x2b9f
movq 0x18(%r13,%r14,4), %rdi
callq 0x43b5
addq $0x8, %r14
cmpq %r14, %rbp
jne 0x2b61
movq 0x20(%rbx), %rdx
incq %rdx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x410b
movq %rbx, %rdi
movq %r13, %rsi
callq 0x40c4
movq %r15, %rdi
callq 0x21c0
movq %rax, %r12
testq %rax, %rax
jne 0x2b46
cmpl $0x2, 0x45b0(%rip)      # 0x7190
jl 0x2bee
leaq 0x276e(%rip), %rdi      # 0x5357
callq 0x2260
movq %rbx, %rdi
callq 0x33dd
cmpl $0x2, 0x4593(%rip)      # 0x7190
jl 0x2c0b
leaq 0x2764(%rip), %rdi      # 0x536a
callq 0x2260
movq %r15, %rdi
callq 0x21b0
movq 0x18(%rsp), %rdi
callq 0x2230
movl (%rbx), %edi
callq 0x22d0
movq %rbx, %rdi
callq 0x21e0
xorl %edi, %edi
callq 0x2200
nop