#!/bin/sh
# -*- tab-width:4;indent-tabs-mode:nil -*-
# ex: ts=4 sw=4 et

# Pull environment for this install
. "/usr/local/lib/riak-cs/lib/env.sh"

# Make sure the user running this script is the owner and/or su to that user
check_user "$@"

# Make sure CWD is set to runner run dir
cd $RUNNER_BASE_DIR

# Identify the script name
SCRIPT=`basename $0`

usage() {
    echo "Usage: $SCRIPT { status | gc | access | storage | stanchion | cluster-info | cleanup-orphan-multipart | audit-bucket-ownership}"
}

# Check the first argument for instructions
case "$1" in
    status)
        shift
        node_up_check
        $NODETOOL rpc riak_cs_console status
        ;;
    gc)
        shift
        case "$1" in
            batch|status|pause|resume|cancel|set-interval|set-leeway|earliest-keys)
                # Make sure the local node IS running
                node_up_check

                $NODETOOL rpc riak_cs_gc_console "$@"
                ;;
            *)
                echo "Usage: $SCRIPT gc { batch [<leeway_seconds>|--help] | status | pause | resume | cancel |"
                echo "                          set-interval <interval_seconds> | set-leeway <leeway_seconds> |"
                echo "                          earliest-keys [bag names]}"
                exit 1
                ;;
        esac
        ;;
    storage)
        shift
        case "$1" in
            batch|status|pause|resume|cancel)
                # Make sure the local node IS running
                node_up_check

                $NODETOOL rpc riak_cs_storage_console "$@"
                ;;
            *)
                echo "Usage: $SCRIPT storage $1 { batch | status | pause | resume | cancel }"
                exit 1
                ;;
        esac
        ;;
    access)
        shift
        case "$1" in
            flush)
                # Make sure the local node IS running
                node_up_check

                $NODETOOL rpc riak_cs_access_console flush "$@"
                ;;
            *)
                echo "Usage: $SCRIPT access $1 { flush }"
                exit 1
                ;;
        esac
        ;;
    stanchion)
        shift
        case "$1" in
            switch|show)
                # Make sure the local node IS running
                node_up_check

                $NODETOOL rpc riak_cs_stanchion_console "$@"
                ;;
            *)
                echo "Usage: $SCRIPT stanchion $1 { switch HOST PORT | show }"
                exit 1
                ;;
        esac
        ;;
    cluster[_-]info)
        if [ $# -lt 2 ]; then
            echo "Usage: $SCRIPT $1 <output_file>"
            exit 1
        fi
        shift

        # Make sure the local node is running
        node_up_check

        $NODETOOL rpc_infinity riak_cs_console cluster_info "$@"
        ;;
    cleanup[_-]orphan[_-]multipart)
        shift

        # Make sure the local node IS running
        node_up_check

        $NODETOOL rpc riak_cs_console cleanup_orphan_multipart "$@"
        ;;
    audit[_-]bucket[_-]ownership)
        shift
        node_up_check
        $NODETOOL rpc riak_cs_console audit_bucket_ownership "$@"
        ;;
    *)
        usage
        exit 1
        ;;
esac
