// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x8080, %rsp           # imm = 0x8080
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movb $0x0, -0x4d(%rbp)
movb $0x0, -0x8059(%rbp)
movl $0x1, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x8(%rbp), %eax
jge 0x4229
movq -0x10(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x1af2c(%rip), %rsi     # 0x1f0f2
callq 0x3340
cmpl $0x0, %eax
jne 0x41f6
movq -0x10(%rbp), %rax
movl -0x4c(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, -0x4c(%rbp)
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rsi
leaq 0x23295(%rip), %rdi     # 0x27480
callq 0x1da60
movb $0x1, -0x4d(%rbp)
jmp 0x421c
movq -0x10(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x1aeec(%rip), %rsi     # 0x1f0f5
callq 0x3340
cmpl $0x0, %eax
jne 0x421a
movb $0x1, -0x8059(%rbp)
jmp 0x421c
jmp 0x421e
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x41ab
movb -0x4d(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x1, %eax
je 0x4266
movq 0x22db3(%rip), %rax     # 0x26ff0
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rdx
movq -0x10(%rbp), %rax
movq (%rax), %rcx
leaq 0x1aea4(%rip), %rsi     # 0x1f0f9
movb $0x0, %al
callq 0x3370
movl $0x1, %edi
callq 0x35f0
cmpl $0x0, 0x233eb(%rip)     # 0x27658
jg 0x4296
movq 0x22d7a(%rip), %rax     # 0x26ff0
movq (%rax), %rdi
leaq 0x1aec1(%rip), %rsi     # 0x1f141
movb $0x0, %al
callq 0x3370
callq 0x3f50
movl $0x1, %edi
callq 0x35f0
movl $0x2, %edi
leaq -0x302(%rip), %rsi      # 0x3fa0
callq 0x3350
movq 0x231fa(%rip), %rsi     # 0x274a8
leaq 0x231cb(%rip), %rdi     # 0x27480
addq $0x58, %rdi
movl $0x1, %edx
callq 0x32c0
cmpl $0x0, %eax
je 0x430c
movq 0x22d21(%rip), %rax     # 0x26ff0
movq (%rax), %rax
movq %rax, -0x8078(%rbp)
callq 0x3090
movl (%rax), %edi
callq 0x3650
movq -0x8078(%rbp), %rdi
movq %rax, %rdx
leaq 0x1ae86(%rip), %rsi     # 0x1f17c
movb $0x0, %al
callq 0x3370
callq 0x3f50
movl $0x1, %edi
callq 0x35f0
leaq 0x2316d(%rip), %rdi     # 0x27480
addq $0x30, %rdi
movl $0x14, %esi
callq 0x39b0
leaq -0x48(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0xac90
leaq -0x48(%rbp), %rdi
callq 0x3b40
callq 0x33d0
movl %eax, 0x23326(%rip)     # 0x27668
cmpl $0x0, 0x2331f(%rip)     # 0x27668
jge 0x438f
movq 0x22c9e(%rip), %rax     # 0x26ff0
movq (%rax), %rax
movq %rax, -0x8080(%rbp)
callq 0x3090
movl (%rax), %edi
callq 0x3650
movq -0x8080(%rbp), %rdi
movq %rax, %rdx
leaq 0x1ae34(%rip), %rsi     # 0x1f1ad
movb $0x0, %al
callq 0x3370
callq 0x3f50
movl $0x1, %edi
callq 0x35f0
movl 0x232d3(%rip), %edi     # 0x27668
movq 0x2312c(%rip), %rsi     # 0x274c8
movl $0x2, %edx
callq 0x3450
movl %eax, 0x232c0(%rip)     # 0x2766c
xorl %edi, %edi
callq 0x3d60
movq %rax, 0x232a6(%rip)     # 0x27660
movl 0x232a8(%rip), %eax     # 0x27668
movl %eax, -0x8064(%rbp)
movw $0x1, -0x8060(%rbp)
movq 0x22c1a(%rip), %rax     # 0x26ff0
movq (%rax), %rdi
movq 0x230c0(%rip), %rdx     # 0x274a0
movq 0x23099(%rip), %rcx     # 0x27480
leaq 0x1adf1(%rip), %rsi     # 0x1f1df
movb $0x0, %al
callq 0x3370
movl $0x0, -0x8068(%rbp)
cmpl $0x2, 0x22f6a(%rip)     # 0x27370
jne 0x4411
leaq -0x48(%rbp), %rdi
callq 0x3b40
leaq -0x48(%rbp), %rdi
xorl %esi, %esi
callq 0xb050
leaq -0x8064(%rbp), %rdi
movl $0x1, %esi
movl $0x3e8, %edx            # imm = 0x3E8
callq 0x34f0
movl %eax, -0x8058(%rbp)
cmpl $-0x1, -0x8058(%rbp)
jne 0x445b
movq 0x22ba8(%rip), %rax     # 0x26ff0
movq (%rax), %rdi
leaq 0x1adb3(%rip), %rsi     # 0x1f205
movb $0x0, %al
callq 0x3370
jmp 0x446a
cmpl $0x0, -0x8058(%rbp)
jne 0x4466
jmp 0x43f5
jmp 0x4468
jmp 0x446a
movl 0x231f8(%rip), %edi     # 0x27668
leaq -0x8050(%rbp), %rsi
movl $0x8000, %edx           # imm = 0x8000
callq 0x32f0
movl %eax, -0x8054(%rbp)
cmpl $0x0, -0x8054(%rbp)
jge 0x44aa
movq 0x22b59(%rip), %rax     # 0x26ff0
movq (%rax), %rdi
leaq 0x1ad87(%rip), %rsi     # 0x1f228
movb $0x0, %al
callq 0x3370
jmp 0x44cd
cmpl $0x0, -0x8054(%rbp)
jne 0x44cb
movq 0x22b36(%rip), %rax     # 0x26ff0
movq (%rax), %rdi
leaq 0x1ad86(%rip), %rsi     # 0x1f24a
movb $0x0, %al
callq 0x3370
jmp 0x44cd
jmp 0x44cf
movl -0x8068(%rbp), %eax
cmpl -0x8054(%rbp), %eax
jge 0x45e9
movslq -0x8068(%rbp), %rcx
leaq -0x8050(%rbp), %rax
addq %rcx, %rax
movq %rax, -0x8070(%rbp)
movq -0x8070(%rbp), %rax
cmpl $0x0, 0xc(%rax)
je 0x45c4
movq -0x8070(%rbp), %rax
movl 0x4(%rax), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x45c2
movq -0x8070(%rbp), %rax
movl 0x4(%rax), %eax
andl $0x40000000, %eax       # imm = 0x40000000
cmpl $0x0, %eax
jne 0x45c0
movq -0x8070(%rbp), %rdi
addq $0x10, %rdi
movq 0x22f86(%rip), %rsi     # 0x274d0
callq 0x3340
cmpl $0x0, %eax
jne 0x45be
movq 0x23105(%rip), %rdi     # 0x27660
movb -0x8059(%rbp), %al
andb $0x1, %al
movzbl %al, %esi
callq 0x3fd0
cmpl $0x0, %eax
jg 0x45bc
movq 0x22a79(%rip), %rax     # 0x26ff0
movq (%rax), %rdi
leaq 0x1acf3(%rip), %rsi     # 0x1f274
movb $0x0, %al
callq 0x3370
movq 0x230d1(%rip), %rdi     # 0x27660
callq 0x3d20
movl $0x1, %edi
callq 0x3d60
movq %rax, 0x230bb(%rip)     # 0x27660
movq 0x230b4(%rip), %rdi     # 0x27660
movb -0x8059(%rbp), %al
andb $0x1, %al
movzbl %al, %esi
callq 0x3fd0
jmp 0x45be
jmp 0x45c0
jmp 0x45c2
jmp 0x45c4
movq -0x8070(%rbp), %rax
movl 0xc(%rax), %eax
movl %eax, %ecx
addq $0x10, %rcx
movslq -0x8068(%rbp), %rax
addq %rcx, %rax
movl %eax, -0x8068(%rbp)
jmp 0x44cf
jmp 0x43f5
nop