#!/bin/sh
#
# Install AntiVir for UNIX
#
# Copyright 2002-2003 H+BEDV Datentechnik GmbH
#

PATH=$PATH:/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin:/usr/local/sbin

check_copyfiles()
{
	for CPITEM in $CPLIST
	do
		if [ ! -f "$CPITEM" ]
		then
			echo ""
			echo "Not all the installation files were found. Make sure"
			echo "you have the complete installation package. This script"
			echo "requires the following files:"
			for CPITEM in $CPLIST
			do
				echo "    $CPITEM"
			done
			echo " "
			exit 1
		fi
	done
}

check_executables()
{
	for EXE in $EXELIST
	do
		if [ ! -x "$(which $EXE)" ]
		then
			echo ""
			echo "This script requires '$EXE' to run."
			echo " "
			exit 1
		fi
	done
}

convert_version()
{
	TEMP3="${VERSION#*[!0-9]}"
	MAJ=${VERSION%[!0-9]$TEMP3}
	if [ -z "$MAJ" ]
	then
		MAJ=0
	fi
	test $MAJ -eq 0
	if [ $? -gt 1 ]
	then
		MAJ=0
	fi
	TEMP2="${TEMP3#*[!0-9]}"
	MIN=${TEMP3%[!0-9]$TEMP2}
	if [ -z "$MIN" ]
	then
		MIN=0
	fi
	test $MIN -eq 0
	if [ $? -gt 1 ]
	then
		MIN=0
	fi
	TEMP1="${TEMP2#*[!0-9]}"
	REV=${TEMP2%[!0-9]$TEMP1}
	if [ -z "$REV" ]
	then
		REV=0
	fi
	test $REV -eq 0
	if [ $? -gt 1 ]
	then
		REV=0
	fi
	BLD="${TEMP1%%[!0-9]*}"
	if [ -z "$BLD" ]
	then
		BLD=0
	fi
	test $BLD -eq 0
	if [ $? -gt 1 ]
	then
		BLD=0
	fi
	VERSION=$((($MAJ * 1000000) + ($MIN * 10000) + ($REV * 100) + $BLD))
}

compare_versions()
{
	# 0 = replace older version
	# 1 = do not replace same version
	# 2 = do not replace newer version
	# 3 = TOO new version (do not install)

	if [ $OLDVERSION -lt $NEWVERSION ]
	then
		return 0
	elif [ $OLDVERSION -eq $NEWVERSION ]
	then
		return 1
	elif [ $OLDVERSION -gt $NEWVERSION ]
	then
		if [ $(($OLDVERSION / 10000)) -gt $(($NEWVERSION / 10000)) ]
		then
			return 3
		fi

		return 2
	fi

	return 0
}

identify_rcd()
{
	RCDIR="/etc"
	if [ ! -d "$RCDIR/rc0.d" ]
	then
		RCDIR="/etc/rc.d"
		if [ ! -d "$RCDIR/rc0.d" ]
		then
			RCDIR=""
			return
		fi
	fi
}

make_startup_links()
{
	sleep 1
	echo -n "linking $RCDIR/rc(LEVEL).d/(S/K)20$LINKNAME to $AVDIR/$LINKNAME ..."
	sleep 1
	echo " "
	for LEVEL in 0 1 2 3 4 5 6
	do
		echo -n "runlevel $LEVEL ... "
		sleep 1
		ln -sf $AVDIR/$LINKNAME $RCDIR/rc$LEVEL.d/S20$LINKNAME
		ln -sf $AVDIR/$LINKNAME $RCDIR/rc$LEVEL.d/K20$LINKNAME
		echo "done"
	done
}

remove_startup_links()
{
	for LEVEL in 0 1 2 3 4 5 6
	do
		rm -f $RCDIR/rc$LEVEL.d/S20$LINKNAME
		rm -f $RCDIR/rc$LEVEL.d/K20$LINKNAME
	done
}

install_guard()
{
	COPYGUARDCONF=1
	sleep 1
	echo -n "checking for existing /etc/$GUARDNAME.conf ... "
	sleep 2
	if [ -f "/etc/$GUARDNAME.conf" ]
	then
		COPYGUARDCONF=0
		echo "found"
	else
		echo "not found"
	fi

	if [ $COPYGUARDCONF -eq 1 ]
	then
		sleep 1
		echo -n "copying conf/$GUARDNAME.conf to /etc ... "
		sleep 2
		cp -f conf/$GUARDNAME.conf /etc
		if [ $? -ne 0 ]
		then
			echo "failed"
			echo ""
			exit 1
		fi
		chmod 644 /etc/$GUARDNAME.conf
		echo "done"
	fi

	sleep 1
	echo -n "copying sh/$GUARDNAME to $AVDIR ... "
	sleep 2
	cp -f sh/$GUARDNAME $AVDIR
	if [ $? -ne 0 ]
	then
		echo "failed"
		echo ""
		exit 1
	fi
	chmod 755 $AVDIR/$GUARDNAME
	echo "done"

	sleep 1
	echo -n "linking $CONFIGGUARDNAME to $CONFIGPROGNAME ... "
	sleep 2
	ln -sf $AVDIR/$CONFIGPROGNAME $AVDIR/$CONFIGGUARDNAME
	echo "done"

	sleep 1
	echo " "
	echo -n "Would you like AvGuard to start automatically? [y] "
	read INPUT

	STARTAVGUARD=1
	case $INPUT in
		n*|N*)
			STARTAVGUARD=0
			;;
	esac

	if [ -z "$RCDIR" ]
	then
		identify_rcd
	fi
	if [ $STARTAVGUARD -eq 1 ]
	then
		sleep 1
		echo -n "identifying startup script location ... "
		sleep 2
		if [ -z "$RCDIR" ]
		then
			echo "failed"
			echo ""
			echo " "
			echo "************************************************"
			echo " Unable to locate where startup scripts belong."
			echo " You can launch AvGuard with:"
			echo " "
			echo "      $AVDIR/$GUARDNAME start"
			echo "************************************************"
			echo " "
			echo -n "Press <ENTER> to continue."
			read INPUT
		else
			echo "found ($RCDIR/)"
			LINKNAME="$GUARDNAME"
			make_startup_links
		fi
	else
		if [ ! -z "$RCDIR" ]
		then
			LINKNAME="$GUARDNAME"
			remove_startup_links
		fi
	fi

	sleep 1
	echo "installation of AvGuard scanner complete"
}
	

# main()

AVDIR="/usr/lib/AntiVir"
PROGNAME="antivir"
VDFNAME="antivir.vdf"
UPDATENAME="avupdater"
GUARDNAME="avguard"
CONFIGGUARDNAME="configavguard"
CONFIGPROGNAME="configantivir"
PRODUCTVERSION="2.0.7"
STEP=1
SERVER=1
FILECACHE=1
EXELIST="which id sleep head cat grep cp chmod ln uname ps"
CPLIST="bin/$PROGNAME vdf/$VDFNAME conf/$PROGNAME.conf conf/$GUARDNAME.conf sh/$UPDATENAME sh/$GUARDNAME sh/$CONFIGPROGNAME"

if [ $SERVER -eq 1 ]
then
	PRODUCTTYPE="Server"
else
	PRODUCTTYPE="Workstation"
fi

if [ $FILECACHE -eq 1 ]
then
	CPLIST="$CPLIST bin/$PROGNAME-fc"
fi

check_copyfiles
check_executables

if [ $(id -u) -ne 0 ]
then
	echo ""
	echo "This script must be run as root."
	echo " "
	exit 1
fi

KERNELVERSION="linux-$(uname -r)"
if [ ! -z "$(uname -a | grep SMP)" ]
then
	KERNELVERSION="$KERNELVERSION-smp"
fi

if [ ! -z "$(ps ax | grep antivir.*[-]-workstation)" -o ! -z "$(ps ax | grep antivir.*[-]-server)" ]
then
	echo ""
	echo "A version of AvGuard appears to be running."
	echo " "
	echo "Please shut down your existing AvGuard before installing"
	echo "this version. You can stop the running version with:"
	echo " "
	echo "     $AVDIR/$GUARDNAME stop"
	echo " "
	exit 1
fi

echo " "

echo "Starting AntiVir for UNIX $PRODUCTTYPE $PRODUCTVERSION installation..."

echo " "

if [ -d "$AVDIR" ]
then
	sleep 1
	echo "previous installation of AntiVir found"
	echo "(updating necessary components)"
	sleep 2
	echo " "
fi

sleep 1

echo "$STEP) installing command line scanner"
if [ ! -d "$AVDIR" ]
then
	sleep 1
	echo -n "creating install directory $AVDIR ... "
	mkdir -p $AVDIR
	chmod 755 $AVDIR
	sleep 2
	echo "done"
fi

NEWLINE=0
for KEY in $(ls | grep -i \.key)
do
	sleep 1
	echo -n "copying $KEY to $AVDIR ... "
	sleep 2
	if [ $NEWLINE -eq 0 -a -f "$AVDIR/$KEY" ]
	then
		echo " "
		NEWLINE=1
	fi
	cp -ip $KEY $AVDIR
	if [ $NEWLINE -eq 0 ]
	then
		echo "done"
	fi
done

COPYEXE=1
if [ -x "$AVDIR/$PROGNAME" ]
then
	sleep 1
	echo -n "checking executable file ... "
	sleep 2
	VERSION="$($AVDIR/$PROGNAME --version | grep "product version:")"
	VERSION=${VERSION#product version:}
	convert_version
	OLDVERSION=$VERSION
	VERSION="$(bin/$PROGNAME --version | grep "product version:")"
	VERSION=${VERSION#product version:}
	convert_version
	NEWVERSION=$VERSION
	compare_versions
	RESULT1=$?
	VERSION="$($AVDIR/$PROGNAME --version | head -n 1)"
	VERSION=${VERSION#product version:}
	convert_version
	OLDVERSION=$VERSION
	VERSION="$(bin/$PROGNAME --version | head -n 1)"
	VERSION=${VERSION#product version:}
	convert_version
	NEWVERSION=$VERSION
	compare_versions
	RESULT2=$?
	if [ $RESULT2 -lt $RESULT1 ]
	then
		RESULT1=$RESULT2
	fi
	case "$RESULT1" in
		0)
			echo "old version"
			;;
		1)
			COPYEXE=0
			echo "compatible"
			;;
		2)
			COPYEXE=0
			echo "compatible"
			;;
		3)
			echo "incompatible"
			echo ""
			echo "Your $PROGNAME program is too new for this version"
			echo "of AntiVir for UNIX $PRODUCTTYPE."
			echo "Please download the latest version from:"
			echo " "
			echo "http://www.hbedv.com   <- english"
			echo "http://www.antivir.de  <- german"
			echo " "
			exit 1
			;;
		*)
			echo "error (overwriting)"
			;;
	esac
fi

COPYVDF=1
if [ -f "$AVDIR/$VDFNAME" ]
then
	sleep 1
	echo -n "checking data file ... "
	sleep 2
	VERSION="$(head -n4 $AVDIR/$VDFNAME | grep Version)"
	VERSION="${VERSION#Version: }"
	convert_version
	OLDVERSION=$VERSION
	VERSION="$(head -n4 vdf/$VDFNAME | grep Version)"
	VERSION="${VERSION#Version: }"
	convert_version
	NEWVERSION=$VERSION
	compare_versions
	case "$?" in
		0)
			echo "old version"
			;;
		1)
			COPYVDF=0
			echo "compatible"
			;;
		2)
			COPYVDF=0
			echo "compatible"
			;;
		3)
			echo "incompatible"
			echo ""
			echo "Your data file is too new for this version"
			echo "of AntiVir for UNIX $PRODUCTTYPE."
			echo "Please download the latest version from:"
			echo " "
			echo "http://www.hbedv.com   <- english"
			echo "http://www.antivir.de  <- german"
			echo " "
			exit 1
			;;
		*)
			echo "error (overwriting)"
			;;
	esac
fi

COPYCONF=1
sleep 1
echo -n "checking for existing /etc/$PROGNAME.conf ... "
sleep 2
if [ -f "/etc/$PROGNAME.conf" ]
then
	COPYCONF=0
	echo "found"
else
	echo "not found"
fi

if [ $COPYEXE -eq 1 ]
then
	sleep 1
	echo -n "copying bin/$PROGNAME to $AVDIR ... "
	sleep 2
	cp -f bin/$PROGNAME $AVDIR
	if [ $? -ne 0 ]
	then
		echo "failed"
		echo ""
		exit 1
	fi
	chmod 755 $AVDIR/$PROGNAME
	echo "done"
fi

if [ $FILECACHE -eq 1 ]
then
	sleep 1
	echo -n "copying bin/$PROGNAME-fc to $AVDIR ... "
	sleep 2
	cp -f bin/$PROGNAME-fc $AVDIR
	if [ $? -ne 0 ]
	then
		echo "failed"
		echo ""
		exit 1
	fi
	chmod 755 $AVDIR/$PROGNAME-fc
	echo "done"
fi

if [ $COPYVDF -eq 1 ]
then
	sleep 1
	echo -n "copying vdf/$VDFNAME to $AVDIR ... "
	sleep 2
	cp -f vdf/$VDFNAME $AVDIR
	if [ $? -ne 0 ]
	then
		echo "failed"
		echo ""
		exit 1
	fi
	chmod 644 $AVDIR/$VDFNAME
	echo "done"
fi

if [ $COPYCONF -eq 1 ]
then
	sleep 1
	echo -n "copying conf/$PROGNAME.conf to /etc ... "
	sleep 2
	cp -f conf/$PROGNAME.conf /etc
	if [ $? -ne 0 ]
	then
		echo "failed"
		echo ""
		exit 1
	fi
	chmod 644 /etc/$PROGNAME.conf
	echo "done"
fi

sleep 1
echo -n "copying sh/$CONFIGPROGNAME to $AVDIR ... "
sleep 2
cp -f sh/$CONFIGPROGNAME $AVDIR
if [ $? -ne 0 ]
then
	echo "failed"
	echo ""
	exit 1
fi
chmod 755 $AVDIR/$CONFIGPROGNAME
echo "done"

if [ ! -L "/usr/bin/$PROGNAME" ]
then
	sleep 1
	echo " "
	echo -n "Would you like to create a link in /usr/bin ? [y] "
	read INPUT

	CREATELINK=1
	case $INPUT in
		n*|N*)
			CREATELINK=0
			;;
	esac

	if [ $CREATELINK -eq 1 ]
	then
		sleep 1
		echo -n "linking /usr/bin/$PROGNAME to $AVDIR/$PROGNAME ... "
		sleep 2
		ln -sf $AVDIR/$PROGNAME /usr/bin/$PROGNAME
		echo "done"
	fi
fi

sleep 1
echo "installation of command line scanner complete"

STEP=$(($STEP + 1))
echo " "
sleep 1
echo " "
sleep 1
echo "$STEP) installing automatic internet updater"
sleep 1
echo "An automatic internet updater is available with version $PRODUCTVERSION of"
echo "AntiVir for UNIX. This is a daemon that will run in the background"
echo "and automatically check for updates (internet access is required)."
echo "You may also manually check for updates using:"
echo " "
echo "     antivir --update"
echo " "
echo "You do not need to install the automatic internet updater in order"
echo "to manually check for updates. Please read the README file for more"
echo "information on updates and how they can best suit you."
echo " "
if [ -f "$AVDIR/$UPDATENAME" ]
then
	REINSTALL="re-"
else
	REINSTALL=""
fi
echo -n "Would you like to ${REINSTALL}install the automatic internet updater? [n] "
read INPUT

INSTALLUPDATER=0
case $INPUT in
	y*|Y*|j*|J*)
		INSTALLUPDATER=1
		;;
esac

RCDIR=""
if [ $INSTALLUPDATER -eq 1 ]
then
	sleep 1
	echo -n "copying sh/$UPDATENAME to $AVDIR ... "
	sleep 2
	cp -f sh/$UPDATENAME $AVDIR
	if [ $? -ne 0 ]
	then
		echo "failed"
		echo ""
		exit 1
	fi
	chmod 755 $AVDIR/$UPDATENAME
	echo "done"

	sleep 1
	echo " "
	echo -n "Would you like the automatic updater to start automatically? [y] "
	read INPUT

	STARTUPDATER=1
	case $INPUT in
		n*|N*)
			STARTUPDATER=0
			;;
	esac

	identify_rcd
	if [ $STARTUPDATER -eq 1 ]
	then
		sleep 1
		echo -n "identifying startup script location ... "
		sleep 2
		if [ -z "$RCDIR" ]
		then
			echo "failed"
			echo ""
			echo " "
			echo "*****************************************************"
			echo " Unable to locate where startup scripts belong."
			echo " You can launch the automatic internet updater with:"
			echo " "
			echo "      $AVDIR/$UPDATENAME start"
			echo "*****************************************************"
			echo " "
			echo -n "Press <ENTER> to continue."
			read INPUT
		else
			echo "found ($RCDIR/)"
			LINKNAME="$UPDATENAME"
			make_startup_links
		fi

	else
		if [ ! -z "$RCDIR" ]
		then
			LINKNAME="$UPDATENAME"
			remove_startup_links
		fi
	fi

	sleep 1
	echo "${REINSTALL}installation of automatic internet updater complete"
else
	echo "automatic internet updater will NOT be ${REINSTALL}installed"
fi

STEP=$(($STEP + 1))
echo " "
sleep 1
echo " "
sleep 1
echo "$STEP) installing AvGuard"
sleep 1
echo "Version $PRODUCTVERSION of AntiVir for UNIX is capable of on-access,"
echo "real-time scanning of files. This provides the ultimate protection"
echo "against viruses and other unwanted software. The on-access scanner"
echo "(called AvGuard) is based on Dazuko, a free software project providing"
echo "access control. In order to use AvGuard you will need to compile Dazuko"
echo "for your kernel. Please refer to src/HOWTO-Dazuko for information on how"
echo "to do this. There are several ways in which you can install AvGuard."
echo " "
echo "        module     - Dazuko has been compiled as a kernel module"
echo " "
echo "        kernel     - Dazuko has been compiled into the Linux kernel"
echo " "
echo "        no install - do not install AvGuard at this time"
echo " "
echo "Note: Dazuko currently only works with GNU/Linux systems. If you are"
echo "      interested in helping us port Dazuko to FreeBSD and OpenBSD,"
echo "      feel free to check out the Dazuko Project at: http://dazuko.org"
echo " "
echo "available options: m k n"

OK=0
while [ $OK -eq 0 ]
do
	if [ -z "$(cat /proc/modules | grep dazuko)" -a ! -z "$(cat /proc/devices | grep dazuko)" ]
	then
		QDEFAULT="k"
	elif [ -f "$AVDIR/$KERNELVERSION/dazuko.o" ]
	then
		QDEFAULT="m"
	else
		QDEFAULT="n"
	fi

	echo " "
	echo -n "How should AvGuard be installed? [$QDEFAULT] "
	read INPUT

	if [ -z "$INPUT" ]
	then
		INPUT="$QDEFAULT"
	fi

	case $INPUT in
		m*|M*)
			if [ -f "$AVDIR/$KERNELVERSION/dazuko.o" ]
			then
				QDEFAULT=" [$AVDIR/$KERNELVERSION/dazuko.o]"
			else
				QDEFAULT=""
			fi
			echo -n "Enter the full path to dazuko.o${QDEFAULT}: "
			read INPUT

			if [ -z "$INPUT" -a ! -z "$QDEFAULT" ]
			then
				INPUT="$AVDIR/$KERNELVERSION/dazuko.o"
			fi

			if [ -f "$INPUT" ]
			then
				echo " "

				EXELIST="$EXELIST insmod rmmod"
				check_executables

				OK=1

				sleep 1
				if [ ! -z "$(cat /proc/modules | grep dazuko)" ]
				then
					echo ""
					echo "Dazuko has been detected (already running). Please"
					echo "remove the running Dazuko module before running"
					echo "this install program."
					echo " "
					echo "rmmod dazuko"
					OK=0
				fi

				if [ ! -z "$(cat /proc/devices | grep dazuko)" ]
				then
					echo ""
					echo "Dazuko has been detected as built-in to your"
					echo "kernel. There is no need for Dazuko as a module."
					OK=0
				fi

				if [ $OK -eq 1 ]
				then
					echo -n "testing $INPUT ... "
					sleep 2
					insmod -p $INPUT
					RET=$?
					if [ $RET -eq 0 ]
					then
						insmod $INPUT
						RET=$?
						if [ $RET -eq 0 ]
						then
							rmmod dazuko
						fi
					fi

					if [ $RET -ne 0 ]
					then
						echo "failed"
						echo ""
						echo "This Dazuko module does not appear to work properly with"
						echo "the loaded kernel. Please make sure that you compile"
						echo "Dazuko using the SAME source code as your Linux kernel."
						OK=0
					else
						echo "ok"
					fi
				fi

				if [ $OK -eq 1 ]
				then
					if [ "$INPUT" != "$AVDIR/$KERNELVERSION/dazuko.o" ]
					then
						sleep 1
						echo -n "detecting kernel version ... "
						sleep 2
						echo "$KERNELVERSION"

						sleep 1
						echo -n "copying $INPUT to $AVDIR/$KERNELVERSION ... "
						sleep 2
						if [ ! -d "$AVDIR/$KERNELVERSION" ]
						then
							mkdir -p $AVDIR/$KERNELVERSION
							chmod 755 $AVDIR/$KERNELVERSION
						fi
						cp -f $INPUT $AVDIR/$KERNELVERSION
						if [ $? -ne 0 ]
						then
							echo "failed"
							echo ""
							OK=0
							exit 1
						fi
						chmod 644 $AVDIR/$KERNELVERSION/dazuko.o
						echo "done"
					fi
				fi
			else
				echo "$INPUT not found."
			fi

			if [ $OK -eq 1 ]
			then
				install_guard
				GUARDINSTALLEDAS="module"
			fi
			;;
		k*|K*)
			OK=1

			if [ -z "$(cat /proc/devices | grep dazuko)" ]
			then
				echo ""
				echo "Dazuko has not been detected as built-in to this"
				echo "running kernel."
				OK=0
			fi

			if [ $OK -eq 1 ]
			then
				install_guard
				GUARDINSTALLEDAS="kernel"
			fi
			;;
		n*|N*)
			echo "AvGuard will NOT be installed. See src/HOWTO-Dazuko for more"
			echo "information about Dazuko."
			sleep 1
			GUARDINSTALLEDAS=""
			OK=1
			;;
	esac
done

STEP=$(($STEP + 1))
echo " "
sleep 1
echo " "
sleep 1
echo "$STEP) configuring AntiVir"
sleep 1
echo -n "Would you like to configure AntiVir now? [y] "
read INPUT

CONFIGPROG=1
case $INPUT in
	n*|N*)
		CONFIGPROG=0
		;;
esac

if [ $CONFIGPROG -eq 1 ]
then
	if [ -f "$AVDIR/$CONFIGGUARDNAME" ]
	then
		$AVDIR/$CONFIGGUARDNAME
	else
		$AVDIR/$CONFIGPROGNAME
	fi
	echo -n "Press <ENTER> to continue."
	read INPUT
else
	if [ -f "$AVDIR/$CONFIGGUARDNAME" ]
	then
		$AVDIR/$CONFIGGUARDNAME --check-conf
		echo -n "Press <ENTER> to continue."
		read INPUT
	fi
fi

echo " "
echo " "
echo "Installation of the following features complete:"
sleep 1
echo -n "     AntiVir command line scanner"
if [ $COPYEXE -eq 0 ]
then
	echo -n " (previously installed)"
fi
echo " "
if [ -f "$AVDIR/$UPDATENAME" ]
then
	sleep 1
	echo -n "     AntiVir Automatic Internet Updater"
	if [ $INSTALLUPDATER -eq 0 ]
	then
		echo -n " (previously installed)"
	fi
	echo " "
fi
if [ -f "$AVDIR/$KERNELVERSION/dazuko.o" -a -f "/etc/$GUARDNAME.conf" ]
then
	sleep 1
	echo -n  "     AntiVir Guard"
	if [ -z "$GUARDINSTALLEDAS" ]
	then
		echo -n " (previously installed)"
	fi
	echo " "
fi

sleep 1
echo " "
sleep 1
if [ -z "$(ls $AVDIR | grep \.key)" ]
then
	echo " "
	echo "If you have any license key files, please copy them to $AVDIR"
	echo "before running the software. Without a valid license key, it will"
	echo "run in DEMO mode."
fi
echo " "
echo "Be sure to read the README file for additional information."
echo "Thank you for your interest in AntiVir for UNIX."
echo " "
