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


# set default variable values
OS=`uname | env LANG=C LC_ALL=C LC_CTYPE=C tr '[:upper:]' '[:lower:]'`
AVDIR_BASE="/opt/avira"
LAUNCHDDIR="/Library/LaunchDaemons"
AVGAURDDIR_BASE="/Applications/Avira.app/Contents"
AVDIR="${AVDIR_BASE}"
PRODUCT="agent"
COMPACT_LIST="0"
SILENCE="0"
PROCESS_FOUND="0"
KEY_FOUND="0"
AGENT_INSTALLED="0"
GUARD_INSTALLED="0"

INSTALLED_PRODUCTS=""
SMC_PLUGIN=""
RCSCRIPT=""
RCDIR=""
SYSTEM=""
MAIN_CONF_FILES=""


f_echo_sun()
{
	# this script comes from the man page of echo(1) on Solaris 9
	
	if [ "X${1}" = "X-n" ]
	then
		shift
		printf "%s" "${*}"
	else
		printf "%s\n" "${*}"
	fi
}


f_echo_me()
{
	if [ "${SILENCE}" -eq "0" ]
	then
		if [ "${OS}" = "sunos" -o "${OS}" = "darwin" ]
		then
			f_echo_sun ${*}
		else
			if [ "X$1" = "X-n" ]
			then
				shift
				echo -n "${*}"
			else
				echo "${*}"
			fi
		fi
	fi
}


f_check_if_product_installed()
{
	TEMP1="`echo ${INSTALLED_PRODUCTS} | env LANG=C LC_ALL=C LC_CTYPE=C tr '[:upper:]' '[:lower:]'`"

	echo "${TEMP1}" | grep "${PRODUCT}" > /dev/null 2>&1
	if [ ${?} -ne 0 ]
	then
		f_echo_me "Product not installed: ${PRODUCT}"
		exit 1
	fi

	if [ -z "${PRODUCT}" ]
	then
		f_echo_me "no product specified."
		exit 1
	fi
}


f_check_installed_products()
{
# 	products with the subdir in /usr/lib/AntiVir/*
	if [ -x "${AVDIR_BASE}/agent/agent" ]
	then
		INSTALLED_PRODUCTS="${INSTALLED_PRODUCTS} Agent"
		GUARD_INSTALLED="1"
	fi

	f_check_if_product_installed
}


f_root_check()
{
	ID=`id`
	CUT=`echo $ID | sed -e 's,uid=0(,,'`
	if [ "$CUT" = "$ID" ]
	then
		CUT=`echo $ID | sed -e 's,\euid=0(,,'`
		if [ "$CUT" = "$ID" ]
		then
			f_echo_me " "
			f_echo_me "This script must be run as root."
			f_echo_me " "
			exit 0
		fi
	fi
}


f_set_product_variables()
{
# 	set some product specific default values for the needed variables

	case "${PRODUCT}" in
# 		products with a sub-directory in /usr/lib/AntiVir/
		agent)
			FULLPRODUCTNAME="Avira AMC Agent"
			AVDIR="${AVDIR_BASE}/agent"
			PRODUCT_BIN="${AVDIR}/agent"
			ALL_PLIST_ITEM="com.avira.amc.agent"
			;;
	esac
}


f_remove_launchd_entries()
{
	for PLIST_ITEM in ${ALL_PLIST_ITEM}
	do
		if [ -f "${LAUNCHDDIR}/${PLIST_ITEM}.plist" ]
		then
			if launchctl list ${PLIST_ITEM} > /dev/null 2>&1
			then
				launchctl unload ${LAUNCHDDIR}/${PLIST_ITEM}.plist
			fi
	
			rm ${LAUNCHDDIR}/${PLIST_ITEM}.plist
		fi
	done
}


f_restore_launchd_entries()
{
	LAUNCHDDIR="/Library/LaunchAgents"

	for DEFAULT_PLIST in odscan.template update.template
	do
		if [ -f "${AVGAURDDIR_BASE}/Resources/av_scheduler_plugin/default_jobs/${DEFAULT_PLIST}" ]
		then
			case ${DEFAULT_PLIST} in
				*odscan.template)
					DEFAULT_PLIST_NEW="com.avira.antivirus.odscan.default"
					;;

				*update.template)
					DEFAULT_PLIST_NEW="com.avira.antivirus.update.default"
					;;
			esac

			cp ${AVGAURDDIR_BASE}/Resources/av_scheduler_plugin/default_jobs/${DEFAULT_PLIST} ${LAUNCHDDIR}/${DEFAULT_PLIST_NEW}.plist

# 			check for the user which is currently logged on on the gui and unload those jobs
			LOGGED_ON_USERS=$(stat -f%Su /dev/console)

			sudo su - ${LOGGED_ON_USERS} -c 'launchctl load -S Aqua '${LAUNCHDDIR}'/'${DEFAULT_PLIST_NEW}'.plist'
		fi
	done
}


f_remove_agent_log_dir()
{
	if [ ! -d "/var/log/avira/agent/" ]
	then
		return 0
	fi

	f_echo_me ""
	f_echo_me "remove AMC server log files"
	rm -r /var/log/avira/agent/
}


f_unregister_agent()
{
# 	here we can not perform any checks, because the agent exit code is always 0, even when the agent fails
	f_echo_me " "
	f_echo_me -n "unregister ${FULLPRODUCTNAME} from AMC server ... "
	${AVDIR}/agent -u > /dev/null 2>&1
	f_echo_me "done"
}


f_unregister_smc_plugins()
{
	f_echo_me "unregistering existing AMC plugins ... "

	for KNOWN_SMC_PLUGINS in osxsrv osxwks
	do
		f_echo_me -n "check for existing AMC plugins (lib${KNOWN_SMC_PLUGINS}) ... "
		if [ -f "${AVGAURDDIR_BASE}/guard/agent/lib${KNOWN_SMC_PLUGINS}.dylib" ]
		then
			f_echo_me "found"
			${AVDIR}/agent --remove-plugin ${KNOWN_SMC_PLUGINS} > /dev/null 2>&1
		else
			f_echo_me "${KNOWN_SMC_PLUGINS} not found"
		fi
	done
}


f_remove_product_files()
{
	if [ -x "${PRODUCT_BIN}" ]
	then
		${PRODUCT_BIN} --stop > /dev/null 2>&1
	fi

# 	remove agent conf files
	for CONF_2_REMOVE in avupdate.conf.default avupdate.conf agent.ini.default agent.ini
	do
		f_echo_me "remove ${FULLPRODUCTNAME} config file ${CONF_2_REMOVE} ... "
		rm ${CONF_2_REMOVE} > /dev/null 2>&1
	done

# 	remove agent install dirl
	rm -r ${AVDIR_BASE}
}


# main()
# 	check which product is installed
	f_check_installed_products

# 	check if the user is root
	f_root_check

# 	check if the given product is a supported avira product and set some important variables
	f_set_product_variables

# 	check if there are product update cronjobs available from the old products
	f_remove_launchd_entries

# 	reactivate the guard launchd entries
	f_restore_launchd_entries

# 	remove agent log dir
	f_remove_agent_log_dir

# 	if the product is agent, we have to unregister agent on the server side
	f_unregister_agent

# 	if there is an agent installed, unregister the smc plugins
	f_unregister_smc_plugins

# 	remove the product files and directories
	f_remove_product_files

	f_echo_me " "
	f_echo_me "${FULLPRODUCTNAME} successfully uninstalled"
# AVCS4F3A4200C37O030000000203000002A4010000000000000000000000000303000000650300000200P00000000000000007B17A04217694C768C9BA3425A610E738A3CA046482EA4E1BB941B5C4CE9D7DB955D5CAFF49398DBD5B08DA4E02AFDFEC4CA9D868B0FBA2DFCB40FAF4A4B0A37A1767A8BCD94197484EBBDF6816AE997A803FA87FE8A3B6AD7672121B3AAEC38E761B08874E9ECBF492E04068595FC064409FDA06DC2813DAF1FEBFFC76F3EA522342F7D644792EE4389BC1B997A2DE25E3F8BE406D0B13B23F93116F95CAE8F568E0A4E3310E5F536E33F836030FB46A60A2FC9BF575F187831833B9E926A850C83FF0910A61D7A3014754DEDC0E25185FFB44F191CC3B00A10E7AB9DC381B4594A0CA476129FAA06B265843170090755097E5EB34F9F29DBF62FA6659058B900000000000000000000000000000292030000000003T62F3AB0132FAVCSE