// File: AGWA[P]logmailer/logmailer.cpp
// Function: main
int main (int argc, char** argv)
{
	const char*			pidfile = NULL;
	bool				pidfile_created = false;
	int				exitcode = 0;

	try {
		hostname = get_hostname();

		Log_mailer		log_mailer;
		bool			no_daemonize = false;
		const char*		username = NULL;
		const char*		groupname = NULL;

		int			flag;
		while ((flag = getopt(argc, argv, "fu:g:p:m:M:r:s:")) != -1) {
			switch (flag) {
			case 'f':
				no_daemonize = true;
				break;
			case 'u':
				username = optarg;
				break;
			case 'g':
				groupname = optarg;
				break;
			case 'p':
				pidfile = optarg;
				break;
			case 'm':
				log_mailer.min_wait_time = std::atol(optarg);
				break;
			case 'M':
				log_mailer.max_wait_time = std::atol(optarg);
				break;
			case 'r':
				log_mailer.recipient = optarg;
				break;
			case 's':
				log_mailer.subject_format = optarg;
				break;
			default:
				print_usage(argv[0]);
				return 2;
			}
		}

		if (argc - optind != 1) {
			print_usage(argv[0]);
			return 2;
		}

		log_mailer.fifo_path = argv[optind];

		if (no_daemonize && pidfile) {
			std::clog << argv[0] << ": -p (PID file) can't be specified with -f (don't daemonize)" << std::endl;
			return 2;
		}

		// Open FIFO before dropping privileges in case FIFO isn't readable by unprivileged user.
		// Open FIFO before daemonizing so we can report errors.
		log_mailer.open_fifo();

		drop_privileges(username, groupname);
		if (!no_daemonize) {
			daemonize(pidfile);
			if (pidfile) {
				pidfile_created = true;
			}
		}
		init_signals();
		log_mailer.run(is_running, flush_requested);
	} catch (const System_error& error) {
		std::clog << argv[0] << ": " << error.syscall;
		if (!error.target.empty()) {
			std::clog << ": " << error.target;
		}
		std::clog << ": " << std::strerror(error.number) << std::endl;
		exitcode = 3;
	}
	if (pidfile_created) {
		unlink(pidfile);
	}
	return exitcode;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x170, %rsp            # imm = 0x170
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movb $0x0, -0x19(%rbp)
movl $0x0, -0x20(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x3bb0
jmp 0x3727
leaq 0x5bca(%rip), %rdi      # 0x92f8
leaq -0x40(%rbp), %rsi
movq %rsi, -0x130(%rbp)
callq 0x3370
movq -0x130(%rbp), %rdi
callq 0x3588
leaq -0x100(%rbp), %rdi
callq 0x5440
jmp 0x3758
movb $0x0, -0x101(%rbp)
movq $0x0, -0x110(%rbp)
movq $0x0, -0x118(%rbp)
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
leaq 0x3934(%rip), %rdx      # 0x70b7
callq 0x3070
movl %eax, -0x11c(%rbp)
cmpl $-0x1, %eax
je 0x3999
movl -0x11c(%rbp), %eax
addl $-0x4d, %eax
movl %eax, %ecx
movq %rcx, -0x138(%rbp)
subl $0x28, %eax
ja 0x3969
movq -0x138(%rbp), %rax
leaq 0x3844(%rip), %rcx      # 0x7004
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x3890
movb $0x1, -0x101(%rbp)
jmp 0x3994
movq 0x57d3(%rip), %rax      # 0x8fc0
movq (%rax), %rax
movq %rax, -0x110(%rbp)
jmp 0x3994
movq 0x57bd(%rip), %rax      # 0x8fc0
movq (%rax), %rax
movq %rax, -0x118(%rbp)
jmp 0x3994
movq 0x57a7(%rip), %rax      # 0x8fc0
movq (%rax), %rax
movq %rax, -0x18(%rbp)
jmp 0x3994
movq 0x5794(%rip), %rax      # 0x8fc0
movq (%rax), %rdi
callq 0x3360
movq %rax, -0xa0(%rbp)
jmp 0x3994
movq 0x5779(%rip), %rax      # 0x8fc0
movq (%rax), %rdi
callq 0x3360
movq %rax, -0x98(%rbp)
jmp 0x3994
movq 0x575e(%rip), %rax      # 0x8fc0
movq (%rax), %rsi
leaq -0xe0(%rbp), %rdi
callq 0x33e0
jmp 0x3873
jmp 0x3994
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x100(%rbp), %rdi
callq 0x5d60
movl -0x4c(%rbp), %eax
movl $0x1, %ecx
cmpl %ecx, %eax
jne 0x3b91
movq -0x48(%rbp), %rdi
callq 0x30f0
movq %rax, -0x128(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rsi
movq 0x56ea(%rip), %rdi      # 0x8fa8
callq 0x3290
movq %rax, -0x140(%rbp)
jmp 0x38cc
movq -0x140(%rbp), %rdi
leaq 0x387f(%rip), %rsi      # 0x7159
callq 0x3290
movq %rax, -0x148(%rbp)
jmp 0x38e8
movq -0x148(%rbp), %rdi
movq -0x128(%rbp), %rsi
callq 0x3240
jmp 0x38fd
movq -0x128(%rbp), %rdi
addq $0x20, %rdi
callq 0x34c0
testb $0x1, %al
jne 0x3b08
movq 0x568c(%rip), %rdi      # 0x8fa8
leaq 0x3836(%rip), %rsi      # 0x7159
callq 0x3290
movq %rax, -0x150(%rbp)
jmp 0x3931
movq -0x150(%rbp), %rdi
movq -0x128(%rbp), %rsi
addq $0x20, %rsi
callq 0x3240
jmp 0x394a
jmp 0x3b08
movq 0x566a(%rip), %rax      # 0x8fc0
movq (%rax), %rsi
leaq -0xc0(%rbp), %rdi
callq 0x33e0
jmp 0x3967
jmp 0x3994
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq 0x5631(%rip), %rsi      # 0x8fa8
callq 0x3db0
jmp 0x397e
movl $0x2, -0x4(%rbp)
movl $0x1, -0x120(%rbp)
jmp 0x3ad6
jmp 0x3775
movl -0x8(%rbp), %eax
movq 0x5635(%rip), %rcx      # 0x8fd8
subl (%rcx), %eax
cmpl $0x1, %eax
je 0x39d5
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq 0x55f0(%rip), %rsi      # 0x8fa8
callq 0x3db0
jmp 0x39bf
movl $0x2, -0x4(%rbp)
movl $0x1, -0x120(%rbp)
jmp 0x3ad6
movq -0x10(%rbp), %rax
movq 0x55f8(%rip), %rcx      # 0x8fd8
movslq (%rcx), %rcx
movq (%rax,%rcx,8), %rsi
leaq -0x100(%rbp), %rdi
callq 0x33e0
jmp 0x39f5
testb $0x1, -0x101(%rbp)
je 0x3a65
cmpq $0x0, -0x18(%rbp)
je 0x3a65
movq -0x10(%rbp), %rax
movq (%rax), %rsi
movq 0x5595(%rip), %rdi      # 0x8fa8
callq 0x3290
movq %rax, -0x158(%rbp)
jmp 0x3a21
movq -0x158(%rbp), %rdi
leaq 0x3698(%rip), %rsi      # 0x70c7
callq 0x3290
movq %rax, -0x160(%rbp)
jmp 0x3a3d
movq -0x160(%rbp), %rdi
movq 0x556d(%rip), %rsi      # 0x8fb8
callq 0x32c0
jmp 0x3a52
movl $0x2, -0x4(%rbp)
movl $0x1, -0x120(%rbp)
jmp 0x3ad6
leaq -0x100(%rbp), %rdi
callq 0x5570
jmp 0x3a73
movq -0x110(%rbp), %rdi
movq -0x118(%rbp), %rsi
callq 0x3e00
jmp 0x3a88
testb $0x1, -0x101(%rbp)
jne 0x3aa9
movq -0x18(%rbp), %rdi
callq 0x4570
jmp 0x3a9c
cmpq $0x0, -0x18(%rbp)
je 0x3aa7
movb $0x1, -0x19(%rbp)
jmp 0x3aa9
callq 0x49e0
jmp 0x3ab0
leaq 0x5819(%rip), %rsi      # 0x92d0
leaq 0x5836(%rip), %rdx      # 0x92f4
leaq -0x100(%rbp), %rdi
callq 0x5720
jmp 0x3acc
movl $0x0, -0x120(%rbp)
leaq -0x100(%rbp), %rdi
callq 0x5d60
movl -0x120(%rbp), %eax
testl %eax, %eax
je 0x3af3
jmp 0x3aee
jmp 0x3b85
jmp 0x3b6c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
callq 0x34b0
jmp 0x3b7d
movq 0x5499(%rip), %rdi      # 0x8fa8
leaq 0x3643(%rip), %rsi      # 0x7159
callq 0x3290
movq %rax, -0x168(%rbp)
jmp 0x3b24
movq -0x128(%rbp), %rax
movl 0x40(%rax), %edi
callq 0x30e0
movq -0x168(%rbp), %rdi
movq %rax, %rsi
callq 0x3290
movq %rax, -0x170(%rbp)
jmp 0x3b4b
movq -0x170(%rbp), %rdi
movq 0x545f(%rip), %rsi      # 0x8fb8
callq 0x32c0
jmp 0x3b60
movl $0x3, -0x20(%rbp)
callq 0x34b0
testb $0x1, -0x19(%rbp)
je 0x3b7f
movq -0x18(%rbp), %rdi
callq 0x3140
jmp 0x3b7f
jmp 0x3b91
movl -0x20(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x170, %rsp            # imm = 0x170
popq %rbp
retq
movq -0x48(%rbp), %rdi
callq 0x3500
movq %rax, %rdi
callq 0x5dc0
nopw %cs:(%rax,%rax)