// File: /workspace/github_c_makefile_star2_O2/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 0x8973
movq %rax, %rdi
callq 0x8f80
movq %rax, %r15
leaq 0xba7c(%rip), %rsi      # 0x1d28f
movsd 0x87fd(%rip), %xmm0     # 0x1a018
movq %rax, %rdi
callq 0x91bd
movsd %xmm0, 0x8(%rsp)
leaq 0xba6e(%rip), %rsi      # 0x1d29e
leaq 0x88d5(%rip), %rdx      # 0x1a10c
movq %r15, %rdi
callq 0x8ff5
leaq 0x280(%rsp), %rdi
movl $0x100, %edx            # imm = 0x100
movq %rax, %rsi
callq 0xe97a
leaq 0xba50(%rip), %rsi      # 0x1d2ab
leaq 0x8ea7(%rip), %r14      # 0x1a709
movq %r15, %rdi
movq %r14, %rdx
callq 0x8ff5
leaq 0x180(%rsp), %rdi
movl $0x100, %edx            # imm = 0x100
movq %rax, %rsi
callq 0xe97a
leaq 0xba35(%rip), %rsi      # 0x1d2be
leaq 0x883b(%rip), %rdx      # 0x1a0cb
movq %r15, %rdi
callq 0x8ff5
leaq 0x80(%rsp), %rdi
movl $0x100, %edx            # imm = 0x100
movq %rax, %rsi
callq 0xe97a
leaq 0xba1b(%rip), %rsi      # 0x1d2cf
movq %r15, %rdi
movq %r14, %rdx
callq 0x8ff5
leaq 0x1434fa(%rip), %rdi    # 0x154dc0
movl $0x400, %edx            # imm = 0x400
movq %rax, %rsi
callq 0xe97a
leaq 0xba0c(%rip), %rsi      # 0x1d2e6
movq %r15, %rdi
movq %r14, %rdx
callq 0x8ff5
leaq 0x1435e4(%rip), %rdi    # 0x154ed0
movl $0x400, %edx            # imm = 0x400
movq %rax, %rsi
callq 0xe97a
leaq 0xb9fd(%rip), %rsi      # 0x1d2fd
movq %r15, %rdi
movq %r14, %rdx
callq 0x8ff5
leaq 0x780(%rsp), %rbx
movl $0x400, %edx            # imm = 0x400
movq %rbx, %rdi
movq %rax, %rsi
callq 0xe97a
leaq 0xb9e7(%rip), %rsi      # 0x1d311
movq %r15, 0x18(%rsp)
movq %r15, %rdi
movq %r14, %rdx
callq 0x8ff5
leaq 0x380(%rsp), %r14
movl $0x400, %edx            # imm = 0x400
movq %r14, %rdi
movq %rax, %rsi
callq 0xe97a
leaq 0x97f5(%rip), %rsi      # 0x1b14e
movl $0x400, %edx            # imm = 0x400
movq %r14, %rdi
callq 0xe918
movq %r14, %rdi
callq 0x8120
movq %rax, %r14
movq %rbx, %rdi
callq 0x8120
addq %r14, %rax
cmpq $0x3ff, %rax            # imm = 0x3FF
ja 0x119a0
leaq 0x380(%rsp), %rdi
leaq 0x780(%rsp), %rsi
movl $0x400, %edx            # imm = 0x400
callq 0xe918
jmp 0x119b7
leaq 0xb4c8(%rip), %rsi      # 0x1ce6f
leaq 0xb976(%rip), %rdx      # 0x1d324
xorl %edi, %edi
xorl %eax, %eax
callq 0x1920b
cvttsd2si 0x8(%rsp), %eax
movl %eax, 0x10(%rsp)
leaq 0x143608(%rip), %r14    # 0x154fd0
movq %r14, %rdi
callq 0xed98
xorl %ebx, %ebx
leaq 0xb988(%rip), %r15      # 0x1d361
leaq 0x380(%rsp), %r12
pushq $0x50
popq %r13
movq %r12, %rdi
movl %ebx, %esi
movq %r15, %rdx
callq 0xed9c
testq %rax, %rax
je 0x11a1f
cmpb $0x0, (%rax)
je 0x11a1f
movq %r14, %rdi
movq %rax, %rsi
movq %r13, %rdx
callq 0xe97a
incq %rbx
addq $0x50, %r14
movq %r14, %rdi
callq 0xed98
cmpq $0x40, %rbx
jne 0x119e5
movabsq $-0x200000000, %rax     # imm = 0xFFFFFFFE00000000
movq %rax, 0x8(%rsp)
leaq 0x14359b(%rip), %rbx    # 0x154fd0
leaq 0xb433(%rip), %r15      # 0x1ce6f
leaq 0xb925(%rip), %r12      # 0x1d368
pushq $0x2
popq %rbp
movabsq $0x100000000, %r13      # imm = 0x100000000
xorl %r14d, %r14d
cmpb $0x0, (%rbx)
je 0x11a7b
movl %ebp, %edi
movq %r15, %rsi
movq %r12, %rdx
movl %r14d, %ecx
movq %rbx, %r8
xorl %eax, %eax
callq 0x1920b
incq %r14
addq %r13, 0x8(%rsp)
addq $0x50, %rbx
jmp 0x11a53
leaq 0x14354e(%rip), %rbx    # 0x154fd0
leaq 0x144947(%rip), %r15    # 0x1563d0
pushq $0x50
popq %r12
movq %r14, %r13
subq $0x1, %r13
jb 0x11aae
movq %r15, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0xe97a
addq $0x50, %rbx
addq $0x50, %r15
jmp 0x11a90
decq %r14
sarq $0x20, 0x8(%rsp)
leaq 0x144962(%rip), %rbx    # 0x156420
pushq $0x1
popq %r12
xorl %eax, %eax
pushq $0x50
popq %r13
cmpq 0x8(%rsp), %rax
jge 0x11b63
movq %rax, 0x20(%rsp)
imulq $0x50, %rax, %rbp
leaq 0x1448ed(%rip), %rax    # 0x1563d0
addq %rax, %rbp
movl %r12d, 0x14(%rsp)
movq %rbx, 0x28(%rsp)
cmpl %r12d, %r14d
jle 0x11b45
movq %rbx, %rdi
callq 0x8120
movq %rax, %r15
movq %rbp, %rdi
callq 0x8120
cmpq %rax, %r15
jbe 0x11b3c
leaq 0x30(%rsp), %r15
movq %r15, %rdi
movq %rbx, %rsi
movq %r13, %rdx
callq 0xe97a
movq %rbx, %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0xe97a
movq %rbp, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0xe97a
addq $0x50, %rbx
incl %r12d
jmp 0x11af0
movq 0x20(%rsp), %rax
incq %rax
movq 0x28(%rsp), %rbx
addq $0x50, %rbx
movl 0x14(%rsp), %r12d
incl %r12d
jmp 0x11ac8
leaq 0xb305(%rip), %rbx      # 0x1ce6f
leaq 0xb80e(%rip), %r14      # 0x1d37f
pushq $0x2
popq %rbp
xorl %r12d, %r12d
leaq 0x144852(%rip), %r15    # 0x1563d0
cmpb $0x0, (%r15)
je 0x11ba2
movl %ebp, %edi
movq %rbx, %rsi
movq %r14, %rdx
movl %r12d, %ecx
movq %r15, %r8
xorl %eax, %eax
callq 0x1920b
addq $0x50, %r15
incl %r12d
jmp 0x11b7e
callq 0x113c3
leaq 0xb7e8(%rip), %rsi      # 0x1d396
leaq 0x8b54(%rip), %rbx      # 0x1a709
movq 0x18(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rdx
callq 0x8ff5
leaq 0xbf6b4(%rip), %rdi     # 0xd1280
movl $0x100, %edx            # imm = 0x100
movq %rax, %rsi
callq 0xe97a
leaq 0xb7c8(%rip), %rsi      # 0x1d3a8
movq %r14, %rdi
movq %rbx, %rdx
callq 0x8ff5
leaq 0x145bde(%rip), %rdi    # 0x1577d0
movl $0x100, %edx            # imm = 0x100
movq %rax, %rsi
callq 0xe97a
leaq 0xb7b2(%rip), %rsi      # 0x1d3b8
movq %r14, %rdi
movq %rbx, %rdx
callq 0x8ff5
leaq 0x145cb8(%rip), %rdi    # 0x1578d0
movl $0x100, %edx            # imm = 0x100
movq %rax, %rsi
callq 0xe97a
movq %r14, %rdi
callq 0x8fd6
leaq 0xb795(%rip), %rdi      # 0x1d3c9
callq 0x13650
leaq 0x280(%rsp), %rdi
leaq 0x180(%rsp), %rdx
movl 0x10(%rsp), %esi
callq 0x9534
movq %rax, 0xc782f(%rip)     # 0xd9488
leaq -0xfa5(%rip), %rsi      # 0x10cbb
pushq $0x3
popq %rdi
callq 0xcba7
leaq -0xf7e(%rip), %rsi      # 0x10cf1
pushq $0x4
popq %rdi
callq 0xcba7
leaq -0xbbc(%rip), %rsi      # 0x110c2
pushq $0x5
popq %rdi
callq 0xcba7
leaq -0xf70(%rip), %rsi      # 0x10d1d
pushq $0x11
popq %rdi
callq 0xcba7
leaq -0xf75(%rip), %rsi      # 0x10d27
pushq $0x12
popq %rdi
callq 0xcba7
leaq -0x91b(%rip), %rsi      # 0x11390
pushq $0x8
popq %rdi
callq 0xcba7
leaq -0xea5(%rip), %rsi      # 0x10e15
pushq $0x15
popq %rdi
callq 0xcba7
leaq -0xcd1(%rip), %rsi      # 0x10ff8
pushq $0x16
popq %rdi
callq 0xcba7
leaq -0xe4a(%rip), %rsi      # 0x10e8e
pushq $0x17
popq %rdi
callq 0xcba7
leaq -0xc2b(%rip), %rsi      # 0x110bc
pushq $0x18
popq %rdi
callq 0xcba7
leaq -0xc37(%rip), %rsi      # 0x110bf
pushq $0x19
popq %rdi
callq 0xcba7
leaq -0xa66(%rip), %rsi      # 0x1129f
pushq $0x1a
popq %rdi
callq 0xcba7
leaq -0x105c(%rip), %rsi     # 0x10cb8
xorl %r14d, %r14d
xorl %edi, %edi
callq 0xcba7
leaq -0x5e0(%rip), %rsi      # 0x11745
pushq $0x1c
popq %rdi
callq 0xcba7
leaq -0x587(%rip), %rsi      # 0x117ad
pushq $0x1e
popq %rdi
callq 0xcba7
leaq -0x1310(%rip), %rdi     # 0x10a33
callq 0xf145
movq %rax, 0xcf741(%rip)     # 0xe1490
pushq $0xa
popq %rsi
movq %rax, %rdi
callq 0xf1b1
callq 0x138b3
leaq 0x80(%rsp), %rdi
callq 0x1383a
callq 0x10b6a
movq %rax, 0xc7708(%rip)     # 0xd9480
callq 0x13856
callq 0x11711
leaq 0x143147(%rip), %rbx    # 0x154ed0
movq %rbx, %rdi
callq 0x11e4c
leaq 0xb0d7(%rip), %rsi      # 0x1ce6f
leaq 0xb632(%rip), %rdx      # 0x1d3d1
xorl %edi, %edi
movl %eax, %ecx
movq %rbx, %r8
xorl %eax, %eax
callq 0x1920b
leaq 0xcb6dc(%rip), %rdi     # 0xdd490
callq 0xed98
leaq 0xc76d0(%rip), %rdi     # 0xd9490
callq 0xed98
leaq 0x145c04(%rip), %rdi    # 0x1579d0
callq 0xed98
leaq 0xdb7d8(%rip), %rdi     # 0xed5b0
callq 0xed98
callq 0x10b6a
movq %rax, 0x149be7(%rip)    # 0x15b9d0
movabsq $0x74726174736552, %rax # imm = 0x74726174736552
movq %rax, 0x149be6(%rip)    # 0x15b9e0
callq 0x10d6d
leaq 0xd77aa(%rip), %rbx     # 0xe95b0
cmpq $0x4000, %r14           # imm = 0x4000
je 0x11e21
leaq (%rbx,%r14), %rdi
callq 0xed98
addq $0x100, %r14            # imm = 0x100
jmp 0x11e06
leaq 0x149cb8(%rip), %rdi    # 0x15bae0
callq 0x10638
xorl %eax, %eax
cmpq $0x0, 0xc7651(%rip)     # 0xd9488
sete %al
addq $0xb88, %rsp            # imm = 0xB88
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq