// File: /workspace/github_c_makefile_star2_O3/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
cmpl $0x2, %edi
jl 0x4366
movl %edi, %ebp
movl $0x1, %r12d
xorl %r13d, %r13d
leaq 0x11044(%rip), %r15     # 0x150f2
xorl %r14d, %r14d
movslq %r12d, %rax
movq (%rbx,%rax,8), %rdi
cmpb $0x2d, (%rdi)
jne 0x40c9
cmpb $0x63, 0x1(%rdi)
jne 0x40c9
cmpb $0x0, 0x2(%rdi)
je 0x40e3
movq %r15, %rsi
callq 0x3310
testl %eax, %eax
sete %al
orb %al, %r13b
incl %r12d
cmpl %ebp, %r12d
jl 0x40b1
jmp 0x40fd
leal 0x1(%rax), %r12d
movq 0x8(%rbx,%rax,8), %rsi
leaq 0x1a2bd(%rip), %rdi     # 0x1e3b0
callq 0x1497a
movb $0x1, %r14b
jmp 0x40d9
testb $0x1, %r14b
je 0x4366
cmpl $0x0, 0x1a47a(%rip)     # 0x1e588
jle 0x4386
leaq -0x20b(%rip), %rsi      # 0x3f10
movl $0x2, %edi
callq 0x3320
movq 0x1a2ac(%rip), %rsi     # 0x1e3d8
leaq 0x1a2d5(%rip), %rdi     # 0x1e408
movl $0x1, %edx
callq 0x32a0
testl %eax, %eax
jne 0x43a8
leaq 0x1a294(%rip), %rdi     # 0x1e3e0
movl $0x14, %esi
callq 0x3c49
leaq 0x8(%rsp), %rbx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x7b9d
movq %rbx, %rdi
callq 0x3d65
callq 0x33a0
movl %eax, 0x1a420(%rip)     # 0x1e598
testl %eax, %eax
js 0x43c7
movq 0x1a271(%rip), %rsi     # 0x1e3f8
movl %eax, %edi
movl $0x2, %edx
callq 0x3410
movl %eax, 0x1a407(%rip)     # 0x1e5a0
xorl %edi, %edi
callq 0x3de7
movq %rax, 0x1a3e9(%rip)     # 0x1e590
movl 0x1a3eb(%rip), %eax     # 0x1e598
movq %rsp, %rcx
movl %eax, (%rcx)
movw $0x1, 0x4(%rcx)
movq 0x19e31(%rip), %rax     # 0x1dff0
movq (%rax), %rdi
movq 0x1a207(%rip), %rdx     # 0x1e3d0
movq 0x1a1e0(%rip), %rcx     # 0x1e3b0
leaq 0x11005(%rip), %rsi     # 0x151dc
xorl %eax, %eax
callq 0x3340
leaq 0x8(%rsp), %r14
movl $0x40000002, %r15d      # imm = 0x40000002
movzbl %r13b, %ebp
andl $0x1, %ebp
cmpl $0x2, 0x1a159(%rip)     # 0x1e350
jne 0x4201
movq %r14, %rdi
callq 0x3d65
movq %r14, %rdi
xorl %esi, %esi
callq 0x7e0e
movl $0x1, %esi
movq %rsp, %rdi
movl $0x3e8, %edx            # imm = 0x3E8
callq 0x34b0
testl %eax, %eax
je 0x41f0
cmpl $-0x1, %eax
je 0x431c
movl 0x1a368(%rip), %edi     # 0x1e598
movl $0x8000, %edx           # imm = 0x8000
leaq 0x40(%rsp), %rsi
callq 0x32d0
movq %rax, %r13
testl %r13d, %r13d
js 0x42f7
je 0x4341
xorl %r12d, %r12d
movslq %r12d, %rax
movl 0x4c(%rsp,%rax), %ebx
testl %ebx, %ebx
je 0x4298
leaq (%rsp,%rax), %rdi
addq $0x40, %rdi
movl 0x4(%rdi), %eax
andl %r15d, %eax
cmpl $0x2, %eax
jne 0x4298
addq $0x10, %rdi
movq 0x1a183(%rip), %rsi     # 0x1e400
callq 0x3310
testl %eax, %eax
jne 0x4298
movq 0x1a303(%rip), %rdi     # 0x1e590
movl %ebp, %esi
callq 0x3f3d
testl %eax, %eax
jle 0x42a9
addl %ebx, %r12d
addl $0x10, %r12d
cmpl %r13d, %r12d
jl 0x4254
jmp 0x41f0
movq 0x19d40(%rip), %rax     # 0x1dff0
movq (%rax), %rcx
movl $0x3b, %esi
movl $0x1, %edx
leaq 0x10fad(%rip), %rdi     # 0x15271
callq 0x35e0
movq 0x1a2c0(%rip), %rdi     # 0x1e590
testq %rdi, %rdi
je 0x42da
callq 0x3160
movl $0x1, %edi
callq 0x3de7
movq %rax, 0x1a2a5(%rip)     # 0x1e590
movq %rax, %rdi
movl %ebp, %esi
callq 0x3f3d
jmp 0x4298
movq 0x19cf2(%rip), %rax     # 0x1dff0
movq (%rax), %rcx
movl $0x21, %esi
movl $0x1, %edx
leaq 0x10f13(%rip), %rdi     # 0x15225
callq 0x35e0
jmp 0x41f0
movq 0x19ccd(%rip), %rax     # 0x1dff0
movq (%rax), %rcx
movl $0x22, %esi
movl $0x1, %edx
leaq 0x10ecb(%rip), %rdi     # 0x15202
callq 0x35e0
jmp 0x422a
movq 0x19ca8(%rip), %rax     # 0x1dff0
movq (%rax), %rcx
movl $0x29, %esi
movl $0x1, %edx
leaq 0x10eeb(%rip), %rdi     # 0x15247
callq 0x35e0
jmp 0x41f0
movq 0x19c83(%rip), %rax     # 0x1dff0
movq (%rax), %rdi
movq (%rbx), %rcx
leaq 0x10d7c(%rip), %rsi     # 0x150f6
movq %rcx, %rdx
xorl %eax, %eax
callq 0x3340
jmp 0x43f6
movq 0x19c63(%rip), %rax     # 0x1dff0
movq (%rax), %rcx
leaq 0x10da7(%rip), %rdi     # 0x1513e
movl $0x3a, %esi
movl $0x1, %edx
callq 0x35e0
jmp 0x43f1
movq 0x19c41(%rip), %rax     # 0x1dff0
movq (%rax), %rbx
callq 0x30a0
movl (%rax), %edi
callq 0x3600
leaq 0x10db4(%rip), %rsi     # 0x15179
jmp 0x43e4
movq 0x19c22(%rip), %rax     # 0x1dff0
movq (%rax), %rbx
callq 0x30a0
movl (%rax), %edi
callq 0x3600
leaq 0x10dc6(%rip), %rsi     # 0x151aa
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x3340
callq 0x3ebd
movl $0x1, %edi
callq 0x35b0