// File: /workspace/github_c_makefile_star2_O2/schroeder-lvb[P]sissm/src/api.c
// Function: apiMapcycleRead
int apiMapcycleRead( char *mapcycleFile )
{
    FILE *fpr;
    int   i, j;
    char  tmpLine[256], lastMapName[256], lastReqName[256];
    char  *w, *w2, *v, *u, scenarioName[256];

    strclr( lastMapName );
    for (i = 0; i<API_MAXMAPS; i++) {
        strclr( mapCycleList[i].mapName );
        strclr( mapCycleList[i].reqName );
        strclr( mapCycleList[i].scenario );
        strclr( mapCycleList[i].traveler );
        strclr( mapCycleList[i].gameMode );
        mapCycleList[i].dayNight =  0;
        mapCycleList[i].secIns   = -1;

        strclr( mapNameMerged[i] );
    }

#if DELME
    // mutator meta:  #SISSM.allowmutator, and #SISSM.defaultmutator
    //
    strclr( mutAllowed );
    strclr( mutDefault );
#endif

    i = 0;
    if ( NULL != (fpr = fopen( mapcycleFile, "rt" ))) {
        while ( !feof( fpr ) ) {
            if (NULL != fgets( tmpLine, 255, fpr )) {

                // part 1:  parse the "#SISSM.mapname" line
                //
                // printf("\nDEBUG - reading %s", tmpLine );
                if ( tmpLine == strcasestr( tmpLine, "#SISSM.mapname" )) {
                    if ( NULL != (w = getWord( tmpLine, 1, "= ,;:\015\012" ))) {
                        strlcpy( lastMapName, w, 256 );
                        strlcpy( lastReqName, w, 256 );
                        if ( NULL != (w2 = getWord( tmpLine, 2, "= ,;:\015\012" ))) {
                            if (0 != strlen( w2 )) strlcpy( lastReqName, w2, 256 );
                        }
                        // printf("\nDEBUG - reading map name ::%s::", lastMapName );

                        // create a merged list for "maplist" command
                        //
                        for ( j = 0; j<API_MAXMAPS; j++ ) {
                            if ( 0 == strcasecmp( mapNameMerged[j], lastReqName ) ) 
                                break;
                            if ( 0 == strlen( mapNameMerged[j] ) ) {
                                strlcpy( mapNameMerged[j], lastReqName, 40 ); 
                                break;
                            }
                        }
                    }
                }

                // part 2:  parse the scenario name and the game mode (checkpoint, hardcorecheckpoint, etc.)
                //
                else if ( tmpLine == strcasestr( tmpLine, "(Scenario=\"" )) {

                    strlcpy( scenarioName,  getWord( tmpLine, 1, "=\"" ), 255 );

                    if ( (0 != strlen( scenarioName )) && (0 != strlen( lastMapName )) )  {
                        strlcpy( mapCycleList[i].scenario, scenarioName, 256 );
                        strlcpy( mapCycleList[i].mapName,  lastMapName,  256 );
                        strlcpy( mapCycleList[i].reqName,  lastReqName,  256 );
                        strlcpy( mapCycleList[i].traveler, lastMapName,  256 );
                        strncat( mapCycleList[i].traveler, "?Scenario=", 256 );
                        strncat( mapCycleList[i].traveler, scenarioName, 128 );

                        // parse the gamemode
                        //
                        if ( NULL != ( v = strcasestr( tmpLine, "mode=\"" )  )) {
                            if ( NULL != (u = getWord( v, 1, "\"" )) ) {
                                strncat( mapCycleList[i].traveler, "?Game=", 256 );
                                strncat( mapCycleList[i].traveler, u,        172 );
                                strlcpy( mapCycleList[i].gameMode, u,         40 );
                            }
                        }

                        // parse the day/night
                        //
                        if ( NULL != strcasestr( tmpLine, "lighting=\"night\"" )) { 
                            mapCycleList[i].dayNight = 1;
                            strncat( mapCycleList[i].traveler, "?Lighting=Night", 256 );
                        }
                        else {
                            strncat( mapCycleList[i].traveler, "?Lighting=Day", 256 );
                        }

                        mapCycleList[i].secIns = -1;
                        if ( NULL != strcasestr( scenarioName, "insurgent"  )) 
                            mapCycleList[i].secIns = 1;
                        if ( NULL != strcasestr( scenarioName, "security"   )) 
                            mapCycleList[i].secIns = 0;
 
                        logPrintf( LOG_LEVEL_INFO, "api", "Mapcycle %03d tag %s traveler ::Sec/Ins:%1d Day/Night:%1d ::%s:: GameMode ::%s::", 
                            i,
                            mapCycleList[i].reqName,  mapCycleList[i].secIns,  mapCycleList[i].dayNight, 
                            mapCycleList[i].traveler, mapCycleList[i].gameMode );

                        if ( ++i >= API_MAXMAPS ) break; 

                    }
                }

#if DELME
                else if ( tmpLine == strcasestr( tmpLine, "#SISSM.allowmutator" )) {
                    j = 1;
                    while ( 1 == 1 ) {
                        u = getWord( tmpLine, j++, " ,=:\012\015" );
                        if ( u == NULL )        break;
                        if ( 0 == strlen( u ) ) break;
                        strlcat( mutAllowed, u,   16000 );
                        strlcat( mutAllowed, " ", 16000 );
                    } 
                }
                else if ( tmpLine == strcasestr( tmpLine, "#SISSM.defaultmutator" )) {
                    j = 1;
                    while ( 1 == 1 ) {
                        u = getWord( tmpLine, j++, " ,=:\012\015" );
                        if ( u == NULL )        break;
                        if ( 0 == strlen( u ) ) break;
                        strlcat( mutDefault, u,   256 );
                        strlcat( mutDefault, " ", 256 );
                    } 
                }
#endif
            }
        }
    }
 
#if DELME
    // todo-mut

    // Expand the space-separated single string mutAllowed to list mutAllowedList
    //
    strclr( mutAllowedList[0] );
    for (j = 0; j<API_MUT_MAXLIST; i++ ) { 
        w = getWord( mutAllowed, j, " ," );
        if ( w != NULL ) 
            strlcpy( mutAllowedList[j], w, API_MUT_MAXCHAR );
        else {
            strclr( mutAllowedList[j] );
            break;
        }              
    }
#endif 

#if 0
    if ( i != 0 ) {
        printf("\nMaps ------------------------\n");
        for ( j = 0; j<API_MAXMAPS; j++ ) {
            if ( 0 == strlen( mapNameMerged[j] ) ) break;
            printf("::%s:: ",  mapNameMerged[j] );
        }
        printf("\nMutators ------------------------");
        printf("\nMutAllowed ::%s::",    mutAllowed );
        printf("\nMutDefault ::%s::\n",  mutDefault );
    }
#endif

    return( i );
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x428, %rsp            # imm = 0x428
movq %rdi, %rbx
leaq 0x220(%rsp), %rdi
callq 0xed98
leaq 0x1ec07c(%rip), %r14    # 0x1fdef0
xorl %r13d, %r13d
leaq 0x166072(%rip), %r12    # 0x177ef0
movabsq $-0x100000000, %rbp     # imm = 0xFFFFFFFF00000000
cmpq $0x86000, %r13          # imm = 0x86000
je 0x11ef6
leaq (%r12,%r13), %rdi
callq 0xed98
leaq (%r12,%r13), %rdi
addq $0x100, %rdi            # imm = 0x100
callq 0xed98
leaq (%r12,%r13), %rdi
addq $0x200, %rdi            # imm = 0x200
callq 0xed98
leaq (%r12,%r13), %rdi
addq $0x300, %rdi            # imm = 0x300
callq 0xed98
leaq (%r12,%r13), %r15
addq $0x408, %r15            # imm = 0x408
movq %r15, %rdi
callq 0xed98
movq %rbp, -0x8(%r15)
movq %r14, %rdi
callq 0xed98
addq $0x430, %r13            # imm = 0x430
addq $0x28, %r14
jmp 0x11e88
leaq 0x8f8e(%rip), %rsi      # 0x1ae8b
movq %rbx, %rdi
callq 0x8330
testq %rax, %rax
je 0x122c7
movq %rax, %rbx
leaq 0x320(%rsp), %r14
movq $0x0, 0x8(%rsp)
movq %rbx, %rdi
callq 0x8270
testl %eax, %eax
jne 0x122c0
movq %r14, %rdi
movl $0xff, %esi
movq %rbx, %rdx
callq 0x8200
testq %rax, %rax
je 0x11f22
movq %r14, %rdi
leaq 0xb604(%rip), %rsi      # 0x1d555
callq 0x8360
movq %r14, %rdi
cmpq %rax, %r14
je 0x12110
leaq 0xb603(%rip), %rsi      # 0x1d56c
callq 0x8360
cmpq %rax, %r14
jne 0x11f22
movq %r14, %rdi
pushq $0x1
popq %rsi
leaq 0xb607(%rip), %rdx      # 0x1d587
callq 0xed9c
movl $0xff, %edx
leaq 0x20(%rsp), %rdi
movq %rax, %rsi
callq 0xe97a
cmpb $0x0, 0x20(%rsp)
je 0x11f22
cmpb $0x0, 0x220(%rsp)
je 0x11f22
movl 0x8(%rsp), %eax
imulq $0x430, %rax, %r15      # imm = 0x430
leaq (%r12,%r15), %r13
leaq (%r15,%r12), %rdi
addq $0x200, %rdi            # imm = 0x200
movl $0x100, %edx            # imm = 0x100
leaq 0x20(%rsp), %rsi
callq 0xe97a
movl $0x100, %edx            # imm = 0x100
movq %r13, %rdi
movq %r13, %rbp
leaq 0x220(%rsp), %r13
movq %r13, %rsi
callq 0xe97a
leaq (%r15,%r12), %rdi
addq $0x100, %rdi            # imm = 0x100
movl $0x100, %edx            # imm = 0x100
movq %rdi, 0x18(%rsp)
leaq 0x120(%rsp), %rsi
callq 0xe97a
addq %r12, %r15
addq $0x300, %r15            # imm = 0x300
movl $0x100, %edx            # imm = 0x100
movq %r15, %rdi
movq %r13, %rsi
movq %rbp, %r13
callq 0xe97a
movq %r15, %rdi
callq 0x8120
movabsq $0x6972616e6563533f, %rcx # imm = 0x6972616E6563533F
movq %rcx, 0x300(%rax,%rbp)
movl $0x3d6f69, 0x307(%rax,%rbp) # imm = 0x3D6F69
movl $0x80, %edx
movq %r15, %rdi
leaq 0x20(%rsp), %rsi
callq 0x81c0
movq %r14, %rdi
leaq 0xb513(%rip), %rsi      # 0x1d583
callq 0x8360
testq %rax, %rax
je 0x120cf
movq %rax, %rdi
pushq $0x1
popq %rsi
leaq 0xb501(%rip), %rdx      # 0x1d588
callq 0xed9c
testq %rax, %rax
je 0x120cf
movq %rax, %rbp
movq %r15, %rdi
callq 0x8120
movl $0x6d61473f, (%r15,%rax) # imm = 0x6D61473F
movl $0x3d656d, 0x3(%r15,%rax) # imm = 0x3D656D
movl $0xac, %edx
movq %r15, %rdi
movq %rbp, %rsi
callq 0x81c0
leaq 0x408(%r13), %rdi
movq %rbp, %rsi
pushq $0x28
popq %rdx
callq 0xe97a
movq %r14, %rdi
leaq 0xb4b8(%rip), %rsi      # 0x1d591
callq 0x8360
testq %rax, %rax
movq %r15, 0x10(%rsp)
je 0x121e9
movl $0x1, 0x400(%r13)
movq %r15, %rdi
callq 0x8120
movups 0xb49c(%rip), %xmm0     # 0x1d5a2
movups %xmm0, (%r15,%rax)
jmp 0x1220e
pushq $0x1
popq %rsi
leaq 0xb44a(%rip), %rdx      # 0x1d564
callq 0xed9c
testq %rax, %rax
je 0x11f22
movq %rax, %rbp
movl $0x100, %edx            # imm = 0x100
leaq 0x220(%rsp), %rdi
movq %rax, %rsi
callq 0xe97a
movl $0x100, %edx            # imm = 0x100
leaq 0x120(%rsp), %rdi
movq %rbp, %rsi
callq 0xe97a
movq %r14, %rdi
pushq $0x2
popq %rsi
leaq 0xb402(%rip), %rdx      # 0x1d564
callq 0xed9c
testq %rax, %rax
je 0x12186
cmpb $0x0, (%rax)
je 0x12186
movl $0x100, %edx            # imm = 0x100
leaq 0x120(%rsp), %rdi
movq %rax, %rsi
callq 0xe97a
leaq 0x1ebd63(%rip), %rbp    # 0x1fdef0
xorl %r15d, %r15d
addq $-0x28, %r15
leaq 0x28(%rbp), %r13
cmpq $-0x5028, %r15          # imm = 0xAFD8
je 0x11f22
movq %rbp, %rdi
leaq 0x120(%rsp), %rsi
callq 0x8060
testl %eax, %eax
je 0x11f22
cmpb $0x0, (%rbp)
movq %r13, %rbp
jne 0x12190
leaq 0x1ebd23(%rip), %rdi    # 0x1fdef0
subq %r15, %rdi
addq $-0x28, %rdi
leaq 0x120(%rsp), %rsi
pushq $0x28
popq %rdx
callq 0xe97a
jmp 0x11f22
movq %r15, %rdi
callq 0x8120
movabsq $0x6e69746867694c3f, %rcx # imm = 0x6E69746867694C3F
movq %rcx, (%r15,%rax)
movabsq $0x7961443d676e69, %rcx # imm = 0x7961443D676E69
movq %rcx, 0x6(%r15,%rax)
orl $-0x1, 0x404(%r13)
leaq 0x20(%rsp), %r15
movq %r15, %rdi
leaq 0xb39b(%rip), %rsi      # 0x1d5c0
callq 0x8360
cmpq $0x1, %rax
movl $0x0, %ebp
sbbl %ebp, %ebp
orl $0x1, %ebp
movl %ebp, 0x404(%r13)
movq %r15, %rdi
leaq 0xb381(%rip), %rsi      # 0x1d5ca
callq 0x8360
testq %rax, %rax
movl $0x0, %eax
cmovnel %eax, %ebp
movl %ebp, 0x404(%r13)
movl 0x400(%r13), %r10d
addq $0x408, %r13            # imm = 0x408
subq $0x8, %rsp
pushq $0x2
popq %rdi
leaq 0xabf3(%rip), %rsi      # 0x1ce6f
leaq 0xb350(%rip), %rdx      # 0x1d5d3
movq 0x10(%rsp), %r15
movl %r15d, %ecx
movq 0x20(%rsp), %r8
movl %ebp, %r9d
xorl %eax, %eax
pushq %r13
pushq 0x20(%rsp)
pushq %r10
callq 0x1920b
addq $0x20, %rsp
leal 0x1(%r15), %eax
cmpl $0x1ff, %r15d           # imm = 0x1FF
movl %eax, %ecx
movq %rcx, 0x8(%rsp)
jb 0x11f22
jmp 0x122c9
movq 0x8(%rsp), %rax
jmp 0x122c9
xorl %eax, %eax
addq $0x428, %rsp            # imm = 0x428
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq