#!/bin/bash
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
change_flag='n'
info() {
echo -e "${GREEN}[INFO]${NC} $1"
}
warn() {
echo -e "${YELLOW}[WARN]${NC} $1"
}
error() {
echo -e "${RED}[ERROR]${NC} $1"
}
validate_install_dir() {
local base_dir="$1"
if [[ ! -d "$base_dir" ]]; then
error "Specified directory does not exist: $base_dir"
exit 1
fi
INSTALL_MODULE_DIR="$base_dir/tools/operator_cmp"
info "Checking for operator_cmp at: $INSTALL_MODULE_DIR"
if [[ ! -d "$INSTALL_MODULE_DIR" ]]; then
return 2
fi
if [[ ! -w "$(dirname "$INSTALL_MODULE_DIR")" ]]; then
chmod +w "$(dirname "$INSTALL_MODULE_DIR")"
change_flag='y'
fi
return 0
}
check_write_permission() {
if [[ ! -w "$INSTALL_MODULE_DIR" ]]; then
error "No permission to delete $INSTALL_MODULE_DIR"
error "Current user: $(whoami)"
error "Please run uninstall with a user that has permissions"
error "or manually delete: rm -rf $INSTALL_MODULE_DIR"
return 1
fi
return 0
}
perform_uninstall() {
info "Uninstalling mindstudio-accucmp..."
info "Install path: $INSTALL_MODULE_DIR"
rm -rf "$INSTALL_MODULE_DIR"
rc=$?
if [ "$change_flag" = "y" ]; then
chmod -w "$(dirname "$INSTALL_MODULE_DIR")"
fi
if [ $rc -eq 0 ]; then
info "mindstudio-accucmp has been successfully removed"
return 0
else
error "Deletion failed, please check permissions"
return 1
fi
}
main() {
echo "========================================="
echo " mindstudio-accucmp Uninstaller"
echo "========================================="
local _script_dir=$(cd "$(dirname "$0")" && pwd)
local _install_dir=$(cd "${_script_dir}/../../.." && pwd)
info "Script directory: ${_script_dir}"
info "INSTALL_DIR: ${_install_dir}"
local _cann_uninstall="${_install_dir}/cann_uninstall.sh"
validate_install_dir "${_install_dir}"
local _validate_result=$?
if [ ${_validate_result} -eq 2 ]; then
info "Nothing to uninstall, exiting"
rm -rf "${_install_dir}/share/info/operator_cmp"
if [ -f "${_cann_uninstall}" ]; then
sed -i "/uninstall_package \"share\/info\/operator_cmp\"/d" "${_cann_uninstall}"
fi
exit 0
fi
if [ ${_validate_result} -ne 0 ]; then
exit 1
fi
if ! check_write_permission; then
exit 1
fi
if perform_uninstall; then
info "Uninstall completed!"
info "mindstudio-accucmp has been successfully removed"
else
error "Error occurred during uninstall"
exit 1
fi
rm -rf "${_install_dir}/share/info/operator_cmp"
if [ -f "${_cann_uninstall}" ]; then
sed -i "/uninstall_package \"share\/info\/operator_cmp\"/d" "${_cann_uninstall}"
fi
}
main "$@"