// File: /workspace/github_cpp_makefile_star2_O2/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 0x8bc4(%rip), %rsi      # 0xf850
leaq 0x1e0(%rsp), %r13
movl $0x180, %edx            # imm = 0x180
movq %r13, %rdi
callq 0x4260
movq $0x0, 0x30(%rsp)
pushq $-0x1
popq %rcx
leaq 0x3dc2(%rip), %rbx      # 0xaa76
movq $0x0, 0x38(%rsp)
xorl %eax, %eax
movl $0x0, 0x28(%rsp)
movl %ecx, 0x2c(%rsp)
movl %ecx, 0x24(%rsp)
movl %eax, %r14d
movl %r12d, %edi
movq %r15, %rsi
movq %rbx, %rdx
movq %r13, %rcx
xorl %r8d, %r8d
callq 0x4520
movl %eax, %ebp
leal -0x53(%rbp), %eax
cmpl $0x5, %eax
jbe 0x6d24
leal -0x61(%rbp), %eax
cmpl $0x7, %eax
jbe 0x6d57
movb $0x1, %al
cmpl $0x44, %ebp
je 0x6e17
cmpl $0x75, %ebp
je 0x6ccf
cmpl $0x76, %ebp
je 0x6e24
cmpl $-0x1, %ebp
je 0x6e45
cmpl $0x3f, %ebp
jne 0x6d76
jmp 0x6fc1
leaq 0x38c5(%rip), %rcx      # 0xa5f0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x924d(%rip), %rax      # 0xff88
movq (%rax), %rdi
movl %r14d, %eax
testq %rdi, %rdi
je 0x6ccf
callq 0x44d0
movl %eax, 0x28(%rsp)
movl %r14d, %eax
jmp 0x6ccf
leaq 0x38aa(%rip), %rcx      # 0xa608
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movb $0x1, 0x95fa(%rip)      # 0x10368
movl %r14d, %eax
jmp 0x6ccf
movq 0x9233(%rip), %rdi      # 0xffb0
leaq 0x3d1e(%rip), %rsi      # 0xaaa2
callq 0x42d0
movsbl %bpl, %esi
movq %rax, %rdi
callq 0x4370
movq %rax, %rdi
leaq 0x3d09(%rip), %rsi      # 0xaaa8
callq 0x42d0
movl %r14d, %eax
jmp 0x6ccf
movq 0x91d5(%rip), %rax      # 0xff88
movq (%rax), %rdi
movl %r14d, %eax
testq %rdi, %rdi
je 0x6ccf
callq 0x44d0
movl %eax, 0x2c(%rsp)
movl %r14d, %eax
jmp 0x6ccf
movb $0x1, %al
movq %rax, 0x38(%rsp)
movl %r14d, %eax
jmp 0x6ccf
incl 0x9578(%rip)            # 0x10360
movl %r14d, %eax
jmp 0x6ccf
movq 0x9191(%rip), %rax      # 0xff88
movq (%rax), %rdi
movl %r14d, %eax
testq %rdi, %rdi
je 0x6ccf
callq 0x44d0
movl %eax, 0x24(%rsp)
movl %r14d, %eax
jmp 0x6ccf
movq %rax, 0x30(%rsp)
movl %r14d, %eax
jmp 0x6ccf
incl 0x953a(%rip)            # 0x10364
movl %r14d, %eax
jmp 0x6ccf
callq 0x698b
jmp 0x6e3e
callq 0x68fb
xorl %ebx, %ebx
jmp 0x70ac
callq 0x62ac
leaq 0x8(%rsp), %rdi
callq 0x6595
movq 0x38(%rsp), %r15
movl 0x24(%rsp), %ebp
movl 0x2c(%rsp), %ebx
cmpq $0x0, 0x8(%rsp)
je 0x70c0
callq 0x63d5
testl %ebx, %ebx
js 0x6eca
cmpl $0x0, 0x94e3(%rip)      # 0x10360
jne 0x6e88
cmpl $0x0, 0x94de(%rip)      # 0x10364
jle 0x6eb7
testl %ebx, %ebx
leaq 0x3c44(%rip), %rax      # 0xaad5
leaq 0x3c3b(%rip), %rsi      # 0xaad3
cmoveq %rax, %rsi
movq 0x910d(%rip), %rdi      # 0xffb0
callq 0x42d0
leaq 0x3c2a(%rip), %rsi      # 0xaad9
movq %rax, %rdi
callq 0x42d0
movq 0x8(%rsp), %rdi
xorl %esi, %esi
testl %ebx, %ebx
setne %sil
movq (%rdi), %rax
callq *0x20(%rax)
testb $0x1, 0x30(%rsp)
je 0x6ee2
xorl %edi, %edi
xorl %esi, %esi
callq 0x4390
testl %eax, %eax
jne 0x70f2
movq 0x90c7(%rip), %rdi      # 0xffb0
leaq 0x3bfd(%rip), %rsi      # 0xaaed
callq 0x42d0
movq %rax, %rbx
movq 0x8(%rsp), %rdi
movq (%rdi), %rax
callq *0x10(%rax)
movq %rbx, %rdi
movq %rax, %rsi
callq 0x42d0
pushq $0xa
popq %rsi
movq %rax, %rdi
callq 0x4370
movq 0x8(%rsp), %rdi
movq (%rdi), %rax
pushq $0x2
popq %rsi
xorl %edx, %edx
callq *0x40(%rax)
movq 0x8(%rsp), %rdi
movq (%rdi), %rax
pushq $0x1
popq %rsi
xorl %edx, %edx
callq *0x40(%rax)
testl %ebp, %ebp
js 0x6f4a
movq 0x8(%rsp), %rdi
movq (%rdi), %rax
movl %ebp, %esi
callq *0x30(%rax)
movq 0x8(%rsp), %rdi
movq (%rdi), %rax
movzbl %r15b, %ebx
andl $0x1, %ebx
pushq $0x3
popq %rsi
xorl %edx, %edx
movl %ebx, %ecx
callq *0x28(%rax)
movq 0x8(%rsp), %rdi
movq (%rdi), %rax
pushq $0x3
popq %rsi
pushq $0x1
popq %rdx
movl %ebx, %ecx
callq *0x28(%rax)
movq 0x8(%rsp), %rdi
movq (%rdi), %rax
pushq $0x3
popq %rsi
pushq $0x2
popq %rdx
movl %ebx, %ecx
callq *0x28(%rax)
movq 0x8(%rsp), %rdi
movq (%rdi), %rax
pushq $0x3
popq %rsi
pushq $0x3
popq %rdx
movl %ebx, %ecx
callq *0x28(%rax)
xorl %ebx, %ebx
testb $0x1, %r15b
jne 0x70a2
movl 0x28(%rsp), %esi
testl %esi, %esi
jle 0x6ff6
leaq 0x8(%rsp), %rdi
callq 0x69a3
movl %eax, %ebx
jmp 0x70a2
movq 0x8fe8(%rip), %rdi      # 0xffb0
leaq 0x3aad(%rip), %rsi      # 0xaa7c
callq 0x42d0
movq (%r15), %rsi
movq %rax, %rdi
callq 0x42d0
leaq 0x3a9c(%rip), %rsi      # 0xaa82
movq %rax, %rdi
callq 0x42d0
pushq $0x1
popq %rbx
jmp 0x70ac
leaq 0x18(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x5ae0
testb $0x1, %r14b
je 0x7015
leaq 0x18(%rsp), %rdi
callq 0x5c5e
leaq 0x40(%rsp), %rdi
callq 0x47ec
leaq 0x40(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x4874
leaq 0x40(%rsp), %rdi
callq 0x4d42
testb $0x1, %r14b
je 0x7048
leaq 0x18(%rsp), %rdi
callq 0x5b06
xorl %r15d, %r15d
pushq $0x2
popq %rbx
pushq $0x1
popq %rbp
movslq 0x60(%rsp), %rax
cmpq %rax, %r15
jge 0x708c
movslq 0x1b4(%rsp,%r15,4), %r14
movq 0x8(%rsp), %rdi
movq (%rdi), %rax
movl %ebx, %esi
movq %r14, %rdx
xorl %ecx, %ecx
callq *0x28(%rax)
movq 0x8(%rsp), %rdi
movq (%rdi), %rax
movl %ebp, %esi
movq %r14, %rdx
xorl %ecx, %ecx
callq *0x28(%rax)
incq %r15
jmp 0x7051
leaq 0x40(%rsp), %rdi
callq 0x481e
leaq 0x18(%rsp), %rdi
callq 0x5aec
xorl %ebx, %ebx
leaq 0x10(%rsp), %rdi
callq 0x5758
movl %ebx, %eax
addq $0x368, %rsp            # imm = 0x368
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x10
popq %rdi
callq 0x4160
movq %rax, %r15
leaq 0x39d9(%rip), %rsi      # 0xaaab
movq %rax, %rdi
callq 0x4110
movq 0x8f07(%rip), %rsi      # 0xffe8
movq 0x8eb0(%rip), %rdx      # 0xff98
movq %r15, %rdi
callq 0x4530
jmp 0x7142
pushq $0x10
popq %rdi
callq 0x4160
movq %rax, %r15
leaq 0x3924(%rip), %rsi      # 0xaa28
leaq 0x40(%rsp), %rdi
leaq 0x18(%rsp), %rdx
callq 0x5694
callq 0x4030
movl (%rax), %edx
movb $0x1, %bpl
leaq 0x40(%rsp), %rsi
movq %r15, %rdi
callq 0x56d0
xorl %ebp, %ebp
leaq 0x85ad(%rip), %rsi      # 0xf6e0
movq 0x8e66(%rip), %rdx      # 0xffa0
movq %r15, %rdi
callq 0x4530
jmp 0x7144
movq %rdx, %r14
movq %rax, %rbx
jmp 0x718f
jmp 0x719b
movq %rdx, %r14
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x4150
testb %bpl, %bpl
jne 0x716f
jmp 0x71a1
jmp 0x7169
jmp 0x717f
movq %rdx, %r14
movq %rax, %rbx
movq %r15, %rdi
callq 0x4220
jmp 0x71a1
jmp 0x71ad
jmp 0x71ad
jmp 0x71ad
movq %rdx, %r14
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x481e
leaq 0x18(%rsp), %rdi
callq 0x5aec
jmp 0x71a1
movq %rdx, %r14
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x5758
jmp 0x71b3
movq %rdx, %r14
movq %rax, %rbx
cmpl $0x1, %r14d
jne 0x7215
movq %rbx, %rdi
callq 0x4130
movq (%rax), %rcx
movq %rax, %rdi
callq *0x10(%rcx)
movq 0x8e0f(%rip), %rdi      # 0xffe0
movq %rax, %rsi
callq 0x42d0
pushq $0xa
popq %rsi
movq %rax, %rdi
callq 0x4370
callq 0x4170
testl %eax, %eax
je 0x7200
movq 0x8dbc(%rip), %rdi      # 0xffb0
leaq 0x38fa(%rip), %rsi      # 0xaaf5
callq 0x42d0
pushq $0x1
popq %rbx
callq 0x4500
jmp 0x70ac
movq %rax, %rbx
callq 0x4500
movq %rbx, %rdi
callq 0x4550
movq %rax, %rdi
callq 0x5688