// File: /workspace/github_cpp_makefile_star2_O1/merelin[P]mediasmartserverd/src/mediasmartserverd.cpp
// Function: main
int main( int argc, char* argv[] ) try {
	int brightness = -1;
	int light_show = 0;
	int mount_usb = -1;
	bool run_as_daemon = false;
	bool xmas = false;
	bool run_update_monitor = false;
	
	// long command line arguments
	const struct option long_opts[] = {
		{ "brightness",     required_argument, 0, 'b' },
		{ "daemon",         no_argument,       0, 'D' },
		{ "activity",       no_argument,       0, 'a' },
		{ "debug",          no_argument,       0, 'd' },
		{ "help",           no_argument,       0, 'h' },
		{ "light-show",     required_argument, 0, 'S' },
		{ "update-monitor", no_argument,       0, 'u' },
		{ "usb",            required_argument, 0, 'U' },
		{ "verbose",        no_argument,       0, 'v' },
		{ "version",        no_argument,       0, 'V' },
		{ "xmas",           no_argument,       0, 'X' },
		{ 0, 0, 0, 0 },
	};
	
	// pass command line arguments
	while ( true ) {
		const int c = getopt_long( argc, argv, "aDuvV", long_opts, 0 );
		if ( -1 == c ) break;
		
		switch ( c ) {
		case 'b': // brightness
			if ( optarg ) brightness = atoi( optarg );
			break;
		case 'd': // debug
			++debug;
			break;
		case 'D': // run as a daemon (background)
			run_as_daemon = true;
			break;
		case 'a': // run as a daemon (background)
			activity = true;
			break;
		case 'h': // help!
			return show_help( );
		case 'S': // light-show
			if ( optarg ) light_show = atoi( optarg );
			break;
		case 'u': //Use system LED as update notification light.
			run_update_monitor = true;
			break;
		case 'U': // mount/unmount USB device
			if ( optarg ) mount_usb = atoi( optarg );
			break;
		case 'v': // verbose, more verbose, even more verbose
			++verbose;
			break;
		case 'V': // our version
			return show_version( );
		case 'X': // light all the LEDs up like a xmas tree
			xmas = true;
			break;
		case '?': // no idea
			cout << "Try `" << argv[0] << " --help' for more information.\n";
			return 1;
		default:
			cout << "+++ '" << (char)c << "'\n";
		}
	}
	
	
	// register signal handlers
	init_signals( );
	
	// find led control interface
	LedControlPtr leds = get_led_interface( );
	if ( !leds ) throw std::runtime_error( "Failed to find an LED control interface" );
	
	// drop root priviledges
	drop_priviledges( );
	
	// mount USB?
	if ( mount_usb >= 0 ) {
		if ( debug || verbose > 0 ) cout << ( (mount_usb) ? "M" : "Unm" ) << "ounting USB device\n";
		leds->MountUsb( !!mount_usb );
	}
	
	// run as a daemon?
	if ( run_as_daemon && daemon( 0, 0 ) ) throw ErrnoException( "daemon" );
	
	cout << "Found: " << leds->Desc( ) << '\n';
	
	// disable annoying blinking guy // changed to disabling completely
	leds->SetSystemLed( LED_RED, false );
	leds->SetSystemLed( LED_BLUE, false );
	
	//
	if ( brightness >= 0 ) leds->SetBrightness( brightness );
	
	// clear out LEDs
	leds->Set( LED_BLUE | LED_RED, 0, xmas );
	leds->Set( LED_BLUE | LED_RED, 1, xmas );
	leds->Set( LED_BLUE | LED_RED, 2, xmas );
	leds->Set( LED_BLUE | LED_RED, 3, xmas );
	if ( xmas ) return 0;
	
	if ( light_show > 0 ) return run_light_show( leds, light_show );
	
	// initialise update monitor
	UpdateMonitor update_monitor(leds);
	if (run_update_monitor) update_monitor.Start();
	
	// initialise device monitor
	DeviceMonitor device_monitor;
	device_monitor.Init( leds );
	
	// begin monitoring
	device_monitor.Main( );
	
	if (run_update_monitor) update_monitor.Stop();
	
	// re-enable annoying blinking // leaving it disabled
	//leds->SetSystemLed( LED_BLUE, LED_BLINK );
    for( int i = 0; i < device_monitor.numDisks(); ++i )
    {
        int led_idx = device_monitor.ledIndex(i);
        leds->Set( LED_RED, led_idx, false );
        leds->Set( LED_BLUE, led_idx, false );
    }
	
	return 0;
	
} catch ( std::exception& e ) {
	std::cerr << e.what() << '\n';
	if ( 0 != getuid() ) cout << "Try running as root\n";
	
	return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x368, %rsp            # imm = 0x368
movq %rsi, %r12
movl %edi, %r13d
leaq 0x80de(%rip), %rsi      # 0xf830
leaq 0x1e0(%rsp), %rbx
movl $0x180, %edx            # imm = 0x180
movq %rbx, %rdi
callq 0x4220
movq $0x0, 0x30(%rsp)
movl $0xffffffff, 0x20(%rsp) # imm = 0xFFFFFFFF
leaq 0x430f(%rip), %rbp      # 0xba8e
movq $0x0, 0x38(%rsp)
movq $0x0, 0x28(%rsp)
movl $0x0, 0x1c(%rsp)
movl $0xffffffff, 0x24(%rsp) # imm = 0xFFFFFFFF
xorl %r15d, %r15d
movl %r13d, %edi
movq %r12, %rsi
movq %rbp, %rdx
movq %rbx, %rcx
xorl %r8d, %r8d
callq 0x44f0
movl %eax, %r14d
cmpl $0x60, %eax
jg 0x7807
cmpl $0x52, %r14d
jle 0x7829
leal -0x53(%r14), %eax
cmpl $0x5, %eax
ja 0x794c
leaq 0x3e2e(%rip), %rcx      # 0xb608
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x879e(%rip), %rax      # 0xff88
movq (%rax), %rdi
xorl %r14d, %r14d
testq %rdi, %rdi
je 0x79e3
callq 0x44a0
movl %eax, 0x1c(%rsp)
jmp 0x79e3
leal -0x61(%r14), %eax
cmpl $0x7, %eax
ja 0x7856
leaq 0x3e09(%rip), %rcx      # 0xb620
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movb $0x1, 0x8b31(%rip)      # 0x10358
jmp 0x787f
cmpl $-0x1, %r14d
je 0x7904
cmpl $0x3f, %r14d
je 0x790f
cmpl $0x44, %r14d
jne 0x794c
xorl %r14d, %r14d
movb $0x1, %al
movq %rax, 0x30(%rsp)
jmp 0x79e3
cmpl $0x75, %r14d
je 0x7995
cmpl $0x76, %r14d
jne 0x794c
incl 0x8ae4(%rip)            # 0x10354
jmp 0x787f
callq 0x735c
jmp 0x78d2
incl 0x8ad1(%rip)            # 0x10350
xorl %r14d, %r14d
jmp 0x79e3
movq 0x86fa(%rip), %rax      # 0xff88
movq (%rax), %rdi
xorl %r14d, %r14d
testq %rdi, %rdi
je 0x79e3
callq 0x44a0
movl %eax, 0x24(%rsp)
jmp 0x79e3
xorl %r14d, %r14d
movb $0x1, %al
movq %rax, 0x38(%rsp)
jmp 0x79e3
movl $0x39, %edx
movq 0x86ea(%rip), %rdi      # 0xffb0
leaq 0x4116(%rip), %rsi      # 0xb9e3
callq 0x4310
xorl %r15d, %r15d
movl $0x1, %r14d
jmp 0x79e3
movq 0x86a1(%rip), %rax      # 0xff88
movq (%rax), %rdi
xorl %r14d, %r14d
testq %rdi, %rdi
je 0x79e3
callq 0x44a0
movl %eax, 0x20(%rsp)
jmp 0x79e3
movl $0x3, %r14d
jmp 0x79e3
movl $0x5, %edx
movq 0x8695(%rip), %rdi      # 0xffb0
leaq 0x4172(%rip), %rsi      # 0xba94
callq 0x4310
movq (%r12), %r14
testq %r14, %r14
je 0x79a1
movq %r14, %rdi
callq 0x4130
movq 0x8671(%rip), %rdi      # 0xffb0
movq %r14, %rsi
movq %rax, %rdx
callq 0x4310
jmp 0x79bf
movl $0x5, %edx
movq 0x8658(%rip), %rdi      # 0xffb0
leaq 0x415b(%rip), %rsi      # 0xbaba
callq 0x4310
movb %r14b, (%rsp)
movl $0x1, %edx
movq 0x863c(%rip), %rdi      # 0xffb0
movq %rsp, %rsi
callq 0x4310
xorl %r14d, %r14d
movl $0x2, %edx
movq %rax, %rdi
leaq 0x4132(%rip), %rsi      # 0xbac0
callq 0x4310
jmp 0x79e3
xorl %r14d, %r14d
movb $0x1, %al
movq %rax, 0x28(%rsp)
jmp 0x79e3
movq 0x8608(%rip), %rcx      # 0xffb0
movq (%rcx), %rax
movq -0x18(%rax), %rax
leaq (%rcx,%rax), %rdi
movl 0x20(%rcx,%rax), %esi
orl $0x1, %esi
callq 0x4510
movl $0x1, %r14d
movl $0x1f, %edx
movq 0x85df(%rip), %rdi      # 0xffb0
leaq 0x40c2(%rip), %rsi      # 0xba9a
callq 0x4310
movl $0x1, %r15d
testl %r14d, %r14d
je 0x77a4
cmpl $0x3, %r14d
jne 0x7c87
callq 0x6a40
movq %rsp, %rdi
callq 0x6dd1
movq 0x38(%rsp), %r12
movl 0x24(%rsp), %ebp
movl 0x20(%rsp), %ebx
cmpq $0x0, (%rsp)
je 0x7c9c
callq 0x6b95
testl %ebx, %ebx
js 0x7a8d
cmpl $0x0, 0x8925(%rip)      # 0x10350
jne 0x7a36
cmpl $0x0, 0x8920(%rip)      # 0x10354
jle 0x7a7b
xorl %eax, %eax
testl %ebx, %ebx
sete %al
leaq 0x40a9(%rip), %rcx      # 0xbaed
leaq 0x40a0(%rip), %rsi      # 0xbaeb
cmoveq %rcx, %rsi
leaq 0x1(,%rax,2), %rdx
movq 0x8552(%rip), %rdi      # 0xffb0
callq 0x4310
movq 0x8546(%rip), %rdi      # 0xffb0
leaq 0x4080(%rip), %rsi      # 0xbaf1
movl $0x13, %edx
callq 0x4310
movq (%rsp), %rdi
xorl %esi, %esi
testl %ebx, %ebx
setne %sil
movq (%rdi), %rax
callq *0x20(%rax)
testb $0x1, 0x30(%rsp)
je 0x7aa5
xorl %edi, %edi
xorl %esi, %esi
callq 0x4350
testl %eax, %eax
jne 0x7cd0
movq 0x8504(%rip), %rbx      # 0xffb0
leaq 0x4052(%rip), %rsi      # 0xbb05
movl $0x7, %edx
movq %rbx, %rdi
callq 0x4310
movq (%rsp), %rdi
movq (%rdi), %rax
callq *0x10(%rax)
movq %rax, %r14
testq %rax, %rax
je 0x7aee
movq %r14, %rdi
callq 0x4130
movq 0x84cf(%rip), %rdi      # 0xffb0
movq %r14, %rsi
movq %rax, %rdx
callq 0x4310
jmp 0x7b05
movq (%rbx), %rax
movq -0x18(%rax), %rax
leaq (%rbx,%rax), %rdi
movl 0x20(%rbx,%rax), %esi
orl $0x1, %esi
callq 0x4510
leaq 0x10(%rsp), %rsi
movb $0xa, (%rsi)
movq 0x849c(%rip), %rdi      # 0xffb0
movl $0x1, %edx
callq 0x4310
movq (%rsp), %rdi
movq (%rdi), %rax
movl $0x2, %esi
xorl %edx, %edx
callq *0x40(%rax)
movq (%rsp), %rdi
movq (%rdi), %rax
movl $0x1, %esi
xorl %edx, %edx
callq *0x40(%rax)
testl %ebp, %ebp
js 0x7b50
movq (%rsp), %rdi
movq (%rdi), %rax
movl %ebp, %esi
callq *0x30(%rax)
movq (%rsp), %rdi
movq (%rdi), %rax
movzbl %r12b, %ebx
andl $0x1, %ebx
movl $0x3, %esi
xorl %edx, %edx
movl %ebx, %ecx
callq *0x28(%rax)
movq (%rsp), %rdi
movq (%rdi), %rax
movl $0x1, %edx
movl $0x3, %esi
movl %ebx, %ecx
callq *0x28(%rax)
movq (%rsp), %rdi
movq (%rdi), %rax
movl $0x2, %edx
movl $0x3, %esi
movl %ebx, %ecx
callq *0x28(%rax)
movq (%rsp), %rdi
movq (%rdi), %rax
movl $0x3, %edx
movl $0x3, %esi
movl %ebx, %ecx
callq *0x28(%rax)
xorl %r15d, %r15d
testb $0x1, %r12b
jne 0x7c7d
movl 0x1c(%rsp), %esi
testl %esi, %esi
jle 0x7bd1
movq %rsp, %rdi
callq 0x7439
movl %eax, %r15d
jmp 0x7c7d
leaq 0x10(%rsp), %rdi
movq %rsp, %rsi
callq 0x60ac
movq 0x28(%rsp), %rbx
testb $0x1, %bl
je 0x7bf2
leaq 0x10(%rsp), %rdi
callq 0x627e
leaq 0x40(%rsp), %rdi
callq 0x47cc
leaq 0x40(%rsp), %rdi
movq %rsp, %rsi
callq 0x486c
leaq 0x40(%rsp), %rdi
callq 0x4e36
testb $0x1, %bl
je 0x7c22
leaq 0x10(%rsp), %rdi
callq 0x60e0
cmpl $0x0, 0x60(%rsp)
jle 0x7c69
xorl %r14d, %r14d
movslq 0x1b4(%rsp,%r14,4), %rbx
movq (%rsp), %rdi
movq (%rdi), %rax
movl $0x2, %esi
movq %rbx, %rdx
xorl %ecx, %ecx
callq *0x28(%rax)
movq (%rsp), %rdi
movq (%rdi), %rax
movl $0x1, %esi
movq %rbx, %rdx
xorl %ecx, %ecx
callq *0x28(%rax)
incq %r14
movslq 0x60(%rsp), %rax
cmpq %rax, %r14
jl 0x7c2c
leaq 0x40(%rsp), %rdi
callq 0x4806
leaq 0x10(%rsp), %rdi
callq 0x60c6
leaq 0x8(%rsp), %rdi
callq 0x5d12
movl %r15d, %eax
addq $0x368, %rsp            # imm = 0x368
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x10, %edi
callq 0x4140
movq %rax, %r15
leaq 0x3e13(%rip), %rsi      # 0xbac3
movq %rax, %rdi
callq 0x4100
movq 0x8329(%rip), %rsi      # 0xffe8
movq 0x82d2(%rip), %rdx      # 0xff98
movq %r15, %rdi
callq 0x4520
jmp 0x7d2e
movl $0x10, %edi
callq 0x4140
movq %rax, %r15
leaq 0x50(%rsp), %r12
movq %r12, -0x10(%r12)
leaq 0x3d52(%rip), %rsi      # 0xba40
leaq 0x3d51(%rip), %rdx      # 0xba46
leaq 0x40(%rsp), %rdi
callq 0x5e88
callq 0x4030
movl (%rax), %edx
movb $0x1, %bpl
leaq 0x40(%rsp), %rsi
movq %r15, %rdi
callq 0x5be8
xorl %ebp, %ebp
leaq 0x79d1(%rip), %rsi      # 0xf6f0
movq 0x827a(%rip), %rdx      # 0xffa0
movq %r15, %rdi
callq 0x4520
jmp 0x7d30
movq %rdx, %r14
movq %rax, %rbx
jmp 0x7d8f
jmp 0x7d9b
movq %rdx, %r14
movq %rax, %rbx
movq 0x40(%rsp), %rdi
cmpq %r12, %rdi
je 0x7d62
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x42c0
jmp 0x7d62
movq %rdx, %r14
movq %rax, %rbx
movb $0x1, %bpl
testb %bpl, %bpl
jne 0x7d71
jmp 0x7da1
jmp 0x7d7f
movq %rdx, %r14
movq %rax, %rbx
movq %r15, %rdi
callq 0x41e0
jmp 0x7da1
jmp 0x7dad
jmp 0x7dad
movq %rdx, %r14
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x4806
leaq 0x10(%rsp), %rdi
callq 0x60c6
jmp 0x7da1
movq %rdx, %r14
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x5d12
jmp 0x7db3
movq %rdx, %r14
movq %rax, %rbx
cmpl $0x1, %r14d
jne 0x7e27
movq %rbx, %rdi
callq 0x4120
movq (%rax), %rcx
movq %rax, %rdi
callq *0x10(%rcx)
movq 0x820f(%rip), %rdi      # 0xffe0
movq %rax, %rsi
callq 0x4290
leaq 0x40(%rsp), %rsi
movb $0xa, (%rsi)
movl $0x1, %edx
movq %rax, %rdi
callq 0x4310
callq 0x4150
testl %eax, %eax
je 0x7e0f
movq 0x81b2(%rip), %rdi      # 0xffb0
leaq 0x3d08(%rip), %rsi      # 0xbb0d
movl $0x14, %edx
callq 0x4310
callq 0x44d0
movl $0x1, %r15d
jmp 0x7c87
movq %rax, %rbx
callq 0x44d0
movq %rbx, %rdi
callq 0x4550
movq %rax, %rdi
callq 0x5b9e
nop