#!/bin/bash
#
# Copyright (c) 2019-2020 Oracle and/or its affiliates. All rights reserved.
# Licensed under the GNU General Public License Version 2 as shown at https://oss.oracle.com/licenses/GPL-2.

whitelist="
kubeadm
kubelet
istio-istioctl
istio
"
set -x
# Block multiple packages being installed at the same time
invalid_chars=( ' ' ',' )

for invalid_char in "${invalid_chars[@]}"; do
    if echo "$1" | grep -q "${invalid_char}"; then
       	echo "'$1' contains an invalid character: '${invalid_char}'"
	    exit 1
    fi
done

match=false
# Check if we have a whitelist match
for whitelist_item in ${whitelist}; do
    if echo "$1" | grep -qx -E "${whitelist_item}"; then
        match=true
        break
    fi
done

if [[ ${match} == false ]]; then
	echo "$1 is not permitted to be uninstalled by this tool"
	exit 1
fi

if [[ "$1" == kubelet ]]; then
	olversion=$(uname -r | grep el8)
	if [[ -z "$olversion" ]]; then
        	yum remove -y "$1" cri-o conmon cri-tools
	else
		# for OL8 we cannot remove conmon, as doing so will
		# remove podman and olcne-agent
		yum remove -y "$1" cri-o cri-tools	
	fi
else
	yum remove -y "$1"
fi
exit $?
