#!/usr/bin/bash

# SHLINT:yes
# Copyright (c) 2020-2021 by NetApp, Inc.
# All Rights Reserved.

#
# This script:
#   1. Removes the partition from kmip.db
#
# Note: server.cfg entries associated with the partition are removed
#       automatically in kmipsrvmod_mop_epk.c
#
#   2. Removes the associated part-*.json files from the db/data directory
#
# usage:
#   ./netapp_disable_AKV.sh <partition> <vserver-servers.cfg-path>
#
# where:
#   <partition>                 is the partition name (usually corresponds to the vserver UUID)
#   <vserver-servers.cfg-path>  path to vserver's servers.cfg file
#
readonly KMIP_SERVER_DIR="/cfcard/kmip/server"
readonly KMIP_SERVERS_CFG="servers.cfg"
readonly DB_DATA_DIR="db/data"
readonly LOG_DIR="/mroot/etc/log/mlog"

readonly LOG_FILE="${KMIP_SERVER_DIR}/netapp_disable_AKV.log"

readonly AWK="/usr/bin/awk"
readonly GREP="/usr/bin/grep"
readonly RM="/bin/rm"
readonly SUDO_VCONTEXT="sudo vcontext -v 4294967293"

readonly DELETE_EKMIP_PARTITION="/usr/sbin/delete_ekmip_partition"

readonly N_REQUIRED_ARGS=2

PWD=`pwd`

function check_prerequisites
{
    local progname=$1
    local n_args=$2
    local retVal=0

    if [[ ! -d ${KMIP_SERVER_DIR} ]]; then
        retVal=1
    fi

    if [[ $n_args -ne ${N_REQUIRED_ARGS} ]]; then
        echo "usage: $progname <partition_name> <cfg_file>"
        retVal=2
    fi

    return $retVal
}

function remove_partition
{
    local partition=$1
    local cfg_file=$2
    local server=""
    local retVal=0

    # remove partition from kmip.db
    if [[ ! -f ${cfg_file} ]]; then
        echo "Error: ${cfg_file} not present at `date` (`date +%s`)" >> ${LOG_FILE}
        retVal=1
        return $retVal
    fi

    server=`${GREP} host ${cfg_file} | ${GREP} 127.0.0.1 | ${AWK} -F'.' '{print $1}'`
    if [[ -z $server ]]; then
        echo "Error: could not find .host value from ${cfg_file} at `date` (`date +%s`)."  >> ${LOG_FILE}
	echo "${cfg_file} contents:" >> ${LOG_FILE}
	cat ${cfg_file} >> ${LOG_FILE}
        retVal=2
        return $retVal
    fi

    ${SUDO_VCONTEXT} ${DELETE_EKMIP_PARTITION} ${DELETE_EKMIP_PARTITION} $server $partition cfg=${cfg_file}
    retVal=$?

    return $retVal
}

function remove_json_file
{
    local partition=$1
    local retVal=0

    # remove json file from db/data
    sudo ${RM} -f ${DB_DATA_DIR}/part-${partition}.json
    retVal=$?

    return $retVal
}

function remove_traffic_log_file
{
    local partition=$1
    local retVal=0

    # remove json file from db/data
    sudo ${RM} -f ${LOG_DIR}/traffic-${partition}.log
    retVal=$?

    return $retVal
}

function perform_cleanup
{
    local partition=$1
    local cfg_file=$2
    local retVal=0

    cd ${KMIP_SERVER_DIR}

    #fall through even on errors
    remove_partition $partition $cfg_file
    retVal=$?
    if [[ $retVal -ne 0 ]]; then
        echo "Warning: could not remove partition data for ${partition} at `date` (`date +%s`)."  >> ${LOG_FILE}
    fi

    remove_traffic_log_file $partition
    retVal=$?
    if [[ $retVal -ne 0 ]]; then
        echo "Warning: could not remove log file for ${partition} at `date` (`date +%s`)." >> ${LOG_FILE}
    fi

    remove_json_file $partition
    retVal=$?
    if [[ $retVal -ne 0 ]]; then
        echo "Warning: could not remove json file for ${partition} at `date` (`date +%s`)." >> ${LOG_FILE}
    fi

    echo "Removed $partition at `date` (`date +%s`)." >> ${LOG_FILE}

    cd $PWD
    return $retVal
}

check_prerequisites $0 $#
if [[ $? -ne 0 ]]; then
    exit $?
fi

perform_cleanup $1 $2
exit $?
