#!/bin/sh

## -------------------------------------------------------------------
##
## riak-cs-admin: Administrative tasks on Riak CS
##
## Copyright (c) 2014 Basho Technologies, Inc.,
##               2021-2023 TI Tokyo.  All Rights Reserved.
##
## This file is provided to you under the Apache License,
## Version 2.0 (the "License"); you may not use this file
## except in compliance with the License.  You may obtain
## a copy of the License at
##
##   http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing,
## software distributed under the License is distributed on an
## "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
## KIND, either express or implied.  See the License for the
## specific language governing permissions and limitations
## under the License.
##
## -------------------------------------------------------------------

SCRIPT="riak-cs-admin"

PLATFORM_BASE_DIR=/usr/lib64/riak-cs
PLATFORM_BASE_DIR=${PLATFORM_BASE_DIR:-$(cd $(dirname "$0")/.. && pwd -P)}

PLATFORM_BIN_DIR=/usr/lib64/riak-cs/bin
if [ "$PLATFORM_BIN_DIR" = "${PLATFORM_BIN_DIR#/}" ]; then
    PLATFORM_BIN_DIR=$PLATFORM_BASE_DIR/$PLATFORM_BIN_DIR
fi

PLATFORM_LIB_DIR=/usr/lib64/riak-cs/lib
if [ "$PLATFORM_LIB_DIR" = "${PLATFORM_LIB_DIR#/}" ]; then
    PLATFORM_LIB_DIR=$PLATFORM_BASE_DIR/$PLATFORM_LIB_DIR
fi
. $PLATFORM_LIB_DIR/lib.sh

export USE_NODETOOL=1  # {use_nodetool, true} is not clear enough

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

# Check the first argument for instructions
case "$1" in
    version)
        shift
        rpc riak_cs_console version
        ;;
    status)
        shift
        rpc riak_cs_console status
        ;;
    supps)
        shift
        rpc riak_cs_console supps $@
        ;;
    gc)
        shift
        case "$1" in
            batch|status|pause|resume|cancel|set-interval|set-leeway|earliest-keys)
                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)
                rpc riak_cs_storage_console "$@"
                ;;
            *)
                echo "Usage: $SCRIPT storage $1 { batch | status | pause | resume | cancel }"
                exit 1
                ;;
        esac
        ;;
    access)
        shift
        case "$1" in
            flush)
                rpc riak_cs_access_console flush "$@"
                ;;
            *)
                echo "Usage: $SCRIPT access $1 { flush }"
                exit 1
                ;;
        esac
        ;;
    stanchion)
        shift
        case "$1" in
            show)
                rpc riak_cs_stanchion_console "$@"
                ;;
            *)
                echo "Usage: $SCRIPT stanchion $1 show"
                exit 1
                ;;
        esac
        ;;
    'test')
        shift
        rpc riak_cs_console test
        ;;
    cluster[_-]info)
        if [ $# -lt 2 ]; then
            echo "Usage: $SCRIPT $1 <output_file>"
            exit 1
        fi
        shift

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

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

# No explicit exit from within script or nodetool, assumed to have succeeded.
exit 0
