########
# Mongo
function directmod_mongo_shards {

    local QUERY="var cursor = db.getSiblingDB('config').shards.find(); while ( cursor.hasNext() ) { printjson( cursor.next() ); }";

    directmod_mongo prod moddb_ro "$QUERY";
}

function directmod_mongo {

    # mo test root
    # test - db enviromebt (dev, test, test2, prod)
    # root is user
    # query

    local DBNAME='moddb';
    local USER='moddb';
    local PASSWORD='uiFeizumu0';
    local HOST='localhost';
    local ROLE='admin';
    local short_hostname=`hostname --short`;
    local QUERY='';

    if [ -n "$2" ]; then
        USER=$2;
    fi;

    if [ -n "$3" ]; then
        QUERY=$3;
    fi;

    if [ "$1" = 'dev' ]; then
        HOST='localhost';
    elif [ "$1" = 'test' ]; then
        HOST='direct-mod-test';
    elif [ "$1" = 'test2' ]; then
        HOST='direct-mod-test2';
    elif [ "$1" = 'prod' ]; then
        # moddb-mongo-standby01i.yandex.ru 27700 - shard 1
        # moddb-mongo-standby02i.yandex.ru 27701 - shard 2
        # moddb-mongo-standby03i.yandex.ru 27702 - shard 3
        # mongo --norc --ipv6 --host moddb-mongo-standby01i.yandex.ru --port 27017 -u moddb_ro -p uiFeizumu0 --authenticationDatabase admin moddb
        HOST='direct-mod';
        USER='moddb_ro';
    elif [ -n "$1" ]; then
        HOST=$1;
    else
        echo -e "\e[41mWrong host. $HOST will be used instead.\e[0m" 1>&2;
        echo 1>&2;
    fi;

    if [ -n "$HOST" -a $HOST != "${short_hostname}" -a $HOST != 'localhost' ]; then
        USER='moddb_ro';
    fi;

    if [ -z "$NOECHO" -a "${HOST}" != 'direct-mod' ]; then
        echo "mongo ${HOST}/${DBNAME} -u ${USER} -p ${PASSWORD} --authenticationDatabase ${ROLE}";
    fi;

    if [ "${HOST}" != 'direct-mod' ]; then
        if [ -z "${QUERY}" ]; then
            mongo $HOST/$DBNAME -u $USER -p $PASSWORD --authenticationDatabase $ROLE;
        else
            mongo $HOST/$DBNAME -u $USER -p $PASSWORD --authenticationDatabase $ROLE --eval "$QUERY"
        fi;
    else

        HOST=modback01g.yandex.ru;

        ping -c 1 $HOST 2>&1 >&/dev/null;

        if [ "$?" != 0 ]; then
            HOST=modback01e.yandex.ru;
        fi;

        if [ -z "${QUERY}" ]; then
            # пока на локальном порту 27018 живёт mongos смотрящий в продакшен
            # mongo -u $USER -p $PASSWORD --host localhost --port 27018 --authenticationDatabase admin
            # mongo localhost:27018/$DBNAME -u $USER -p $PASSWORD --authenticationDatabase admin
            mongo $HOST:27017/$DBNAME -u $USER -p $PASSWORD --authenticationDatabase admin
        else
            # mongo -u $USER -p $PASSWORD --host localhost --port 27018 --authenticationDatabase admin --eval "$QUERY"
            # mongo localhost:27018/$DBNAME -u $USER -p $PASSWORD --authenticationDatabase admin --eval "$QUERY"
            mongo $HOST:27017/$DBNAME -u $USER -p $PASSWORD --authenticationDatabase admin --eval "$QUERY"
        fi;

        # mongo --norc --ipv6 --host moddb-mongo-standby01i.yandex.ru --port 27017 -u $USER -p $PASSWORD --authenticationDatabase $ROLE $DBNAME;
    fi;
}



#############
# SVN helpers

export MT=svn+ssh://svn.yandex.ru/direct-moderate/trunk
export MB=svn+ssh://svn.yandex.ru/direct-moderate/branches
export MRT=svn+ssh://svn.yandex.ru/direct-moderate


