// File: /workspace/github_c_makefile_star2_O2/sfeakes[P]unifi-motion/unifi-motion.c
// Function: main
int main(int argc, char *argv[]) {
  struct mg_mgr mgr;
  //umConfig config;
  int i;
  bool readConfig = false;
  char buffer[BUF_LEN];
  int length;
  int retval;
  bool logMotion = false;

  for (i = 1; i < argc; i++) {
    if (strcmp(argv[i], "-c") == 0) {
      parse_confg(&_config, argv[++i]);
      readConfig = true;
    } else if (strcmp(argv[i], "-lm") == 0) {
      logMotion = true;
    }
  }

  if (readConfig != true) {
    fprintf(stderr, "Error: Must pass config file on cmd line, example:-\n    %s -c %s.conf.\n", argv[0], argv[0]);
    exit(EXIT_FAILURE);
  }
  if (_config.numMotions <= 0) {
    fprintf(stderr, "Error: No motion actions found, please check config file.\n");
    cleanup();
    exit(EXIT_FAILURE);
  }

#ifdef LOGGING_ENABLED
  print_config(&_config);
#endif

  signal(SIGINT, intHandler);

  if (regcomp(&_config.regexCompiled, _config.regexString, REG_EXTENDED)) {
    fprintf(stderr, "Error: Could not compile regular expression. %s\n", strerror(errno));
    cleanup();
    exit(EXIT_FAILURE);
  };

  generate_mqtt_id(_config.mqtt_ID, MQTT_ID_LEN);

  mg_mgr_init(&mgr, NULL);
  LOG("Connecting to mqtt at '%s'\n", _config.mqtt_address);
  start_mqtt(&mgr);

  _umHandles.trigger = inotify_init();
  if (_umHandles.trigger < 0) {
    fprintf(stderr, "ERROR Failed to register monitor with kernel. %s\n", strerror(errno));
    cleanup();
    exit(EXIT_FAILURE);
  }

  _umHandles.descriptor = inotify_add_watch(_umHandles.trigger, _config.watchDir, IN_MODIFY);
  _umHandles.logfile = open_log(false);

  // Set watch trigger for non-blocking poll
  struct pollfd fds[1];
  fds[0].fd = _umHandles.trigger;
  fds[0].events = POLLIN;

  fprintf(stderr, "Monotoring log %s, events to mqtt %s\n", _config.log_file_to_monitor, _config.mqtt_address);

  while (1) {
    int i = 0;

    if ( _mqtt_status == mqttstopped) {
      LOG("Reset connections\n");
      // Should put a pause in here after first try failed.
      // Not putting it in yet untill debugged mqtt random disconnects.
      start_mqtt(&mgr);
    }
    mg_mgr_poll(&mgr, 0);

    retval = poll(fds, 1, 1000);
    if (retval == -1)
      fprintf(stderr, "Warning, system call poll() failed");
    else if (!retval) { // trigger timeout.
      DLOG(".");
      continue;
    } else { // trigger file action.
      DLOG(":");
    }

    length = read(_umHandles.trigger, buffer, BUF_LEN);

    if (length < 0) {
      fprintf(stderr, "Warning system call read() failed");
    } else if (length == 0) {
      fprintf(stderr, "Warning system call read() returned blank");
    }

    while (i < length) {
      struct inotify_event *event = (struct inotify_event *)&buffer[i];
      if (event->len) {
        if (event->mask & IN_MODIFY) {
          if (!(event->mask & IN_ISDIR)) {
            // LOG("The file %s was modified.\n", event->name );
            if (strcmp(event->name, _config.watchFile) == 0) {
              if (action_log_changes(_umHandles.logfile, logMotion) <= 0) {
                fprintf(stderr, "No lines read, logfile may have been rotated, re-opening:-\n");
                close_log(_umHandles.logfile);
                _umHandles.logfile = open_log(true);
                action_log_changes(_umHandles.logfile, logMotion);
              }
            }
          }
        }
      }
      i += EVENT_SIZE + event->len;
    }
  }

  cleanup();
  
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x8048, %rsp           # imm = 0x8048
movq %rsi, %rbx
movl %edi, %ebp
pushq $0x1
popq %r13
xorl %r14d, %r14d
leaq 0x1022e(%rip), %r15     # 0x140f2
xorl %r12d, %r12d
cmpl %ebp, %r13d
jge 0x3f12
movslq %r13d, %rax
movq (%rbx,%rax,8), %rdi
cmpb $0x2d, (%rdi)
jne 0x3ee4
cmpb $0x63, 0x1(%rdi)
jne 0x3ee4
cmpb $0x0, 0x2(%rdi)
je 0x3ef9
movq %r15, %rsi
callq 0x3310
testl %eax, %eax
sete %al
orb %al, %r12b
incl %r13d
jmp 0x3ec7
incl %r13d
movq 0x8(%rbx,%rax,8), %rsi
leaq 0x1a4a8(%rip), %rdi     # 0x1e3b0
callq 0x12d7e
movb $0x1, %r14b
jmp 0x3ef4
testb $0x1, %r14b
je 0x414c
cmpl $0x0, 0x1a665(%rip)     # 0x1e588
jle 0x416c
leaq -0x21a(%rip), %rsi      # 0x3d16
pushq $0x2
popq %rdi
callq 0x3320
movq 0x1a499(%rip), %rsi     # 0x1e3d8
leaq 0x1a4c2(%rip), %rdi     # 0x1e408
pushq $0x1
popq %rdx
callq 0x32a0
testl %eax, %eax
jne 0x418a
leaq 0x1a483(%rip), %rdi     # 0x1e3e0
pushq $0x14
popq %rsi
callq 0x38e4
leaq 0x8(%rsp), %rbx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x77d9
movq %rbx, %rdi
callq 0x39fe
callq 0x33a0
movl %eax, 0x1a611(%rip)     # 0x1e598
testl %eax, %eax
js 0x41a9
movq 0x1a462(%rip), %rsi     # 0x1e3f8
pushq $0x2
popq %rdx
movl %eax, %edi
callq 0x3410
movl %eax, 0x1a5fa(%rip)     # 0x1e5a0
xorl %edi, %edi
callq 0x3b56
movq %rax, 0x1a5dc(%rip)     # 0x1e590
movl 0x1a5de(%rip), %eax     # 0x1e598
movq %rsp, %rcx
movl %eax, (%rcx)
movw $0x1, 0x4(%rcx)
movq 0x1a024(%rip), %rax     # 0x1dff0
movq (%rax), %rdi
movq 0x1a3fa(%rip), %rdx     # 0x1e3d0
movq 0x1a3d3(%rip), %rcx     # 0x1e3b0
leaq 0x101f8(%rip), %rsi     # 0x141dc
xorl %eax, %eax
callq 0x3340
leaq 0x8(%rsp), %r14
movl $0x40000002, %ebp       # imm = 0x40000002
movzbl %r12b, %r12d
andl $0x1, %r12d
cmpl $0x2, 0x1a34c(%rip)     # 0x1e350
jne 0x400e
movq %r14, %rdi
callq 0x39fe
movq %r14, %rdi
xorl %esi, %esi
callq 0x7a12
movq %rsp, %rdi
pushq $0x1
popq %rsi
movl $0x3e8, %edx            # imm = 0x3E8
callq 0x34b0
testl %eax, %eax
je 0x3ffd
cmpl $-0x1, %eax
je 0x410a
movl 0x1a55d(%rip), %edi     # 0x1e598
movl $0x8000, %edx           # imm = 0x8000
leaq 0x40(%rsp), %rsi
callq 0x32d0
movq %rax, %r13
testl %r13d, %r13d
js 0x40f5
je 0x412b
xorl %ebx, %ebx
cmpl %r13d, %ebx
jge 0x3ffd
movslq %ebx, %rax
movl 0x4c(%rsp,%rax), %r15d
testl %r15d, %r15d
je 0x40a9
leaq (%rsp,%rax), %rdi
addq $0x40, %rdi
movl 0x4(%rdi), %eax
andl %ebp, %eax
cmpl $0x2, %eax
jne 0x40a9
addq $0x10, %rdi
movq 0x1a373(%rip), %rsi     # 0x1e400
callq 0x3310
testl %eax, %eax
jne 0x40a9
movq 0x1a4f3(%rip), %rdi     # 0x1e590
movl %r12d, %esi
callq 0x3d3f
testl %eax, %eax
jle 0x40b1
addl %r15d, %ebx
addl $0x10, %ebx
jmp 0x405e
movq 0x19f38(%rip), %rax     # 0x1dff0
movq (%rax), %rcx
leaq 0x101af(%rip), %rdi     # 0x14271
pushq $0x3b
popq %rsi
pushq $0x1
popq %rdx
callq 0x35e0
movq 0x1a4bc(%rip), %rdi     # 0x1e590
callq 0x3b3c
pushq $0x1
popq %rdi
callq 0x3b56
movq %rax, 0x1a4a8(%rip)     # 0x1e590
movq %rax, %rdi
movl %r12d, %esi
callq 0x3d3f
jmp 0x40a9
movq 0x19ef4(%rip), %rax     # 0x1dff0
movq (%rax), %rcx
leaq 0x1011f(%rip), %rdi     # 0x14225
pushq $0x21
jmp 0x413e
movq 0x19edf(%rip), %rax     # 0x1dff0
movq (%rax), %rcx
leaq 0x100e7(%rip), %rdi     # 0x14202
pushq $0x22
popq %rsi
pushq $0x1
popq %rdx
callq 0x35e0
jmp 0x4035
movq 0x19ebe(%rip), %rax     # 0x1dff0
movq (%rax), %rcx
leaq 0x1010b(%rip), %rdi     # 0x14247
pushq $0x29
popq %rsi
pushq $0x1
popq %rdx
callq 0x35e0
jmp 0x405c
movq 0x19e9d(%rip), %rax     # 0x1dff0
movq (%rax), %rdi
movq (%rbx), %rcx
leaq 0xff96(%rip), %rsi      # 0x140f6
movq %rcx, %rdx
xorl %eax, %eax
callq 0x3340
jmp 0x41d8
movq 0x19e7d(%rip), %rax     # 0x1dff0
movq (%rax), %rcx
leaq 0xffc1(%rip), %rdi      # 0x1413e
pushq $0x3a
popq %rsi
pushq $0x1
popq %rdx
callq 0x35e0
jmp 0x41d3
movq 0x19e5f(%rip), %rax     # 0x1dff0
movq (%rax), %rbx
callq 0x30a0
movl (%rax), %edi
callq 0x3600
leaq 0xffd2(%rip), %rsi      # 0x14179
jmp 0x41c6
movq 0x19e40(%rip), %rax     # 0x1dff0
movq (%rax), %rbx
callq 0x30a0
movl (%rax), %edi
callq 0x3600
leaq 0xffe4(%rip), %rsi      # 0x141aa
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x3340
callq 0x3cc8
pushq $0x1
popq %rdi
callq 0x35b0