#!/usr/bin/php
<?php
set_include_path(get_include_path() . PATH_SEPARATOR . '../include');
set_include_path(get_include_path() . PATH_SEPARATOR . '/usr/share/shard-query/include');
require_once 'Console/Getopt.php';
require_once 'shard-query.php';
require_once 'common.php';
$verbose=false;

main();
exit;

function help() {
        shard_query_common_help();
        run_query_help();
        echo "\n";
        exit;
}

function main() {
        $params = get_commandline(array("table=","delimiter=","schema="));
	if(!empty($params['table'])) {
		$table = $params['table'];
	} else {
		echo "Table name (--table) is required\n";
		exit(-1);
	}

	if(!empty($params['schema'])) {
		$schema = $params['schema'];
	} else {
		$schema = null;
	}

	if(!empty($params['delimiter'])) {
		$delimiter = $params['delimiter'];
	} else {
		$delimiter = "\\t";
	}
#	echo "LOAD FROM STDIN: TABLE: $table DELIMITER: $delimiter\n";

	$fh = fopen('php://stdin', 'r') or die('Could not open STDIN');
	$SQ = new ShardQuery($schema);
	$SL = new ShardLoader($SQ, $delimiter, "", "\n", true, null, null, null, null, "", "\\n");
	$SL->load_from_handle($fh, $table);
	if(!empty($SL->errors)) { print_r($SL->errors); exit(1); }
#	echo "LOAD_FROM_STDIN: Done!\n";
}

