// File: /workspace/github_cpp_makefile_star2_O3/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, %r15
movl %edi, %r12d
leaq 0x8136(%rip), %rsi      # 0xf890
leaq 0x1e0(%rsp), %r13
movl $0x180, %edx            # imm = 0x180
movq %r13, %rdi
callq 0x4220
movq $0x0, 0x30(%rsp)
movl $0xffffffff, 0x24(%rsp) # imm = 0xFFFFFFFF
leaq 0x4307(%rip), %rbx      # 0xba8e
movq $0x0, 0x38(%rsp)
xorl %eax, %eax
movl $0x0, 0x28(%rsp)
movl $0xffffffff, 0x2c(%rsp) # imm = 0xFFFFFFFF
movl %eax, %ebp
movl %r12d, %edi
movq %r15, %rsi
movq %rbx, %rdx
movq %r13, %rcx
xorl %r8d, %r8d
callq 0x44f0
movl %eax, %r14d
cmpl $0x60, %eax
jle 0x77e4
leal -0x61(%r14), %eax
cmpl $0x7, %eax
ja 0x7828
leaq 0x3e50(%rip), %rcx      # 0xb620
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movb $0x1, 0x8b78(%rip)      # 0x10358
movl %ebp, %eax
jmp 0x77a2
cmpl $0x52, %r14d
jle 0x784b
leal -0x53(%r14), %eax
cmpl $0x5, %eax
ja 0x78d4
leaq 0x3e0a(%rip), %rcx      # 0xb608
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x877a(%rip), %rax      # 0xff88
movq (%rax), %rdi
movl %ebp, %eax
testq %rdi, %rdi
je 0x77a2
callq 0x44a0
movl %eax, 0x28(%rsp)
movl %ebp, %eax
jmp 0x77a2
movb $0x1, %al
cmpl $0x75, %r14d
je 0x77a2
cmpl $0x76, %r14d
jne 0x78d4
incl 0x8b10(%rip)            # 0x10354
movl %ebp, %eax
jmp 0x77a2
cmpl $0x44, %r14d
jne 0x78c4
movb $0x1, %al
movq %rax, 0x30(%rsp)
movl %ebp, %eax
jmp 0x77a2
movq 0x8722(%rip), %rax      # 0xff88
movq (%rax), %rdi
movl %ebp, %eax
testq %rdi, %rdi
je 0x77a2
callq 0x44a0
movl %eax, 0x2c(%rsp)
movl %ebp, %eax
jmp 0x77a2
incl 0x8ac6(%rip)            # 0x10350
movl %ebp, %eax
jmp 0x77a2
movb $0x1, %al
movq %rax, 0x38(%rsp)
movl %ebp, %eax
jmp 0x77a2
movq 0x86e2(%rip), %rax      # 0xff88
movq (%rax), %rdi
movl %ebp, %eax
testq %rdi, %rdi
je 0x77a2
callq 0x44a0
movl %eax, 0x24(%rsp)
movl %ebp, %eax
jmp 0x77a2
cmpl $-0x1, %r14d
je 0x798e
cmpl $0x3f, %r14d
je 0x7948
movl $0x5, %edx
movq 0x86d0(%rip), %rdi      # 0xffb0
leaq 0x41d3(%rip), %rsi      # 0xbaba
callq 0x4310
movb %r14b, 0x8(%rsp)
movl $0x1, %edx
movq 0x86b3(%rip), %rdi      # 0xffb0
leaq 0x8(%rsp), %rsi
callq 0x4310
movl $0x2, %edx
movq %rax, %rdi
leaq 0x41aa(%rip), %rsi      # 0xbac0
callq 0x4310
movl %ebp, %eax
jmp 0x77a2
callq 0x7359
jmp 0x7941
movq 0x8680(%rip), %rdi      # 0xffb0
leaq 0x40ac(%rip), %rsi      # 0xb9e3
movl $0x39, %edx
callq 0x4310
xorl %ebx, %ebx
jmp 0x7c6f
movq 0x8661(%rip), %rbx      # 0xffb0
leaq 0x413e(%rip), %rsi      # 0xba94
movl $0x5, %edx
movq %rbx, %rdi
callq 0x4310
movq (%r15), %r14
testq %r14, %r14
je 0x7a91
movq %r14, %rdi
callq 0x4130
movq 0x8632(%rip), %rdi      # 0xffb0
movq %r14, %rsi
movq %rax, %rdx
callq 0x4310
jmp 0x7aa8
callq 0x6a44
leaq 0x8(%rsp), %rdi
callq 0x6dce
movl 0x28(%rsp), %r15d
movq 0x38(%rsp), %r12
movl 0x2c(%rsp), %r13d
movl 0x24(%rsp), %ebx
cmpq $0x0, 0x8(%rsp)
je 0x7c83
callq 0x6b96
testl %ebx, %ebx
js 0x7a2f
cmpl $0x0, 0x8984(%rip)      # 0x10350
jne 0x79d7
cmpl $0x0, 0x897f(%rip)      # 0x10354
jle 0x7a1c
xorl %eax, %eax
testl %ebx, %ebx
sete %al
leaq 0x4108(%rip), %rcx      # 0xbaed
leaq 0x40ff(%rip), %rsi      # 0xbaeb
cmoveq %rcx, %rsi
leaq 0x1(,%rax,2), %rdx
movq 0x85b1(%rip), %rdi      # 0xffb0
callq 0x4310
movq 0x85a5(%rip), %rdi      # 0xffb0
leaq 0x40df(%rip), %rsi      # 0xbaf1
movl $0x13, %edx
callq 0x4310
movq 0x8(%rsp), %rdi
xorl %esi, %esi
testl %ebx, %ebx
setne %sil
movq (%rdi), %rax
callq *0x20(%rax)
testb $0x1, 0x30(%rsp)
je 0x7a47
xorl %edi, %edi
xorl %esi, %esi
callq 0x4350
testl %eax, %eax
jne 0x7cb7
movq 0x8562(%rip), %rbx      # 0xffb0
leaq 0x40b0(%rip), %rsi      # 0xbb05
movl $0x7, %edx
movq %rbx, %rdi
callq 0x4310
movq 0x8(%rsp), %rdi
movq (%rdi), %rax
callq *0x10(%rax)
movq %rax, %r14
testq %rax, %rax
je 0x7aca
movq %r14, %rdi
callq 0x4130
movq 0x852c(%rip), %rdi      # 0xffb0
movq %r14, %rsi
movq %rax, %rdx
callq 0x4310
jmp 0x7ae1
movq (%rbx), %rax
movq -0x18(%rax), %rax
leaq (%rbx,%rax), %rdi
movl 0x20(%rbx,%rax), %esi
orl $0x1, %esi
callq 0x4510
movl $0x1, %ebx
movq 0x84fc(%rip), %rdi      # 0xffb0
leaq 0x3fdf(%rip), %rsi      # 0xba9a
movl $0x1f, %edx
callq 0x4310
jmp 0x7c6f
movq (%rbx), %rax
movq -0x18(%rax), %rax
leaq (%rbx,%rax), %rdi
movl 0x20(%rbx,%rax), %esi
orl $0x1, %esi
callq 0x4510
leaq 0x18(%rsp), %rsi
movb $0xa, (%rsi)
movq 0x84c0(%rip), %rdi      # 0xffb0
movl $0x1, %edx
callq 0x4310
movq 0x8(%rsp), %rdi
movq (%rdi), %rax
movl $0x2, %esi
xorl %edx, %edx
callq *0x40(%rax)
movq 0x8(%rsp), %rdi
movq (%rdi), %rax
movl $0x1, %esi
xorl %edx, %edx
callq *0x40(%rax)
testl %r13d, %r13d
js 0x7b31
movq 0x8(%rsp), %rdi
movq (%rdi), %rax
movl %r13d, %esi
callq *0x30(%rax)
movq 0x8(%rsp), %rdi
movq (%rdi), %rax
movzbl %r12b, %ebx
andl $0x1, %ebx
movl $0x3, %esi
xorl %edx, %edx
movl %ebx, %ecx
callq *0x28(%rax)
movq 0x8(%rsp), %rdi
movq (%rdi), %rax
movl $0x1, %edx
movl $0x3, %esi
movl %ebx, %ecx
callq *0x28(%rax)
movq 0x8(%rsp), %rdi
movq (%rdi), %rax
movl $0x2, %edx
movl $0x3, %esi
movl %ebx, %ecx
callq *0x28(%rax)
movq 0x8(%rsp), %rdi
movq (%rdi), %rax
movl $0x3, %edx
movl $0x3, %esi
movl %ebx, %ecx
callq *0x28(%rax)
xorl %ebx, %ebx
testb $0x1, %r12b
jne 0x7c65
testl %r15d, %r15d
jle 0x7bb6
leaq 0x8(%rsp), %rdi
movl %r15d, %esi
callq 0x7436
movl %eax, %ebx
jmp 0x7c65
leaq 0x18(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x60bc
testb $0x1, %bpl
je 0x7bd5
leaq 0x18(%rsp), %rdi
callq 0x628a
leaq 0x40(%rsp), %rdi
callq 0x47cc
leaq 0x40(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x486c
leaq 0x40(%rsp), %rdi
callq 0x4e32
testb $0x1, %bpl
je 0x7c08
leaq 0x18(%rsp), %rdi
callq 0x60f0
cmpl $0x0, 0x60(%rsp)
jle 0x7c51
xorl %r15d, %r15d
movslq 0x1b4(%rsp,%r15,4), %r14
movq 0x8(%rsp), %rdi
movq (%rdi), %rax
movl $0x2, %esi
movq %r14, %rdx
xorl %ecx, %ecx
callq *0x28(%rax)
movq 0x8(%rsp), %rdi
movq (%rdi), %rax
movl $0x1, %esi
movq %r14, %rdx
xorl %ecx, %ecx
callq *0x28(%rax)
incq %r15
movslq 0x60(%rsp), %rax
cmpq %rax, %r15
jl 0x7c12
leaq 0x40(%rsp), %rdi
callq 0x4806
leaq 0x18(%rsp), %rdi
callq 0x60d6
leaq 0x10(%rsp), %rdi
callq 0x5d10
movl %ebx, %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 0x3e2c(%rip), %rsi      # 0xbac3
movq %rax, %rdi
callq 0x4100
movq 0x8342(%rip), %rsi      # 0xffe8
movq 0x82eb(%rip), %rdx      # 0xff98
movq %r15, %rdi
callq 0x4520
jmp 0x7d15
movl $0x10, %edi
callq 0x4140
movq %rax, %r15
leaq 0x50(%rsp), %r12
movq %r12, -0x10(%r12)
leaq 0x3d6b(%rip), %rsi      # 0xba40
leaq 0x3d6a(%rip), %rdx      # 0xba46
leaq 0x40(%rsp), %rdi
callq 0x5e90
callq 0x4030
movl (%rax), %edx
movb $0x1, %bpl
leaq 0x40(%rsp), %rsi
movq %r15, %rdi
callq 0x5be6
xorl %ebp, %ebp
leaq 0x7a7a(%rip), %rsi      # 0xf780
movq 0x8293(%rip), %rdx      # 0xffa0
movq %r15, %rdi
callq 0x4520
jmp 0x7d17
movq %rdx, %r14
movq %rax, %rbx
jmp 0x7d6f
jmp 0x7d7b
movq %rdx, %r14
movq %rax, %rbx
movq 0x40(%rsp), %rdi
cmpq %r12, %rdi
je 0x7d3e
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x42c0
testb %bpl, %bpl
jne 0x7d4f
jmp 0x7d81
jmp 0x7d49
jmp 0x7d5f
movq %rdx, %r14
movq %rax, %rbx
movq %r15, %rdi
callq 0x41e0
jmp 0x7d81
jmp 0x7d8d
jmp 0x7d8d
jmp 0x7d8d
movq %rdx, %r14
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x4806
leaq 0x18(%rsp), %rdi
callq 0x60d6
jmp 0x7d81
movq %rdx, %r14
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x5d10
jmp 0x7d93
movq %rdx, %r14
movq %rax, %rbx
cmpl $0x1, %r14d
jne 0x7e06
movq %rbx, %rdi
callq 0x4120
movq (%rax), %rcx
movq %rax, %rdi
callq *0x10(%rcx)
movq 0x822f(%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 0x7def
movq 0x81d2(%rip), %rdi      # 0xffb0
leaq 0x3d28(%rip), %rsi      # 0xbb0d
movl $0x14, %edx
callq 0x4310
callq 0x44d0
movl $0x1, %ebx
jmp 0x7c6f
movq %rax, %rbx
callq 0x44d0
movq %rbx, %rdi
callq 0x4550
movq %rax, %rdi
callq 0x5b9c