/**
 * Process command line args into $config array.
 *
 * @param $config Global config.
 * @return void
 */
function process_args(&$config)
{
    $config['system_root'] = isset($_SERVER['argv'][1]) ? $_SERVER['argv'][1] : '';
    if (empty($config['system_root'])) {
        echo "ERROR: You need to supply the path to the System Root as the first argument\n";
        exit;
    }
    if (!is_dir($config['system_root']) || !is_readable($config['system_root'] . '/core/include/init.inc')) {
        echo "ERROR: Path provided doesn't point to a Matrix installation's System Root. Please provide correct path and try again.\n";
        exit;
    }
    $config['assetids'] = isset($_SERVER['argv'][2]) ? $_SERVER['argv'][2] : '';
    if (empty($config['assetids'])) {
        echo "ERROR: You need to specify the root nodes to apply the schema from as the second argument\n";
        exit;
    }
    //end if
    $config['permissions_string'] = isset($_SERVER['argv'][3]) ? $_SERVER['argv'][3] : '';
    if (empty($config['permissions_string'])) {
        echo "ERROR: You need to specify a permissions string as the third argument\n";
        exit;
    }
    //end if
    // Permissions string
    $config['permissions'] = array();
    $config['permission_types'] = array('r' => 1, 'w' => 2, 'a' => 3);
    $perms = explode(';', $config['permissions_string'] . ';');
    foreach ($perms as $p) {
        if (!empty($p)) {
            array_push($config['permissions'], array('type' => (int) $config['permission_types'][$p[0]], 'granted' => $p[1], 'userid' => substr($p, 2)));
        }
    }
    $config['cascades'] = !get_boolean_arg('-nc');
    // Invert.. -nc means no cascade.
    $config['force'] = get_boolean_arg('-f');
    $config['max_threads'] = (int) get_parameterised_arg('--max-threads', 3);
    if ($config['max_threads'] > 5) {
        $config['max_threads'] = 5;
    } elseif ($config['max_threads'] < 1) {
        $config['max_threads'] = 1;
    }
    //end if
    $config['batch_size'] = (int) get_parameterised_arg('--batch-size', 50);
    if ($config['batch_size'] < 5) {
        $config['batch_size'] = 50;
    }
    echo "\n";
    echo "Will attempt to set permissions to asset(s) {$config['assetids']}.\n\n";
    echo "  Max threads: {$config['max_threads']}\n";
    echo "  Batch size: {$config['batch_size']}\n";
    echo "\n";
}
/**
 * Process command line args into $config array.
 *
 * @param $config Global config.
 * @return void
 */
function process_args(&$config)
{
    $config['system_root'] = isset($_SERVER['argv'][1]) ? $_SERVER['argv'][1] : '';
    if (empty($config['system_root'])) {
        echo "ERROR: You need to supply the path to the System Root as the first argument\n";
        usage();
        exit;
    }
    if (!is_dir($config['system_root']) || !is_readable($config['system_root'] . '/core/include/init.inc')) {
        echo "ERROR: Path provided doesn't point to a Matrix installation's System Root. Please provide correct path and try again.\n";
        usage();
        exit;
    }
    $config['assetids'] = isset($_SERVER['argv'][2]) ? trim($_SERVER['argv'][2]) : '';
    if (empty($config['assetids']) || strpos($config['assetids'], '--') === 0) {
        echo "\nWARNING: You are running this update lookup on the whole system.\nThis is fine but it may take a long time\n\nYOU HAVE 5 SECONDS TO CANCEL THIS SCRIPT... ";
        for ($i = 1; $i <= 5; $i++) {
            sleep(1);
            echo $i . ' ';
        }
        $config['assetids'] = '';
    }
    $config['batch_size'] = (int) get_parameterised_arg('--batch-size', 1000);
    $config['verbose'] = (int) get_boolean_arg('--verbose');
    echo "\n";
    echo "Updating lookups from asset(s): {$config['assetids']}.\n";
    echo "Batch size: {$config['batch_size']}\n";
    echo "\n";
}
/**
 * Process command line args into $config array.
 *
 * @param $config Global config.
 * @return void
 */
function process_args(&$config)
{
    $config['system_root'] = isset($_SERVER['argv'][1]) ? $_SERVER['argv'][1] : '';
    if (empty($config['system_root'])) {
        echo "ERROR: You need to supply the path to the System Root as the first argument\n";
        exit;
    }
    if (!is_dir($config['system_root']) || !is_readable($config['system_root'] . '/core/include/init.inc')) {
        echo "ERROR: Path provided doesn't point to a Matrix installation's System Root. Please provide correct path and try again.\n";
        exit;
    }
    $config['assetids'] = isset($_SERVER['argv'][2]) ? $_SERVER['argv'][2] : '';
    if (empty($config['assetids'])) {
        echo "ERROR: You need to specify the root nodes to apply the schema from as the second argument\n";
        exit;
    }
    //end if
    $config['schemaid'] = isset($_SERVER['argv'][3]) ? $_SERVER['argv'][3] : '';
    if (empty($config['schemaid'])) {
        echo "ERROR: You need to specify a schema to apply as the third argument\n";
        exit;
    }
    //end if
    $config['access'] = get_parameterised_arg('--access', 'granted');
    if ($config['access'] == 'denied') {
        $config['granted'] = FALSE;
    } else {
        $config['granted'] = TRUE;
    }
    //end if
    $config['cascades'] = !get_boolean_arg('-nc');
    // Invert.. -nc means no cascade.
    $config['force'] = get_boolean_arg('-f');
    $config['update_assets'] = get_boolean_arg('-u');
    $config['delete'] = get_boolean_arg('-d');
    $config['max_threads'] = (int) get_parameterised_arg('--max-threads', 3);
    if ($config['max_threads'] > 5) {
        $config['max_threads'] = 5;
    } elseif ($config['max_threads'] < 1) {
        $config['max_threads'] = 1;
    }
    //end if
    $config['batch_size'] = (int) get_parameterised_arg('--batch-size', 50);
    if ($config['batch_size'] < 5) {
        $config['batch_size'] = 50;
    }
    echo "\n";
    if ($config['delete'] === TRUE) {
        echo "Will attempt to delete schema {$config['schemaid']} from asset(s) {$config['assetids']}.\n\n";
        echo "  Update assets: {$config['update_assets']}\n";
        echo "  Max threads: {$config['max_threads']}\n";
        echo "  Batch size: {$config['batch_size']}\n";
    } else {
        echo "Will attempt to set schema {$config['schemaid']} to asset(s) {$config['assetids']}.\n\n";
        echo "  Access: {$config['access']}\n";
        echo "  Cascade: {$config['cascades']}\n";
        echo "  Force: {$config['force']}\n";
        echo "  Update assets: {$config['update_assets']}\n";
        echo "  Max threads: {$config['max_threads']}\n";
        echo "  Batch size: {$config['batch_size']}\n";
    }
    echo "\n";
    // Check for potential hazards
    if ($config['access'] == 'denied' && $config['force'] === TRUE) {
        echo "WARNING: You are attempting to 'force' application of a schema as 'denied', which can lead to unpredictable results.\n";
        echo "A schema already applied with 'grant' cannot be subsequently 'denied', it must first be removed.\n";
        // ask for the root password for the system
        echo 'Are you sure you want to continue? (y/N): ';
        $force_denied = rtrim(fgets(STDIN, 4094));
        if (strtoupper($force_denied) != 'Y') {
            exit;
        }
    }
}