// File: /workspace/github_c_makefile_star2_O2/Matthias-Wandel[P]imgcomp/src/main.c
// Function: DoDirectoryVideos
int DoDirectoryVideos(char * DirName)
{
    int a,ret;
    int infileindex;
    int alt = 0;

    Raspistill_restarted = 0;
    infileindex = strstr(VidDecomposeCmd, "<infile>")-VidDecomposeCmd;

    if (infileindex <= 0){
        fprintf(stderr, "Must specify '<infile>' as part of videodecomposecmd\n");
        exit(-1);
    }

    for (;;){
        DirEntry_t * FileNames;
        int NumEntries;
        char VidFileName[200];
        char FFCmd[300];
        int Saw_motion;
        time_t now;
        int VideoActive = 0;

        NumProcessed = 0;
        alt += 1;

        FileNames = GetSortedDir(DirName, &NumEntries);
        if (FileNames == NULL){
            fprintf(stderr, "Could not read dir %s\n",DirName);
            return 0;
        }

        now = time(NULL);
        if (NumEntries != 1){
            fprintf(Log,"%d files to process",NumEntries);
            fprintf(Log, "(%02d:%02d)...\n",(int)(now%3600/60), (int)(now%60));
        }
        for (a=0;a<NumEntries;a++){
            int age;

            age = (int)(now-FileNames[a].MTime);
            if (age < 1){
                VideoActive = 1; // Video files getting updated.
                if (alt & 1 || NumEntries > 1){
                    // Print waiting only every other time to cut down on log clutter.
                    fprintf(Log, "Vid '%s' aged %d %dk (Wait)\n",FileNames[a].FileName, age, FileNames[a].FileSize>>10);
                }
                continue;
            }else{
                fprintf(Log,"Vid '%s': %dk Extract keyframes (%02d:%02d)...\n", FileNames[a].FileName, FileNames[a].FileSize>>10,
                    (int)(now%3600)/60, (int)(now%60));
            }

            strcpy(VidFileName, CatPath(DirName, FileNames[a].FileName));
            strncpy(FFCmd, VidDecomposeCmd, infileindex);
            FFCmd[infileindex] = 0;
            strcpy(FFCmd+infileindex, VidFileName);
            strcat(FFCmd, VidDecomposeCmd+infileindex+8);

            // Use timestamp of video file to sequence output file names (so we'll have the respective times for those)
            unsigned seq = (unsigned)FileNames[a].MTime - 1000000000;
            sprintf(FFCmd+strlen(FFCmd), " -start_number %u %s/sf%%d.jpg",seq, TempDirName);

            errno = 0;
            ret = system(FFCmd);
            if (ret || errno){
                // A command can however fail without errno getting set or system returning an error.
                if (errno) perror("system");
                fprintf(Log, "Error on command %s\n",FFCmd);
                continue;
            }

            // Now should have some files in temp dir.
            Saw_motion = DoDirectoryFunc(TempDirName, 1);
            if (Saw_motion){
                char * DstName;
                char * Ext;
                fprintf(Log,"Vid has motion %d\n", Saw_motion);
                // Make filename, but don't copy the file.
                DstName = BackupImageFile(VidFileName, Saw_motion,1);

                Ext = strstr(DstName, ".h264");
                if (Ext) strcpy(Ext, ".mp4"); // Change xtension to .mp4

                if (DstName){
                    sprintf(FFCmd,"MP4Box -add %s \"%s\"",VidFileName, DstName);
                    errno = 0;
                    ret = system(FFCmd);
                    if (ret || errno){
                        if (errno) perror("system");
                        fprintf(Log, "Error on command %s\n",FFCmd);
                        continue;
                    }
                }
            }

            if (FollowDir){
                //fprintf(Log, "Delete video %s\n",VidFileName);
                unlink(VidFileName);
            }
        }
        FreeDir(FileNames, NumEntries); // Free up the whole directory structure.

        if (FollowDir){
            int b = manage_camera_prog(VideoActive);
            if (b) Raspistill_restarted = 1;
            if (LogToFile[0] != '\0') LogFileMaintain(0);
            sleep(1);
        }else{
            break;
        }

    }
    return a;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x258, %rsp            # imm = 0x258
movq %rdi, 0x28(%rsp)
leaq 0xef4c(%rip), %rax      # 0x12fd4
andl $0x0, (%rax)
leaq 0xf1ee(%rip), %r15      # 0x13280
leaq 0x6ff3(%rip), %rsi      # 0xb08c
movq %r15, %rdi
callq 0x3510
subl %r15d, %eax
testl %eax, %eax
jle 0x44a5
movl %eax, %eax
leaq (%rsp,%rax), %rcx
addq $0x60, %rcx
movq %rcx, 0x50(%rsp)
movq %rax, 0x58(%rsp)
addq %r15, %rax
addq $0x8, %rax
movq %rax, 0x48(%rsp)
movq $0x0, 0x20(%rsp)
andl $0x0, 0xe238(%rip)      # 0x12314
movq 0x28(%rsp), %rdi
leaq 0x14(%rsp), %rsi
callq 0x9138
testq %rax, %rax
je 0x4470
movq %rax, %r12
xorl %edi, %edi
callq 0x3310
movq %rax, %r13
movl 0x14(%rsp), %edx
cmpl $0x1, %edx
jne 0x413a
movq %r13, %rax
cqto
movl $0xe10, %ecx            # imm = 0xE10
idivq %rcx
movq %rdx, %rax
cwtd
movw $0x3c, %cx
idivw %cx
cwtl
movl %eax, 0x10(%rsp)
movq %r13, %rax
cqto
pushq $0x3c
popq %rcx
idivq %rcx
movq %rdx, 0x38(%rsp)
jmp 0x419b
leaq 0xee87(%rip), %rbx      # 0x12fc8
movq (%rbx), %rdi
leaq 0x6f97(%rip), %rsi      # 0xb0e2
xorl %eax, %eax
callq 0x32a0
movq (%rbx), %rdi
movq %r13, %rax
cqto
movl $0xe10, %ecx            # imm = 0xE10
idivq %rcx
movq %rdx, %rax
cwtd
movw $0x3c, %cx
idivw %cx
movswl %ax, %r8d
movq %r13, %rax
cqto
pushq $0x3c
popq %rcx
idivq %rcx
movq %rdx, %rcx
leaq 0x6fac(%rip), %rsi      # 0xb133
movl %r8d, 0x10(%rsp)
movl %r8d, %edx
movq %rcx, 0x38(%rsp)
xorl %eax, %eax
callq 0x32a0
movq 0x20(%rsp), %rax
incl %eax
movl %eax, 0x34(%rsp)
movq %r12, 0x40(%rsp)
addq $0x18, %r12
xorl %r14d, %r14d
movl $0x0, 0xc(%rsp)
movq %r13, 0x18(%rsp)
movslq 0x14(%rsp), %rsi
cmpq %rsi, %r14
jge 0x4417
movl -0x10(%r12), %eax
movl %r13d, %ecx
subl %eax, %ecx
testl %ecx, %ecx
jle 0x42e6
leaq 0xede2(%rip), %rax      # 0x12fc8
movq (%rax), %rdi
movl -0x18(%r12), %ecx
shrl $0xa, %ecx
leaq 0x6f1b(%rip), %rsi      # 0xb113
movq %r12, %rdx
movl 0x10(%rsp), %r8d
movq 0x38(%rsp), %r9
xorl %eax, %eax
callq 0x32a0
movq 0x28(%rsp), %rdi
movq %r12, %rsi
callq 0x8eb0
leaq 0x190(%rsp), %r13
movq %r13, %rdi
movq %rax, %rsi
callq 0x30a0
leaq 0x60(%rsp), %rbp
movq %rbp, %rdi
movq %r15, %rbx
movq %r15, %rsi
movq 0x58(%rsp), %r15
movq %r15, %rdx
callq 0x3090
movb $0x0, 0x60(%rsp,%r15)
movq 0x50(%rsp), %rdi
movq %r13, %rsi
callq 0x30a0
movq %rbp, %rdi
movq 0x48(%rsp), %rsi
callq 0x3480
movl -0x10(%r12), %r15d
movl $0xc4653600, %eax       # imm = 0xC4653600
addl %eax, %r15d
movq %rbp, %rdi
callq 0x3130
leaq (%rsp,%rax), %rdi
addq $0x60, %rdi
leaq 0x6eb8(%rip), %rsi      # 0xb143
movl %r15d, %edx
leaq 0xf0bb(%rip), %rcx      # 0x13350
xorl %eax, %eax
callq 0x3490
callq 0x3070
movq %rax, %r13
andl $0x0, (%rax)
movq %rbp, %rdi
callq 0x3150
movl (%r13), %ecx
testl %eax, %eax
je 0x4330
testl %ecx, %ecx
jne 0x4406
leaq 0xed02(%rip), %rax      # 0x12fc8
movq (%rax), %rdi
leaq 0x6e99(%rip), %rsi      # 0xb169
leaq 0x60(%rsp), %rdx
xorl %eax, %eax
callq 0x32a0
movq %rbx, %r15
movq 0x18(%rsp), %r13
jmp 0x4324
cmpl $0x2, %esi
setl %al
pushq $0x1
popq %rdx
movl %edx, 0xc(%rsp)
testb %al, 0x20(%rsp)
jne 0x4324
leaq 0xecc8(%rip), %rax      # 0x12fc8
movq (%rax), %rdi
movl -0x18(%r12), %r8d
shrl $0xa, %r8d
leaq 0x6de3(%rip), %rsi      # 0xb0f6
movq %r12, %rdx
xorl %eax, %eax
callq 0x32a0
pushq $0x1
popq %rax
movl %eax, 0xc(%rsp)
incq %r14
addq $0x50, %r12
jmp 0x41bf
testl %ecx, %ecx
jne 0x4406
leaq 0xf011(%rip), %rdi      # 0x13350
pushq $0x1
popq %rsi
callq 0x37cd
testl %eax, %eax
je 0x43e4
movl %eax, %r15d
leaq 0xec6f(%rip), %rax      # 0x12fc8
movq (%rax), %rdi
leaq 0x6e1b(%rip), %rsi      # 0xb17e
movl %r15d, %edx
xorl %eax, %eax
callq 0x32a0
leaq 0x190(%rsp), %rdi
movl %r15d, %esi
pushq $0x1
popq %rdx
callq 0x92ba
movq %rax, %r15
movq %rax, %rdi
leaq 0x6e04(%rip), %rsi      # 0xb191
callq 0x3510
testq %rax, %rax
je 0x43a1
movb $0x0, 0x4(%rax)
movl $0x34706d2e, (%rax)     # imm = 0x34706D2E
testq %r15, %r15
je 0x43e4
leaq 0x60(%rsp), %rbp
movq %rbp, %rdi
leaq 0x6de7(%rip), %rsi      # 0xb19c
leaq 0x190(%rsp), %rdx
movq %r15, %rcx
xorl %eax, %eax
callq 0x3490
andl $0x0, (%r13)
movq %rbp, %rdi
callq 0x3150
movl (%r13), %ecx
testl %eax, %eax
jne 0x42b7
testl %ecx, %ecx
jne 0x4406
leaq 0xe5a5(%rip), %rax      # 0x12990
cmpl $0x0, (%rax)
je 0x42dc
leaq 0x190(%rsp), %rdi
callq 0x3080
jmp 0x42dc
leaq 0x6d55(%rip), %rdi      # 0xb162
callq 0x3450
jmp 0x42bf
movq 0x40(%rsp), %rdi
callq 0x92b5
leaq 0xe568(%rip), %rax      # 0x12990
cmpl $0x0, (%rax)
je 0x4490
movl 0xc(%rsp), %edi
callq 0x8d04
testl %eax, %eax
je 0x4447
leaq 0xeb93(%rip), %rax      # 0x12fd4
movl $0x1, (%rax)
leaq 0xe9e2(%rip), %rax      # 0x12e30
cmpb $0x0, (%rax)
je 0x445a
xorl %edi, %edi
callq 0x96a4
pushq $0x1
popq %rdi
callq 0x34e0
movl 0x34(%rsp), %eax
movq %rax, 0x20(%rsp)
jmp 0x40d5
movq 0xdb81(%rip), %rax      # 0x11ff8
movq (%rax), %rdi
leaq 0x6c4a(%rip), %rsi      # 0xb0cb
xorl %r14d, %r14d
movq 0x28(%rsp), %rdx
xorl %eax, %eax
callq 0x32a0
movl %r14d, %eax
addq $0x258, %rsp            # imm = 0x258
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xdb4c(%rip), %rax      # 0x11ff8
movq (%rax), %rcx
leaq 0x6bdf(%rip), %rdi      # 0xb095
pushq $0x35
popq %rsi
pushq $0x1
popq %rdx
callq 0x34b0
pushq $-0x1
popq %rdi
callq 0x34a0