#!/bin/sh
FILE=/usr/lib/ncinfo.xml
ncv="NC-OSS-5.3.6-64"
PRODUCT="NCache"
InstallType=
REMOVEUSER="true"
NCUSER="ncache"
LUCENE_DIST_MAP_PATH="/usr/share/ncache"

#--------------------------------------------------------------------------------------
printMessage ()
{
	message=$1
	echo "$message"
}
#--------------------------------------------------------------------------------------
printError ()
{
	message=$1
	echo "------"
	echo "ERROR: $message" 1>&2;
	echo ""
}
#--------------------------------------------------------------------------------------
printMode ()
{
	message=$1
	if [ ! -z "$VERBOSE" ]
	then
		echo "$message"
	fi
}
#--------------------------------------------------------------------------------------
usage()
{
	echo "This script uninstalls NCache."
	echo ""
	echo "Usage: uninstall [OPTIONS]"
	echo ""
	echo "  -p, --path		NCache install directory. If not provided, then it tries to use the default path (/opt/ncache)"
	echo "  -r, --removeuser 	Do not remove user '$NCUSER' on uninstallation"
	echo "  -F, --force		Suppresses all prompts."
	echo ""
	exit 1
}
#--------------------------------------------------------------------------------------
uninstall()
{
	rm /etc/systemd/system/ncached.service
	
	if [ $REMOVEUSER = "true" ]; then
		userdel "$NCUSER" -f 2> /dev/null
		groupdel $NCUSER -f 2> /dev/null 
		printMessage "Removed '$NCUSER' user"
	fi

	rm /usr/lib/ncinfo.xml
	rm -r $NCHOME
}
#--------------------------------------------------------------------------------------
reconfirm()
{
	read -p "Are you sure you want to uninstall? (y/n)" yn
	case $yn in
		y )
			printMessage "Uninstalling NCache..."
			;;
		n )
			printMessage "Uninstallation cancelled."
			echo ""
			exit
			;;
		* )
			printMessage "Invalid argument."
			echo ""
			exit
			;;
	esac
}
#--------------------------------------------------------------------------------------
stopservice()
{
	systemctl disable ncached.service
	systemctl stop ncached.service

	pgrep -f Alachisoft.NCache.CacheHost | while read -r line ; do
		kill -9 $line 
	done
	
	pgrep -f dotnet.*Alachisoft.NCache.ManagementCenter.dll | while read -r line ; do
		kill -9 $line 
	done

	
	systemctl daemon-reload
}

PostUnInstallationActivity ()
{
	NumberOfCPU="$(grep processor /proc/cpuinfo | wc -l)"
	NumberofCores="$(nproc --all)"
	NumberOfVCPU="$(grep processor /proc/cpuinfo | wc -l)"

	Ram="$(cat /proc/meminfo | grep MemTotal | grep -o -E '[0-9]+')"
	Ram=$((Ram/(1024*1024)))

	OSCode="`uname`"
	case $OSCode in
	  'Linux')
		OSCode='12'
		;;
	  *)
		OSCode="-1"
		;;
	esac

	mi=$OSCode":"$NumberOfCPU":"$NumberofCores":"$NumberOfVCPU":"$Ram
	curl --silent --output /dev/null --data "fn=$FirstName&ln=$LastName&email=$Email&company=$Company&ncv=$ncv&edt=$InstallType&netv=$versionFound&mi=$mi&act=$InstallCode" https://app.alachisoft.com/ncache/nc-getting-started.php
}
#--------------------------------------------------------------------------------------
#--- Start of execution
#--------------------------------------------------------------------------------------
if test -f "$FILE"; then
	Email=$(grep -oP '(?<=email>)[^<]+' "/usr/lib/ncinfo.xml")
	FirstName=$(grep -oP '(?<=first-name>)[^<]+' "/usr/lib/ncinfo.xml")
	LastName=$(grep -oP '(?<=last-name>)[^<]+' "/usr/lib/ncinfo.xml")
	Company=$(grep -oP '(?<=company>)[^<]+' "/usr/lib/ncinfo.xml")
	InstallCode=$(grep -oP '(?<=install-code>)[^<]+' "/usr/lib/ncinfo.xml")
	
	InstallType="Unknown"
	case $InstallCode in
	"p2exR3dHd2GQ4DGwg7cSMxP3N5a3")
		InstallType="server"
		;;
	"p2exR7d35mGQ4DGwg7cSMxP3N5a3" )
		InstallType="client"
		;;
	esac
fi
#--------------------------------------------------------------------------------------
# if [ ! -f "/usr/lib/ncinfo.xml" ]; then 
# 	echo ""
# 	printError "NCache is not installed on your machine or installation is corrupt"
# 	exit 1
# fi
	
NCHOME=$(awk -F '[<>]' '/install-dir/{print $3}' /usr/lib/ncinfo.xml) 2> /dev/null

#--------------------------------------------------------------------------------------
while [ "$1" != "" ]; 
do
    case $1 in
    -p | --path ) shift
           NCPATH=$1           
           ;;
    -F | --force )
           FORCE="y"
           ;;
					
	-r | --removeuser )
			REMOVEUSER="false"
			;;
			
	-h | --help )
			usage
			exit
			;;

    * )
		usage
        exit 
        ;;
    esac
    shift
done

#--------------------------------------------------------------------------------------
#--- Make sure only root can run our script
if [ "$(id -u)" != "0" ]; then
   printError "Installation requires root privileges."
   exit 1
fi

#--------------------------------------------------------------------------------------
if [ -z "$NCPATH" ]; then
   if [ -z "$NCHOME" ]; then
      printError "No install path provided with -p or --path and NCHOME is also not set. Aborting NCache uninstallation..."
      exit 1
   fi
   NCPATH="$NCHOME";
   printMessage "NCHOME found, uninstalling NCache from location: $NCHOME"
fi
#--------------------------------------------------------------------------------------
if [ ! -f "$NCPATH/lib/Alachisoft.NCache.Common.dll" ]; then 
	echo ""
	printError "Provided installation path is not valid."
	exit 1
fi

if [ ! -f "/usr/lib/ncinfo.xml" ]; then 
	echo ""
	printError "NCache is not installed on this machine or installation is corrupt"
	exit 1
fi
#--------------------------------------------------------------------------------------
if [ -z "$FORCE" ]; then
	reconfirm
fi


CURLPATH=$(command -v curl)
if [ -n "$CURLPATH" ]; then	
	PostUnInstallationActivity
fi

printMessage "Stopping & Removing NCache Daemon(s)..."
stopservice 
uninstall

#--------------------------------------------------------------------------------------
printMessage "NCache uninstalled."
printMessage ""
printMessage "For complete documentation visit the website https://www.alachisoft.com/resources/docs"
printMessage ""
#--------------------------------------------------------------------------------------
exit 0
