#!/bin/bash
# ------------------------------------- USAGE ----------
function USAGE {
cat<<ENDUSAGE
Utility to help with routine Kubernetes/GCE operations when working with OVERCAST project.

USAGE: k8s-helper COMMAND [module_dir]

    list:                   list available modules
    create [module_dir]:    create Deployment Confs / Services / Routers into Kubernetes cluster
    cleanup [module_dir]:   remove Deployment Confs / Services / Routers from Kubernetes cluster

ENDUSAGE
}

SCRIPTDIR=$(dirname $0)
. $SCRIPTDIR/_environment
. $SCRIPTDIR/_functions

[ $CLUSTER_TYPE == k8s ] || die 4 "This script will work only for Kubernetes-based environments!"
# ------------------------------------- registration ----------
function registration {
    local ERR_CODE=0
    if [ -f register-k8s.yaml ]; then
        echo "===== Creating objects defined in register.yaml ===========================" 
        kube-create register-k8s.yaml
        ERR_CODE=$?
    fi
    if [ -f register-k8s.json ]; then
        echo "===== Creating objects defined in register.json ==========================="
        kube-create register-k8s.json
        ERR_CODE=$(($ERR_CODE+$?))
    fi
    # executing register.openshift LAST is deliberate: it may need to have commands to activate/start/deploy what was registered with YAML/JSON
    if [ -f register-k8s ]; then
        echo "===== Executing register.k8s =============================================="
        ./register-k8s
        ERR_CODE=$(($ERR_CODE+$?))
    fi
    if [ -d deploy ]; then
        for script in $(ls deploy/*.yaml); do
            cat "$script" | substitute | kubectl apply -f -
        done
    fi
    return $ERR_CODE
}

# ------------------------------------- update -------------
function update {
    local ERR_CODE=0
    if [ -d deploy ]; then
        for script in $(ls deploy/*.yaml); do
            cat "$script" | substitute | kubectl apply -f -
        done
    fi
    return $ERR_CODE
}


# -------------------------------------- cleanup ----------
function cleanup {
    if [ -f cleanup-k8s ]; then
        echo "===== Executing cleanup-k8s ========================================"
        ./cleanup-k8s
    elif [ -d deploy ]; then
        for script in $(ls deploy/*.yaml); do
            cat $script | substitute | kubectl delete -f -
        done
    fi
}

# --------------------------------------------------------------------------
# ---------------------------------= MAIN =---------------------------------
# --------------------------------------------------------------------------

COMMAND=$1
[ -z "$COMMAND" ] && USAGE && die 1 "Need at least one argument"
shift
[ ! -z "$1" ] && SELECTED_MODULE_DIR=$1 && shift

IMG_SRC_DIR=$BASE_DIR/images/service
if [ -z "$KUBECTL_VER" ]; then
    export KUBECTL_VER=latest
fi

case $COMMAND in
    list) # ---------------------------------------------------------------------------
        iterate-directories $IMG_SRC_DIR
        ;;
    create) # -------------------------------------------------------------------------
        if [[ -n SELECTED_MODULE_DIR ]]; then
            iterate-directories $IMG_SRC_DIR registration
        else
            USAGE
            die 22 "Command not recognized"
        fi
        ;;
    update) # -------------------------------------------------------------------------
        if [[ -n SELECTED_MODULE_DIR ]]; then
            iterate-directories $IMG_SRC_DIR update
        else
            USAGE
            die 22 "Command not recognized"
        fi
        ;;
    cleanup) # ------------------------------------------------------------------------
        if [[ -n SELECTED_MODULE_DIR ]]; then
            iterate-directories $IMG_SRC_DIR cleanup
        else
            USAGE
            die 22 "Command not recognized"
        fi
        ;;
    mount) #---------------------------------------------------------------------------
        bucket=$SELECTED_MODULE_DIR
        mnt=$1
        [ -z "$bucket" ] && die 22 "Must specify bucket (etc or abrt)"
        [ -z "$mnt" ] && die 22 "Must specify mount point"
        $BASE_DIR/images/shared-files/start-fuse-mount $bucket --foreground rw $mnt
        ;;
    *) # ------------------------------------------------------------------------------
        USAGE
        die 1 "COMMAND not recognized"
esac

echo ============ DONE ===============


