// File: /workspace/github_c_makefile_star2_O2/Openvario[P]sensord/cmdline_parser.c
// Function: cmdline_parser
void cmdline_parser(int argc, char **argv, t_io_mode *io_mode){

	// locale variables
	int c;
	char datalog_filename[50];
	char sensordata_filename[50];

	const char* Usage = "\n"\
	"  -v              print version information\n"\
	"  -f              don't daemonize, stay in foreground\n"\
	"  -i              inetd mode\n"\
	"  -c [filename]   use config file [filename]\n"\
	"  -d[n]           set debug level. n can be [1..2]. default=1\n"\
   	"  -j              turn on timing jitter... for testing only!\n"\
	"  -r [filename]   record measurement values to file\n"\
	"  -s              second order temperature compensation for MS5611 enable\n"\
	"  -p [filename]   use values from file instead of measuring\n"\
	"\n";

	// check commandline arguments
	while ((c = getopt (argc, argv, "vd::fijlhr:p:c:s")) != -1)
	{
		switch (c) {
			case 'v':
				//sprintf(s, "sensord V0.1 %s %s", __DATE__
				printf("sensord V%c.%c RELEASE %c build: %s %s %s\n", VERSION_MAJOR, VERSION_MINOR, VERSION_RELEASE, VERSION_GIT, __DATE__, __TIME__);
				printf("sensord  Copyright (C) 2014  see AUTHORS on www.openvario.org\n");
				printf("This program comes with ABSOLUTELY NO WARRANTY;\n");
				printf("This is free software, and you are welcome to redistribute it under certain conditions;\n");
				break;

			case 'c':
				// use config file
				// record sensordata for replay
				if (optarg == NULL)
				{
					fprintf(stderr, "Missing option for -c\n");
					fprintf(stderr, "Exiting ...\n");
					exit(EXIT_FAILURE);
				}
				else
				{
					strcpy(config_filename, optarg);
					fprintf(stderr, "!! Using config file %s !!\n", config_filename);

					// Open the fp to config file
					fp_config = fopen(config_filename,"r");

					//check if config file opened ok ...
					if( fp_config == NULL)
					{
						fprintf(stderr, "Error opening config file: %s\n", config_filename);
						fprintf(stderr, "Exiting ...\n");
						exit(EXIT_FAILURE);
					}
				}
				break;

			case 'd':
				if (optarg == NULL)
				{
					g_debug = 1;
				}
				else
					g_debug = atoi(optarg);

				fprintf(stderr, "!! DEBUG LEVEL %d !!\n",g_debug);
				break;

			case 'f':
				// don't daemonize
				fprintf(stderr, "!! STAY in g_foreground !!\n");
				g_foreground = true;
				break;

			case 'i':
				// inetd mode
				g_foreground = true;
				g_inetd = true;
				break;

			case 'j':
				// Cause deliberate timing jitter
				fprintf(stderr, "!! Timing jitter on !!\n");
				tj = true;
				break;

			case 's':
				// enable second order compensation
				fprintf(stderr, "Second order compensation ENABLE\n");
				g_secordcomp = true;
				break;

			case 'r':
				// record sensordata for replay
				if (optarg == NULL)
				{
					fprintf(stderr, "Missing option for -r\n");
					fprintf(stderr, "Exiting ...\n");
					exit(EXIT_FAILURE);
				}

				io_mode->sensordata_to_file = true;
				strcpy(datalog_filename, optarg);
				fprintf(stderr, "!! RECORD DATA TO %s !!\n", datalog_filename);

				// Open the fp to record file
				fp_datalog = fopen(datalog_filename,"w+");
				break;

			case 'p':
				// replay sensordata instead of measuring
				if (optarg == NULL)
				{
					fprintf(stderr, "Missing option for -p\n");
					fprintf(stderr, "Exiting ...\n");
					exit(EXIT_FAILURE);
				}

				io_mode->sensordata_from_file = true;
				strcpy(sensordata_filename, optarg);
				fprintf(stderr, "!! REPLAY DATA FROM %s !!\n", sensordata_filename);
				// Open the fp to replay file
				fp_sensordata = fopen(sensordata_filename,"r");
				if (fp_sensordata == NULL)
				{
					fprintf(stderr, "Error opening file %s for replay !!\n", sensordata_filename);
					fprintf(stderr, "Exiting ...\n");
					exit(EXIT_FAILURE);
				}
				break;

			case '?':
				fprintf(stderr, "Unknown option %c\n", optopt);
				fprintf(stderr, "Usage: sensord [OPTION]\n%s",Usage);
				fprintf(stderr, "Exiting ...\n");
				exit(EXIT_FAILURE);
				break;
		}
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rdx, 0x8(%rsp)
movq %rsi, %r14
movl %edi, %ebp
leaq 0x3dc6(%rip), %r15      # 0x9304
leaq 0x3b8b(%rip), %r12      # 0x90d0
movb $0x1, %r13b
leaq 0x3e0f(%rip), %rbx      # 0x935e
movl %ebp, %edi
movq %r14, %rsi
movq %r15, %rdx
callq 0x2260
leal -0x63(%rax), %ecx
cmpl $0x13, %ecx
ja 0x55cc
movslq (%r12,%rcx,4), %rax
addq %r12, %rax
jmpq *%rax
movb %r13b, 0x7eac(%rip)     # 0xd420
movb %r13b, 0x7ea6(%rip)     # 0xd421
jmp 0x554f
movq %rbx, (%rsp)
leaq 0x3d8d(%rip), %rdi      # 0x9315
pushq $0x30
popq %rsi
pushq $0x33
popq %rdx
pushq $0x35
popq %rcx
leaq 0x3da8(%rip), %r8       # 0x9340
leaq 0x3db3(%rip), %r9       # 0x9352
xorl %eax, %eax
callq 0x2110
leaq 0x3f30(%rip), %rdi      # 0x94dd
callq 0x2080
leaq 0x3f62(%rip), %rdi      # 0x951b
callq 0x2080
leaq 0x3f86(%rip), %rdi      # 0x954b
callq 0x2080
jmp 0x554f
cmpl $-0x1, %eax
je 0x57d1
cmpl $0x3f, %eax
jne 0x554f
jmp 0x5845
movq 0x79e6(%rip), %rax      # 0xcfd0
movq (%rax), %rsi
testq %rsi, %rsi
je 0x5809
movq 0x8(%rsp), %rax
movb $0x1, 0x1(%rax)
leaq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x2060
movq 0x79e5(%rip), %rax      # 0xcff8
movq (%rax), %rdi
leaq 0x3e52(%rip), %rsi      # 0x946f
movq %rbx, %rdx
xorl %eax, %eax
callq 0x21c0
movq %rbx, %rdi
leaq 0x40ff(%rip), %rsi      # 0x9730
callq 0x2250
movq %rax, 0x7deb(%rip)      # 0xd428
testq %rax, %rax
leaq 0x3d17(%rip), %rbx      # 0x935e
jne 0x554f
jmp 0x5890
movq 0x799f(%rip), %rax      # 0xcff8
movq (%rax), %rcx
leaq 0x3d6c(%rip), %rdi      # 0x93cf
pushq $0x1b
popq %rsi
pushq $0x1
popq %rdx
callq 0x22b0
movb $0x1, 0x7dab(%rip)      # 0xd420
jmp 0x554f
leaq 0x40af(%rip), %r13      # 0x9730
movq 0x7948(%rip), %rax      # 0xcfd0
movq (%rax), %rsi
testq %rsi, %rsi
je 0x57f6
leaq 0x7da5(%rip), %rbx      # 0xd440
movq %rbx, %rdi
callq 0x2060
movq 0x794e(%rip), %rax      # 0xcff8
movq (%rax), %rdi
leaq 0x3cca(%rip), %rsi      # 0x937e
movq %rbx, %rdx
xorl %eax, %eax
callq 0x21c0
movq %rbx, %rdi
movq %r13, %rsi
callq 0x2250
movq %rax, 0x7d68(%rip)      # 0xd438
testq %rax, %rax
movb $0x1, %r13b
leaq 0x3c81(%rip), %rbx      # 0x935e
jne 0x554f
jmp 0x58a8
movq 0x78e1(%rip), %rax      # 0xcfd0
movq (%rax), %rdi
pushq $0x1
popq %rdx
testq %rdi, %rdi
je 0x5701
callq 0x2270
movl %eax, %edx
leaq 0x7d6c(%rip), %rax      # 0xd474
movl %edx, (%rax)
movq 0x78e7(%rip), %rax      # 0xcff8
movq (%rax), %rdi
leaq 0x3c9e(%rip), %rsi      # 0x93b9
xorl %eax, %eax
callq 0x21c0
jmp 0x554f
movq 0x78ca(%rip), %rax      # 0xcff8
movq (%rax), %rcx
leaq 0x3ccb(%rip), %rdi      # 0x9403
pushq $0x21
popq %rsi
pushq $0x1
popq %rdx
callq 0x22b0
movb $0x1, 0x7cd8(%rip)      # 0xd422
jmp 0x554f
movq 0x78a2(%rip), %rax      # 0xcff8
movq (%rax), %rcx
leaq 0x3c8b(%rip), %rdi      # 0x93eb
pushq $0x17
popq %rsi
pushq $0x1
popq %rdx
callq 0x22b0
movb $0x1, 0x7cb1(%rip)      # 0xd423
jmp 0x554f
movq 0x7852(%rip), %rax      # 0xcfd0
movq (%rax), %rsi
testq %rsi, %rsi
je 0x57e3
movq 0x8(%rsp), %rax
movb $0x1, (%rax)
leaq 0x50(%rsp), %rbx
movq %rbx, %rdi
callq 0x2060
movq 0x7856(%rip), %rax      # 0xcff8
movq (%rax), %rdi
leaq 0x3c90(%rip), %rsi      # 0x943c
movq %rbx, %rdx
xorl %eax, %eax
callq 0x21c0
movq %rbx, %rdi
leaq 0x3c95(%rip), %rsi      # 0x9455
callq 0x2250
movq %rax, 0x7c64(%rip)      # 0xd430
jmp 0x5548
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x780e(%rip), %r14      # 0xcff8
movq (%r14), %rcx
leaq 0x3c31(%rip), %rdi      # 0x9425
jmp 0x581a
movq 0x77fb(%rip), %r14      # 0xcff8
movq (%r14), %rcx
leaq 0x3b60(%rip), %rdi      # 0x9367
jmp 0x581a
movq 0x77e8(%rip), %r14      # 0xcff8
movq (%r14), %rcx
leaq 0x3c3e(%rip), %rdi      # 0x9458
pushq $0x16
popq %rsi
pushq $0x1
popq %rbx
movq %rbx, %rdx
callq 0x22b0
movq (%r14), %rcx
leaq 0x3677(%rip), %rdi      # 0x8ea9
pushq $0xc
popq %rsi
movq %rbx, %rdx
callq 0x22b0
pushq $0x1
popq %rdi
callq 0x2290
movq 0x77ac(%rip), %rbx      # 0xcff8
movq (%rbx), %rdi
movq 0x778a(%rip), %rax      # 0xcfe0
movl (%rax), %edx
leaq 0x3c50(%rip), %rsi      # 0x94af
xorl %eax, %eax
callq 0x21c0
movq (%rbx), %rdi
leaq 0x3c52(%rip), %rsi      # 0x94c2
leaq 0x38a9(%rip), %rdx      # 0x9120
xorl %eax, %eax
callq 0x21c0
movq (%rbx), %rcx
leaq 0x3621(%rip), %rdi      # 0x8ea9
pushq $0xc
popq %rsi
pushq $0x1
popq %rdx
jmp 0x5838
movq 0x7761(%rip), %rbx      # 0xcff8
movq (%rbx), %rdi
leaq 0x3be9(%rip), %rsi      # 0x948a
leaq 0x10(%rsp), %rdx
jmp 0x5877
movq 0x7749(%rip), %rbx      # 0xcff8
movq (%rbx), %rdi
leaq 0x3ae1(%rip), %rsi      # 0x939a
leaq 0x7b80(%rip), %rdx      # 0xd440
jmp 0x5877
nop