#!/bin/sh
#
# Uninstall AntiVir for UNIX
#
# Copyright (c) 2011 Avira Operations GmbH & Co. KG and its Licensors
#

OS=`uname | env LANG=C LC_ALL=C LC_CTYPE=C tr '[:upper:]' '[:lower:]'`

AVDIR="/usr/lib/AntiVir"
BACKUP_NAME="avira"
BACKUPDIR="/var/backups/${BACKUP_NAME}"
SET_LIBRARY_PATH=""

case "${OS}" in
	sunos)
		PROCCMD="ps -ef"
		PROCCMD="ps -ef -o pid,comm"
		ECHO="echo"
		RM_DIR="-r"
		SET_LIBRARY_PATH="env LD_LIBRARY_PATH="/usr/local/lib:/usr/lib/AntiVir/agent""
		
		;;
	*)
		PROCCMD="ps axw"
		PROCCMD="ps axw -o pid,command"
		ECHO="echo"
		RM_DIR="-rd"
		;;
esac


getPROCESSLIST()
{
	PROCESSLIST=""
	for PID in `$PROCCMD | grep "/usr/lib/AntiVir/agent/agent" | grep -v uninstall | grep -v grep | awk -F' ' '{ print $1 }'`
	do
		PROCESSLIST="$PROCESSLIST $PID"
	done
}


uninstall_plugin()
{
	${AVDIR}/agent/smc-agent status > /dev/null 2>&1
	if [ "${?}" -ne "0" ]
	then
		for SEMID in 0xe9b8b194 0x36bbbe6d 0x36bbbe6e
		do
			if ipcs | grep ${SEMID}  > /dev/null 2>&1
			then
				${ECHO} -n "Remove semaphore id ${SEMID}"
				ipcrm -s `ipcs | grep ${SEMID} | awk -F' ' '{ print $2 }'` > /dev/null 2>&1
				${ECHO} "done"
			fi
		done
	fi

	${SET_LIBRARY_PATH} ${AVDIR}/agent/agent --remove-plugin ${PLUGIN_NAME} > /dev/null 2>&1

	grep "${PLUGIN_NAME}" /etc/avira/agent.ini > /dev/null 2>&1
	if [ "${?}" -eq 0 ]
	then
		/bin/sh ${AVDIR}/agent/smc-agent stop > /dev/null 2>&1
		if [ ${?} -ne 0 ]
		then
			getPROCESSLIST
			kill ${PROCESSLIST}
		fi
		
		cp /etc/avira/agent.ini /etc/avira/agent.ini.orig
		cat /etc/avira/agent.ini.orig | grep -v ${PLUGIN_NAME}=${AVDIR}/agent/${ITEM} > /etc/avira/agent.ini
		rm /etc/avira/agent.ini.orig
		
		/bin/sh ${AVDIR}/agent/smc-agent start > /dev/null 2>&1
	fi

	return 0
}



# main
	for ITEM in $@
	do
		PLUGIN_NAME=`${ECHO} ${AVDIR}/agent/${ITEM} | sed -e "s/^.*ux/ux/; s/\.so$//"`
		uninstall_plugin
	done
	
	exit 0
# AVCS4F3A4200C37O030000000203000002A4010000000000000000000000000303000000650300000200P000000000000000065DD07709DEF1579EEBE4DD8E35C16B1A20612B4363453CB266B85E93973DCE71B100CC78E4D6B070883B3752874422AB43F1AC6FC24748F39267EC1BE7B5F66DF1C69026B6C1CAF880E49B56DA3FDB9736E2C61FB15DA06F70EE0B8A766ED8873B9ED60C425BE8A13BA3A74151449AF72EF04FD5F37A53DAD6848A12CA6E86E22AD4DA3BCF4AEE5A70A65B14F80B52E17BBFB429F8CA2863972B0FE4BA49BE2BFDC082E1859F4D9E01DC03F43A5BCB2C05D7A56B09F46B46EC2DA35DB0FCE1CC51C13E9D00651FD57EF54DF5F171F49DFB9C35B31BC8A43917ADD6F89578C9B1F6505459523295583429C4354956FBED58D56A954E9EC4A420FD9C7B403AEAA00000000000000000000000000000292030000000003T62F3AB0132FAVCSE