// File: /workspace/github_c_makefile_star2_O1/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 $0x8058, %rsp           # imm = 0x8058
movq %rsi, %rbx
cmpl $0x2, %edi
jl 0x416c
movl %edi, %r15d
xorl %ebp, %ebp
movl $0x1, %r12d
movl $0x0, 0xc(%rsp)
movslq %r12d, %r14
movq (%rbx,%r14,8), %r13
movq %r13, %rdi
leaq 0x10fdb(%rip), %rsi     # 0x150f2
callq 0x3310
testl %eax, %eax
je 0x413f
movq %r13, %rdi
leaq 0x10fcb(%rip), %rsi     # 0x150f5
callq 0x3310
testl %eax, %eax
movzbl %bpl, %ebp
movl $0x1, %eax
cmovel %eax, %ebp
jmp 0x415a
leal 0x1(%r14), %r12d
movq 0x8(%rbx,%r14,8), %rsi
leaq 0x1a261(%rip), %rdi     # 0x1e3b0
callq 0x1440c
movb $0x1, %al
movl %eax, 0xc(%rsp)
incl %r12d
cmpl %r15d, %r12d
jl 0x4106
testb $0x1, 0xc(%rsp)
sete %al
jmp 0x4170
movb $0x1, %al
xorl %ebp, %ebp
testb %al, %al
jne 0x43d3
cmpl $0x0, 0x1a409(%rip)     # 0x1e588
jle 0x43f3
leaq -0x221(%rip), %rsi      # 0x3f6b
movl $0x2, %edi
callq 0x3320
movq 0x1a23b(%rip), %rsi     # 0x1e3d8
leaq 0x1a264(%rip), %rdi     # 0x1e408
movl $0x1, %edx
callq 0x32a0
testl %eax, %eax
jne 0x4415
leaq 0x1a223(%rip), %rdi     # 0x1e3e0
movl $0x14, %esi
callq 0x3ca1
leaq 0x18(%rsp), %rbx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x7c84
movq %rbx, %rdi
callq 0x3dbd
callq 0x33a0
movl %eax, 0x1a3af(%rip)     # 0x1e598
testl %eax, %eax
js 0x4434
movq 0x1a200(%rip), %rsi     # 0x1e3f8
movl %eax, %edi
movl $0x2, %edx
callq 0x3410
movl %eax, 0x1a396(%rip)     # 0x1e5a0
xorl %edi, %edi
callq 0x3e3f
movq %rax, 0x1a378(%rip)     # 0x1e590
movl 0x1a37a(%rip), %eax     # 0x1e598
leaq 0x10(%rsp), %rcx
movl %eax, (%rcx)
movw $0x1, 0x4(%rcx)
movq 0x19dbe(%rip), %rax     # 0x1dff0
movq (%rax), %rdi
movq 0x1a194(%rip), %rdx     # 0x1e3d0
movq 0x1a16d(%rip), %rcx     # 0x1e3b0
leaq 0x10f95(%rip), %rsi     # 0x151df
xorl %eax, %eax
callq 0x3340
leaq 0x18(%rsp), %r14
movl $0x40000002, %r15d      # imm = 0x40000002
movzbl %bpl, %ebp
andl $0x1, %ebp
cmpl $0x2, 0x1a0e6(%rip)     # 0x1e350
jne 0x4274
movq %r14, %rdi
callq 0x3dbd
movq %r14, %rdi
xorl %esi, %esi
callq 0x7f09
movl $0x1, %esi
leaq 0x10(%rsp), %rdi
movl $0x3e8, %edx            # imm = 0x3E8
callq 0x34b0
testl %eax, %eax
je 0x4263
cmpl $-0x1, %eax
je 0x4389
movl 0x1a2f3(%rip), %edi     # 0x1e598
movl $0x8000, %edx           # imm = 0x8000
leaq 0x50(%rsp), %rsi
callq 0x32d0
movq %rax, %r13
testl %r13d, %r13d
js 0x436c
je 0x43ae
testl %r13d, %r13d
jle 0x4263
xorl %r12d, %r12d
movslq %r12d, %rax
movl 0x5c(%rsp,%rax), %ebx
testl %ebx, %ebx
je 0x4312
leaq (%rsp,%rax), %rdi
addq $0x50, %rdi
movl 0x4(%rdi), %eax
andl %r15d, %eax
cmpl $0x2, %eax
jne 0x4312
addq $0x10, %rdi
movq 0x1a109(%rip), %rsi     # 0x1e400
callq 0x3310
testl %eax, %eax
jne 0x4312
movq 0x1a289(%rip), %rdi     # 0x1e590
movl %ebp, %esi
callq 0x3f98
testl %eax, %eax
jle 0x4323
addl %ebx, %r12d
addl $0x10, %r12d
cmpl %r13d, %r12d
jl 0x42ce
jmp 0x4263
movq 0x19cc6(%rip), %rax     # 0x1dff0
movq (%rax), %rcx
movl $0x3b, %esi
movl $0x1, %edx
leaq 0x10f36(%rip), %rdi     # 0x15274
callq 0x35e0
movq 0x1a246(%rip), %rdi     # 0x1e590
callq 0x3e25
movl $0x1, %edi
callq 0x3e3f
movq %rax, 0x1a230(%rip)     # 0x1e590
movq %rax, %rdi
movl %ebp, %esi
callq 0x3f98
jmp 0x4312
movq 0x19c7d(%rip), %rax     # 0x1dff0
movq (%rax), %rcx
movl $0x21, %esi
movl $0x1, %edx
leaq 0x10ea1(%rip), %rdi     # 0x15228
jmp 0x43c9
movq 0x19c60(%rip), %rax     # 0x1dff0
movq (%rax), %rcx
movl $0x22, %esi
movl $0x1, %edx
leaq 0x10e61(%rip), %rdi     # 0x15205
callq 0x35e0
jmp 0x429f
movq 0x19c3b(%rip), %rax     # 0x1dff0
movq (%rax), %rcx
movl $0x29, %esi
movl $0x1, %edx
leaq 0x10e81(%rip), %rdi     # 0x1524a
callq 0x35e0
jmp 0x42c6
movq 0x19c16(%rip), %rax     # 0x1dff0
movq (%rax), %rdi
movq (%rbx), %rcx
leaq 0x10d12(%rip), %rsi     # 0x150f9
movq %rcx, %rdx
xorl %eax, %eax
callq 0x3340
jmp 0x4463
movq 0x19bf6(%rip), %rax     # 0x1dff0
movq (%rax), %rcx
leaq 0x10d3d(%rip), %rdi     # 0x15141
movl $0x3a, %esi
movl $0x1, %edx
callq 0x35e0
jmp 0x445e
movq 0x19bd4(%rip), %rax     # 0x1dff0
movq (%rax), %rbx
callq 0x30a0
movl (%rax), %edi
callq 0x3600
leaq 0x10d4a(%rip), %rsi     # 0x1517c
jmp 0x4451
movq 0x19bb5(%rip), %rax     # 0x1dff0
movq (%rax), %rbx
callq 0x30a0
movl (%rax), %edi
callq 0x3600
leaq 0x10d5c(%rip), %rsi     # 0x151ad
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x3340
callq 0x3f1d
movl $0x1, %edi
callq 0x35b0
nopl (%rax)