// File: /workspace/github_c_makefile_star2_O3/schroeder-lvb[P]sissm/src/api.c
// Function: apiInit
int apiInit( void )
{
    cfsPtr cP;
    char   myIP[API_LINE_STRING_MAX], myRconPassword[API_LINE_STRING_MAX];
    int    i, j, numMutators, myPort, mapsCount;
    char   serverName[API_LINE_STRING_MAX];
    char   mutListTemp1[CFS_FETCH_MAX], mutListTemp2[CFS_FETCH_MAX], *u;
    char   tmpStr[ API_MUT_MAXCHAR ];

    // Read the "sissm" systems configuration variables
    //
    cP = cfsCreate( sissmGetConfigPath() );

    myPort = (int) cfsFetchNum( cP, "sissm.RconPort", 27015 );            
    strlcpy( myIP, cfsFetchStr( cP, "sissm.RconIP", "127.0.0.1"), API_LINE_STRING_MAX );
    strlcpy( myRconPassword, cfsFetchStr( cP, "sissm.RconPassword", ""), API_LINE_STRING_MAX );
    // strlcpy( adminListFilePath, cfsFetchStr( cP, "sissm.adminListFilePath", "Admins.txt"), API_LINE_STRING_MAX );

    strlcpy( serverName, cfsFetchStr( cP, "sissm.ServerName", "Unknown Server" ), API_LINE_STRING_MAX );

    // read the Bad Words filename
    //
    strlcpy( badWordsFilePath, cfsFetchStr( cP, "sissm.badWordsFilePath", "" ), CFS_FETCH_MAX );

    // read the Mapcycle filepath
    //
    strlcpy( mapcycleFilePath, cfsFetchStr( cP, "sissm.mapcycleFilePath", "" ), CFS_FETCH_MAX );

    // read the Mutator lists allowed by this system
    //
    strlcpy( mutListTemp1, cfsFetchStr( cP, "sissm.MutatorsStock", "" ), CFS_FETCH_MAX );
    strlcpy( mutListTemp2, cfsFetchStr( cP, "sissm.MutatorsMods",  "" ), CFS_FETCH_MAX );
    strlcat( mutListTemp2, " ", CFS_FETCH_MAX );
    if (( strlen( mutListTemp2 ) + strlen( mutListTemp1 ) ) < CFS_FETCH_MAX )
        strlcat( mutListTemp2, mutListTemp1, CFS_FETCH_MAX );
    else
        logPrintf( LOG_LEVEL_CRITICAL, "api", "String overflow reading mutator list sissm.MutatorStock/Mods" );

    // parse the Mutator list into separate array element
    //
    j = 0;
    strclr( mutAllowedList[ 0 ] );
    while ( 1 == 1 ) {
        u = getWord( mutListTemp2, j, " ,=:\012\015" );
        if ( u == NULL )              break;
        if ( 0 == strlen( u ) )       break;
        strlcpy( mutAllowedList[ j ], u, API_MUT_MAXCHAR );
        strclr( mutAllowedList[ ++j ] );
        if ( j >= API_MUT_MAXLIST ) break;
    }
    j = 0;
    while ( 1 == 1 ) {
        if ( 0 == strlen( mutAllowedList[j] )) break;
        logPrintf( LOG_LEVEL_INFO, "api", "Mutator read %d ::%s::", j, mutAllowedList[j] );
        j++;
    }

    // create a sorted Mutator list by size of string (for lookup match priority) 
    //
    numMutators = j - 1;
    for ( j = 0; j<numMutators + 1; j++ ) {   // include one extra array w/ terminating ""
        strlcpy( mutAllowedListSorted[ j ], mutAllowedList[ j ], API_MUT_MAXCHAR );
    }
    for (j = 0; j<numMutators-1; j++ ) {
        for ( i = j+1; i<numMutators; i++ ) {
            if (  strlen( mutAllowedListSorted[i] ) > strlen( mutAllowedListSorted[j] ) ) {
                strlcpy( tmpStr, mutAllowedListSorted[i], API_MUT_MAXCHAR );
                strlcpy( mutAllowedListSorted[i], mutAllowedListSorted[j], API_MUT_MAXCHAR );
                strlcpy( mutAllowedListSorted[j], tmpStr, API_MUT_MAXCHAR );
            }
        }
    }
    j = 0;
    while ( 1 == 1 ) {
        if ( 0 == strlen( mutAllowedListSorted[j] )) break;
        logPrintf( LOG_LEVEL_INFO, "api", "Mutator sort %d ::%s::", j, mutAllowedListSorted[j] );
        j++;
    }

    // read the AUTH (permissions) data
    //
    apiInitAuth();

    // Used with Circleus' "Advanced Chat" mod
    //
    strlcpy( removeCodes, cfsFetchStr( cP, "sissm.removeCodes", ""), API_LINE_STRING_MAX );
    strlcpy( sayPrefix,   cfsFetchStr( cP, "sissm.sayPrefix",  ""),  API_LINE_STRING_MAX );
    strlcpy( sayPostfix,  cfsFetchStr( cP, "sissm.sayPostfix", ""),  API_LINE_STRING_MAX );

#if FINALCOUNTER_REDUCE
    // Checkpoint/Hardcorecheckpoint Crash Protection Limiter 
    //
    maxBotsCounterAttack = (int) cfsFetchNum( cP, "sissm.MaxBotsCounterAttack", 30.0 ); 
    finalCacheBotQuotaMultiplier = (double) cfsFetchNum( cP, "sissm.FinalCacheBotQuotaMultiplier", 1.2 );
#endif

    //  Close the configuration file reader
    //
    cfsDestroy( cP );

    // Set map to unknown
    //
    rosterSetMapName( "Unknown" );

    // Initialize the RCON TCP/IP interface
    //
    _rPtr = rdrvInit( myIP, myPort, myRconPassword );

    // Setup callbacks for player entering and leaving
    // 
    eventsRegister( SISSM_EV_CLIENT_ADD,  _apiPlayerConnectedCB );
    eventsRegister( SISSM_EV_CLIENT_DEL,  _apiPlayerDisconnectedCB );
    eventsRegister( SISSM_EV_MAPCHANGE,   _apiMapChangeCB );
    eventsRegister( SISSM_EV_TRAVEL,      _apiTravelCB );
    eventsRegister( SISSM_EV_SESSIONLOG,  _apiSessionLogCB );
    eventsRegister( SISSM_EV_ROUND_START, _apiRoundStartCB );

    // These callbacks are used for detecting players entering
    // and leaving territorial capture zone (piantirush)
    //
    eventsRegister( SISSM_EV_BP_PLAYER_CONN,    _apiBPCharNameCB );
    eventsRegister( SISSM_EV_BP_PLAYER_DISCONN, _apiBPCharDisconnectCB );
    eventsRegister( SISSM_EV_BP_CHARNAME,       _apiBPCharPlayerCB );
    eventsRegister( SISSM_EV_BP_TOUCHED_OBJ,    _apiBPCharTouchCB );
    eventsRegister( SISSM_EV_BP_UNTOUCHED_OBJ,  _apiBPCharUntouchCB );

    // Event to handle caching of map objective list 
    //
    eventsRegister( SISSM_EV_MAP_OBJECTIVE, _apiMapObjectiveCB );

    // This hook is mostly used for DEBUG use, non-production
    //
    eventsRegister( SISSM_EV_CHAT,                _apiChatCB );

    // for detecting if the game is in Counterattack (Checkpoint/Hardcore)
    //
    eventsRegister( SISSM_EV_ACTIVITY,         _apiCounterStartCB );
    eventsRegister( SISSM_EV_CA_STOP,          _apiCounterStopCB  );

    // Setup Alarm (periodic callbacks) for fetching roster from RCON
    // 
    _apiPollAlarmPtr = alarmCreate( _apiPollAlarmCB );
    alarmReset( _apiPollAlarmPtr, API_LISTPLAYERS_PERIOD );

    // Clear the Roster module that keeps track of players
    //
    rosterInit();
    rosterSetServerName( serverName );
    lastRosterSuccessTime = apiTimeGet();      
    rosterReset();

    // Read the admin list
    //
    // adminCount = apiIdListRead( adminListFilePath, adminIdList );
    // logPrintf( LOG_LEVEL_CRITICAL, "api", "Admin list %d clients from file %s", adminCount, adminListFilePath );

    // Read the Bad Words list
    //
    // badWordsCount = apiWordListRead( badWordsFilePath, badWordsList );
    // logPrintf( LOG_LEVEL_CRITICAL, "api", "BadWords list %d words from file %s", badWordsCount, badWordsFilePath );
    // 
    apiInitBadWords();

    // Read the Mapcycle.txt
    //
    mapsCount = apiMapcycleRead( mapcycleFilePath );
    logPrintf( LOG_LEVEL_CRITICAL, "api", "Maplist read %d maps from file %s", mapsCount, mapcycleFilePath);


    // Clear the internal roster & gamestate tracking variables
    //
    strclr( rosterPrevious );
    strclr( rosterCurrent  );
    strclr( gameStateCurrent );
    strclr( gameStatePrevious );

    // Clear the last reboot reason/time 
    //
    lastRebootTime = apiTimeGet();
    strlcpy( lastRebootReason, "Restart", 256 );

    // Clear the BPChar translation table
    //
    _apiBPCharInitCB( "" );

    // on init clear the cache of objectives names
    //
    for (i=0; i<API_OBJECTIVES_MAX; i++) strclr( _objectiveListCache[i]) ;

    // Clear the clan list
    //
    apiIdListClear( clanIdList );

    return( _rPtr == NULL );
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb88, %rsp            # imm = 0xB88
callq 0x8970
movq %rax, %rdi
callq 0x8ffc
movq %rax, %rbx
leaq 0xbdc5(%rip), %rsi      # 0x1e29f
movsd 0x8b36(%rip), %xmm0     # 0x1b018
movq %rax, %rdi
callq 0x926a
movsd %xmm0, (%rsp)
leaq 0xbdb8(%rip), %rsi      # 0x1e2ae
leaq 0x8c0f(%rip), %rdx      # 0x1b10c
movq %rbx, %rdi
callq 0x9073
leaq 0x280(%rsp), %rdi
movl $0x100, %edx            # imm = 0x100
movq %rax, %rsi
callq 0xf384
leaq 0xbd9a(%rip), %rsi      # 0x1e2bb
leaq 0x91e1(%rip), %r14      # 0x1b709
movq %rbx, %rdi
movq %r14, %rdx
callq 0x9073
leaq 0x180(%rsp), %rdi
movl $0x100, %edx            # imm = 0x100
movq %rax, %rsi
callq 0xf384
leaq 0xbd7f(%rip), %rsi      # 0x1e2ce
leaq 0x8b75(%rip), %rdx      # 0x1b0cb
movq %rbx, %rdi
callq 0x9073
leaq 0x80(%rsp), %rdi
movl $0x100, %edx            # imm = 0x100
movq %rax, %rsi
callq 0xf384
leaq 0xbd65(%rip), %rsi      # 0x1e2df
movq %rbx, %rdi
movq %r14, %rdx
callq 0x9073
leaq 0x142834(%rip), %rdi    # 0x154dc0
movl $0x400, %edx            # imm = 0x400
movq %rax, %rsi
callq 0xf384
leaq 0xbd56(%rip), %rsi      # 0x1e2f6
movq %rbx, %rdi
movq %r14, %rdx
callq 0x9073
leaq 0x14291e(%rip), %rdi    # 0x154ed0
movl $0x400, %edx            # imm = 0x400
movq %rax, %rsi
callq 0xf384
leaq 0xbd47(%rip), %rsi      # 0x1e30d
movq %rbx, %rdi
movq %r14, %rdx
callq 0x9073
leaq 0x780(%rsp), %r15
movl $0x400, %edx            # imm = 0x400
movq %r15, %rdi
movq %rax, %rsi
callq 0xf384
leaq 0xbd31(%rip), %rsi      # 0x1e321
movq %rbx, %rdi
movq %r14, %rdx
callq 0x9073
leaq 0x380(%rsp), %r14
movl $0x400, %edx            # imm = 0x400
movq %r14, %rdi
movq %rax, %rsi
callq 0xf384
leaq 0x9b34(%rip), %rsi      # 0x1c14e
movl $0x400, %edx            # imm = 0x400
movq %r14, %rdi
callq 0xf310
movq %r14, %rdi
callq 0x8120
movq %rax, %r14
movq %r15, %rdi
callq 0x8120
addq %r14, %rax
cmpq $0x3ff, %rax            # imm = 0x3FF
movq %rbx, 0x10(%rsp)
ja 0x12666
leaq 0x380(%rsp), %rdi
leaq 0x780(%rsp), %rsi
movl $0x400, %edx            # imm = 0x400
callq 0xf310
jmp 0x1267d
leaq 0xb812(%rip), %rsi      # 0x1de7f
leaq 0xbcc0(%rip), %rdx      # 0x1e334
xorl %edi, %edi
xorl %eax, %eax
callq 0x1a3e3
cvttsd2si (%rsp), %eax
movl %eax, 0xc(%rsp)
leaq 0x142943(%rip), %r14    # 0x154fd0
movq %r14, %rdi
callq 0xf829
xorl %r15d, %r15d
leaq 0xbcd2(%rip), %r12      # 0x1e371
leaq 0x380(%rsp), %r13
movq %r13, %rdi
movl %r15d, %esi
movq %r12, %rdx
callq 0xf82d
testq %rax, %rax
je 0x126e4
cmpb $0x0, (%rax)
je 0x126e4
movl $0x50, %edx
movq %r14, %rdi
movq %rax, %rsi
callq 0xf384
incq %r15
addq $0x50, %r14
movq %r14, %rdi
callq 0xf829
cmpq $0x40, %r15
jne 0x126a7
cmpb $0x0, 0x1428e5(%rip)    # 0x154fd0
je 0x12823
movl $0xfffffffe, %ebx       # imm = 0xFFFFFFFE
leaq 0x1428d3(%rip), %r12    # 0x154fd0
leaq 0xb77b(%rip), %r15      # 0x1de7f
leaq 0xbc6d(%rip), %r13      # 0x1e378
xorl %ebp, %ebp
movl $0x2, %edi
movq %r15, %rsi
movq %r13, %rdx
movl %ebp, %ecx
movq %r12, %r8
xorl %eax, %eax
callq 0x1a3e3
incl %ebp
incq %rbx
cmpb $0x0, 0x50(%r12)
leaq 0x50(%r12), %r12
jne 0x1270d
movl %ebp, %r14d
leaq 0x142890(%rip), %r15    # 0x154fd0
leaq 0x143c89(%rip), %r12    # 0x1563d0
movl $0x50, %edx
movq %r12, %rdi
movq %r15, %rsi
callq 0xf384
addq $0x50, %r15
addq $0x50, %r12
decq %r14
jne 0x12747
cmpl $0x3, %ebp
jl 0x12823
movl %ebx, %eax
movq %rax, 0x18(%rsp)
decl %ebp
movl $0x1, %ebx
leaq 0x143c9e(%rip), %r13    # 0x156420
xorl %eax, %eax
leaq 0x30(%rsp), %r15
movq %rbp, 0x20(%rsp)
leaq (%rax,%rax,4), %r12
incq %rax
movq %rax, 0x28(%rsp)
shlq $0x4, %r12
leaq 0x143c2b(%rip), %rax    # 0x1563d0
addq %rax, %r12
movq %rbp, %r14
movq %r13, (%rsp)
movq %r13, %rdi
callq 0x8120
movq %rax, %rbp
movq %r12, %rdi
callq 0x8120
cmpq %rax, %rbp
jbe 0x127f7
movl $0x50, %edx
movq %r15, %rdi
movq %r13, %rsi
callq 0xf384
movl $0x50, %edx
movq %r13, %rdi
movq %r12, %rsi
callq 0xf384
movl $0x50, %edx
movq %r12, %rdi
movq %r15, %rsi
callq 0xf384
addq $0x50, %r13
decq %r14
cmpq %r14, %rbx
jne 0x127af
incq %rbx
movq (%rsp), %r13
addq $0x50, %r13
movq 0x28(%rsp), %rax
cmpq 0x18(%rsp), %rax
movq 0x20(%rsp), %rbp
jne 0x1278e
cmpb $0x0, 0x143ba6(%rip)    # 0x1563d0
je 0x1286a
leaq 0x143b9d(%rip), %r15    # 0x1563d0
leaq 0xb645(%rip), %r14      # 0x1de7f
leaq 0xbb4e(%rip), %r12      # 0x1e38f
xorl %r13d, %r13d
movl $0x2, %edi
movq %r14, %rsi
movq %r12, %rdx
movl %r13d, %ecx
movq %r15, %r8
xorl %eax, %eax
callq 0x1a3e3
incl %r13d
cmpb $0x0, 0x50(%r15)
leaq 0x50(%r15), %r15
jne 0x12844
callq 0x1207c
leaq 0xbb30(%rip), %rsi      # 0x1e3a6
leaq 0x8e8c(%rip), %r14      # 0x1b709
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rdx
callq 0x9073
leaq 0xbe9ec(%rip), %rdi     # 0xd1280
movl $0x100, %edx            # imm = 0x100
movq %rax, %rsi
callq 0xf384
leaq 0xbb10(%rip), %rsi      # 0x1e3b8
movq %rbx, %rdi
movq %r14, %rdx
callq 0x9073
leaq 0x144f16(%rip), %rdi    # 0x1577d0
movl $0x100, %edx            # imm = 0x100
movq %rax, %rsi
callq 0xf384
leaq 0xbafa(%rip), %rsi      # 0x1e3c8
movq %rbx, %rdi
movq %r14, %rdx
callq 0x9073
leaq 0x144ff0(%rip), %rdi    # 0x1578d0
movl $0x100, %edx            # imm = 0x100
movq %rax, %rsi
callq 0xf384
movq %rbx, %rdi
callq 0x9054
leaq 0xbadd(%rip), %rdi      # 0x1e3d9
callq 0x1456b
leaq 0x280(%rsp), %rdi
leaq 0x180(%rsp), %rdx
movl 0xc(%rsp), %esi
callq 0x960c
movq %rax, 0xc6b67(%rip)     # 0xd9488
leaq -0x100d(%rip), %rsi     # 0x1191b
movl $0x3, %edi
callq 0xd40c
leaq -0xfe6(%rip), %rsi      # 0x11953
movl $0x4, %edi
callq 0xd40c
leaq -0xbbc(%rip), %rsi      # 0x11d8e
movl $0x5, %edi
callq 0xd40c
leaq -0xfda(%rip), %rsi      # 0x11981
movl $0x11, %edi
callq 0xd40c
leaq -0xfe1(%rip), %rsi      # 0x1198b
movl $0x12, %edi
callq 0xd40c
leaq -0x936(%rip), %rsi      # 0x12047
movl $0x8, %edi
callq 0xd40c
leaq -0xead(%rip), %rsi      # 0x11ae1
movl $0x15, %edi
callq 0xd40c
leaq -0xcdc(%rip), %rsi      # 0x11cc3
movl $0x16, %edi
callq 0xd40c
leaq -0xe54(%rip), %rsi      # 0x11b5c
movl $0x17, %edi
callq 0xd40c
leaq -0xc39(%rip), %rsi      # 0x11d88
movl $0x18, %edi
callq 0xd40c
leaq -0xc47(%rip), %rsi      # 0x11d8b
movl $0x19, %edi
callq 0xd40c
leaq -0xa93(%rip), %rsi      # 0x11f50
movl $0x1a, %edi
callq 0xd40c
leaq -0x10dc(%rip), %rsi     # 0x11918
xorl %r15d, %r15d
xorl %edi, %edi
callq 0xd40c
leaq -0x601(%rip), %rsi      # 0x12404
movl $0x1c, %edi
callq 0xd40c
leaq -0x5a6(%rip), %rsi      # 0x12470
movl $0x1e, %edi
callq 0xd40c
leaq -0x13aa(%rip), %rdi     # 0x1167d
callq 0xfc23
movq %rax, 0xcea5d(%rip)     # 0xe1490
movl $0xa, %esi
movq %rax, %rdi
callq 0xfc98
callq 0x147d7
leaq 0x80(%rsp), %rdi
callq 0x14761
xorl %edi, %edi
callq 0x82a0
movq %rax, 0xc6a20(%rip)     # 0xd9480
callq 0x1477d
leaq 0x142354(%rip), %rbx    # 0x154dc0
leaq 0xbea0d(%rip), %rsi     # 0xd1480
movq %rbx, %rdi
callq 0x11066
leaq 0xb3fd(%rip), %r14      # 0x1de7f
leaq 0xb785(%rip), %rdx      # 0x1e20e
xorl %edi, %edi
movq %r14, %rsi
movl %eax, %ecx
movq %rbx, %r8
xorl %eax, %eax
callq 0x1a3e3
leaq 0x14242f(%rip), %rbx    # 0x154ed0
movq %rbx, %rdi
callq 0x12b76
leaq 0xb931(%rip), %rdx      # 0x1e3e1
xorl %edi, %edi
movq %r14, %rsi
movl %eax, %ecx
movq %rbx, %r8
xorl %eax, %eax
callq 0x1a3e3
leaq 0xca9c8(%rip), %rdi     # 0xdd490
callq 0xf829
leaq 0xc69bc(%rip), %rdi     # 0xd9490
callq 0xf829
leaq 0x144ef0(%rip), %rdi    # 0x1579d0
callq 0xf829
leaq 0xdaac4(%rip), %rdi     # 0xed5b0
callq 0xf829
xorl %edi, %edi
callq 0x82a0
movq %rax, 0x148ed1(%rip)    # 0x15b9d0
movabsq $0x74726174736552, %rax # imm = 0x74726174736552
movq %rax, 0x148ed0(%rip)    # 0x15b9e0
callq 0x119d3
leaq 0xd6a94(%rip), %rbx     # 0xe95b0
leaq (%rbx,%r15), %rdi
callq 0xf829
addq $0x100, %r15            # imm = 0x100
cmpq $0x4000, %r15           # imm = 0x4000
jne 0x12b1c
xorl %ebx, %ebx
leaq 0x148fa2(%rip), %r14    # 0x15bae0
leaq (%r14,%rbx), %rdi
callq 0xf829
addq $0x100, %rbx            # imm = 0x100
cmpq $0x10000, %rbx          # imm = 0x10000
jne 0x12b3e
xorl %eax, %eax
cmpq $0x0, 0xc6927(%rip)     # 0xd9488
sete %al
addq $0xb88, %rsp            # imm = 0xB88
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq