// File: /workspace/github_c_makefile_star2_O2/Matthias-Wandel[P]imgcomp/src/start_camera_prog.c
// Function: DoMotionRun
void DoMotionRun(int SawMotion)
{
    int NowSec = (int) time(NULL);
    
    static int LastMotion = 0;
    static int LightOn;
    static pid_t child_pid = 0;
    char CmdCopy[200];

    //fprintf(Log, "DoMotionRun %d %d %d\n",SawMotion, SinceLightChange, NowSec-LastMotion);

    if (child_pid > 0){
        // Check if child has exited.
        pid_t r = waitpid(child_pid, NULL, WNOHANG);
        if (r == child_pid || r == -1){
            child_pid = 0;
            fprintf(Log,"Motionrun Child exited %d\n",r);
        }else{
            fprintf(Log,"Child still running  r=%d\n",r);
        }
    }

    // Ignore "motion" events for a few seconds after we hit the light switch.
    SinceLightChange += 1;
    if (SawMotion || NowSec-LastMotion <= 2){
        MotionAccumulate += 5;
        if (MotionAccumulate > 1000) MotionAccumulate = 1000;
    }else{
        MotionAccumulate -= 1;
        if (MotionAccumulate < 0) MotionAccumulate = 0;
    }
    //printf("Ma = %4d\n",MotionAccumulate);    
        
    if (SinceLightChange > 3 && SawMotion){
		//if (!LightOn && NowSec - LastMotion > 2) fprintf(Log, "delayed light on\n");
        if (!LightOn && NowSec - LastMotion <= 2){
			// Want two motion events close together to avoid false triggers
			// which can happen on lighting changes or camera artifacts.
            if (lighton_run[0]){
                if (child_pid <= 0){
                    fprintf(Log, "Turn light ON\n");
                    strncpy(CmdCopy, lighton_run, 200);
                    child_pid = do_launch_program(CmdCopy);
                    SinceLightChange = 0;
                    LightOn = 1;
                }else{
                    fprintf(Log,"Turn lights ON (wait child exit first)\n");
                }
            }else{
                LightOn = 1;
            }
        }
        LastMotion = NowSec;
    }else if (LightOn){
        // Compute how long the light should be left on.
        int mm = MotionAccumulate; if (mm > 500) mm = 500;
        mm -= 30; if (mm < 0) mm = 0;
        
        int timeout = lightoff_min + (lightoff_max-lightoff_min)*mm/500;
        //printf("Ma=%4d  timeout=%d (%d-%d)\n",MotionAccumulate, timeout, lightoff_min,lightoff_max);
        
        if ((NowSec-LastMotion) > timeout){
            if (lightoff_run[0]){
                if (child_pid <= 0){
                    fprintf(Log, "Turn light OFF (%d sec timeout)\n",timeout);
                    strncpy(CmdCopy, lightoff_run, 200);
                    child_pid = do_launch_program(CmdCopy);
                    SinceLightChange = 0;
                    LightOn = 0;
                }else{
                    fprintf(Log,"Turn light OFF (wait for child exit first)\n");
                }
            }else{
                LightOn = 0;
            }
        }
    }
}

// Assembly:
pushq %rbp
pushq %r14
pushq %rbx
subq $0xd0, %rsp
movl %edi, %ebp
xorl %edi, %edi
callq 0x3310
movq %rax, %rbx
movl 0xbbac(%rip), %edi      # 0x14684
testl %edi, %edi
jle 0x8b1d
pushq $0x1
popq %rdx
xorl %esi, %esi
callq 0x3420
cmpl $-0x1, %eax
je 0x8afc
cmpl 0xbb93(%rip), %eax      # 0x14684
je 0x8afc
leaq 0x54e8(%rip), %rsi      # 0xdfe2
jmp 0x8b0a
andl $0x0, 0xbb81(%rip)      # 0x14684
leaq 0x54bd(%rip), %rsi      # 0xdfc7
leaq 0xa4b7(%rip), %rcx      # 0x12fc8
movq (%rcx), %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0x32a0
movl 0xbb65(%rip), %ecx      # 0x14688
leal 0x1(%rcx), %eax
movl %eax, 0xbb5c(%rip)      # 0x14688
testl %ebp, %ebp
jne 0x8b41
movl %ebx, %eax
subl 0xbb44(%rip), %eax      # 0x1467c
cmpl $0x3, %eax
jge 0x8bc5
testl %ebp, %ebp
sete %dl
movl 0xbb40(%rip), %esi      # 0x1468c
movl $0x3e3, %eax            # imm = 0x3E3
cmpl %eax, %esi
cmovll %esi, %eax
addl $0x5, %eax
movl %eax, 0xbb2d(%rip)      # 0x1468c
cmpl $0x3, %ecx
setl %cl
orb %dl, %cl
jne 0x8bdc
cmpb $0x0, 0xbb10(%rip)      # 0x14680
jne 0x8cf2
movl %ebx, %eax
subl 0xbafe(%rip), %eax      # 0x1467c
cmpl $0x2, %eax
jg 0x8cf2
leaq 0xa522(%rip), %rax      # 0x130b0
cmpb $0x0, (%rax)
je 0x8ceb
cmpl $0x0, 0xbae6(%rip)      # 0x14684
leaq 0xa423(%rip), %rax      # 0x12fc8
movq (%rax), %rcx
jle 0x8cad
leaq 0x5457(%rip), %rdi      # 0xe00c
pushq $0x27
popq %rsi
pushq $0x1
popq %rdx
callq 0x34b0
jmp 0x8cf2
movl 0xbac1(%rip), %ecx      # 0x1468c
cmpl $0x2, %ecx
pushq $0x1
popq %rax
cmovgel %ecx, %eax
decl %eax
movl %eax, 0xbab0(%rip)      # 0x1468c
cmpb $0x1, 0xba9d(%rip)      # 0x14680
jne 0x8cf8
movl $0x1f4, %ecx            # imm = 0x1F4
cmpl %ecx, %eax
cmovgel %ecx, %eax
cmpl $0x1f, %eax
pushq $0x1e
popq %rdx
cmovgel %eax, %edx
addl $-0x1e, %edx
leaq 0x96b6(%rip), %rax      # 0x122bc
movl (%rax), %esi
leaq 0x96b1(%rip), %rax      # 0x122c0
movl (%rax), %eax
subl %esi, %eax
imull %edx, %eax
cltd
idivl %ecx
addl %esi, %eax
subl 0xba5b(%rip), %ebx      # 0x1467c
cmpl %eax, %ebx
jle 0x8cf8
leaq 0xa550(%rip), %rcx      # 0x13180
cmpb $0x0, (%rcx)
je 0x8ca4
cmpl $0x0, 0xba48(%rip)      # 0x14684
leaq 0xa385(%rip), %rcx      # 0x12fc8
movq (%rcx), %rcx
jle 0x8c65
leaq 0x5406(%rip), %rdi      # 0xe055
pushq $0x2b
popq %rsi
pushq $0x1
popq %rdx
addq $0xd0, %rsp
popq %rbx
popq %r14
popq %rbp
jmp 0x34b0
leaq 0x53c8(%rip), %rsi      # 0xe034
movq %rcx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0x32a0
leaq 0xa501(%rip), %rsi      # 0x13180
movq %rsp, %rbx
movl $0xc8, %edx
movq %rbx, %rdi
callq 0x3090
movq %rbx, %rdi
callq 0x89da
movl %eax, 0xb9e7(%rip)      # 0x14684
andl $0x0, 0xb9e4(%rip)      # 0x14688
movb $0x0, 0xb9d5(%rip)      # 0x14680
jmp 0x8cf8
leaq 0x5349(%rip), %rdi      # 0xdffd
pushq $0xe
popq %rsi
pushq $0x1
popq %rdx
callq 0x34b0
leaq 0xa3ea(%rip), %rsi      # 0x130b0
movq %rsp, %r14
movl $0xc8, %edx
movq %r14, %rdi
callq 0x3090
movq %r14, %rdi
callq 0x89da
movl %eax, 0xb9a0(%rip)      # 0x14684
andl $0x0, 0xb99d(%rip)      # 0x14688
movb $0x1, 0xb98e(%rip)      # 0x14680
movl %ebx, 0xb984(%rip)      # 0x1467c
addq $0xd0, %rsp
popq %rbx
popq %r14
popq %rbp
retq