#!/bin/sh
#
# Start AntiVir GUI
#
# Copyright 2007 Avira GmbH
#

#list of known directories
DIR="/usr/local /usr/lib /usr/java /opt /usr /usr/lib/jvm"

#list of known keywords
KEYS="java j2 sun blackdown"

ARGCOUNT=$#

AVDIR="/usr/lib/AntiVir/gui"
ARGS=""
JAVABIN=`which java 2>/dev/null`
VERSION=""
OS=`uname | tr "[:upper:]" "[:lower:]"`

###################################################################
# Check for XServer access right                                  #
#                                                                 #
# If no XServer access right present, exit                        #
###################################################################
checkXServer()
{
	if [ -z "$DISPLAY" ]
	then
		echo ""
		echo "ERROR: Can't connect to an X server. Please try the following:"
		echo ""
		echo "- set the \`DISPLAY' variable with:"
		echo "  $ export DISPLAY=:0.0"
		echo "      or"
		echo "  $ setenv DISPLAY :0.0"
		echo ""

		exit 1
	fi

	if [ ! -f "$HOME/.Xauthority" -a "$OS" != "sunos" ]
	then
		echo ""
		echo "ERROR: Can't connect to an X server. Please try the following:"
		echo ""
		echo "- generate or merge \`.Xauthority'. You can merge with:"
		echo "  $ xauth merge <path-to-user-with-X-rights>/.Xauthority"
		echo ""

		exit 1
	fi
}

###################################################################
# start antivir-gui                                               #
###################################################################
main()
{
	if [ $ARGCOUNT -eq 0 -a "${OS}" != "darwin" ]
	then
		checkXServer
	fi

	JARS="$HOME/.AntiVir:."

	for i in `ls lib/*.jar`
	do
		JARS="${JARS}:${i}"
	done

	for i in `ls lib/ext/*.jar`
	do
		JARS="${JARS}:${i}"
	done

	if [ ! -z "$HOME" ]
	then
		mkdir -p "$HOME/.AntiVir"
	fi

	exec $JAVABIN -classpath ${JARS} de.antivir.framework.Main ${ARGS}

	exit 0
}

###################################################################
# Check for Java Version = 1.4, 1.5, 1.6                          #
#                                                                 #
# param $1              the binary to probe                       #
# sets  $VERSION        if the version if Java is 1.4             #
###################################################################
checkVersion()
{
	VERSION=`$1 -version 2>&1 | grep -i version | cut -d\" -f2 | grep 1\.6`

	if [ -z "$VERSION" ]
	then
		VERSION=`$1 -version 2>&1 | grep -i version | cut -d\" -f2 | grep 1\.5`

		if [ -z "$VERSION" ]
		then
			VERSION=`$1 -version 2>&1 | grep -i version | cut -d\" -f2 | grep 1\.4`
		fi
	fi

	if [ ! -z "$VERSION" ]
	then
		GIJCHECK=`$1 -version 2>&1 | grep gij | grep libgcj`
		if [ ! -z "$GIJCHECK" ]
		then
			# GNU gij is not supported
			VERSION=""
		fi
	fi

	if [ ! -z "$VERSION" ]
	then
		main
	fi
}

###################################################################
# Search the Java Binary in the given directory                   #
#                                                                 #
# param $1              the directory to search for the binary    #
# sets  $JAVABIN        the directory where Java is installed     #	
###################################################################
searchBinary()
{
	#check if directory + key exists (and is directory)
	if [ -d "$1" ]
	then
		#check if executable exists
		if [ -x "$1/bin/java" ]
		then
			JAVABIN="$1/bin/java"

		elif [ -x "$1/jre/bin/java" ]
		then
			JAVABIN="$1/jre/bin/java"
		fi
		
		checkVersion $JAVABIN		
	fi
}

###################################################################
# Main                                                            #
###################################################################

FORCE=0

#collect commandline options
for arg in "$@"
do
	case "$arg" in
		*)
			ARGS="${ARGS} $arg"
			;;
	esac
done

CURUSER=`id | sed -e s/\).*// -e s/.*\(//`
if [ "$OS" = "sunos" ]
then
	FULLID=`id -a`
else
	FULLID=`id`
fi
if [ -z "`echo ${FULLID} | grep antivir`" ]
then
	if [ -r "${AVDIR}/lib/de_antivir_framework.jar" ]
	then
		echo ""
		echo "WARNING: ${CURUSER} is not in \`antivir' group"
	else
		echo ""
		echo "ERROR: ${CURUSER} is not in the \`antivir' group"
		echo ""
		echo "To add ${CURUSER} to the \`antivir' group:"
		GROUPLIST=`id -a | head -n 1 | sed -e s/.*=// -e s/\)//g -e s/[0-9]*\(//g`
		echo "# /usr/sbin/usermod -G $GROUPLIST,antivir ${CURUSER}"

		echo ""
		echo "Note: ${CURUSER} must log in again for this"
		echo "      change to take effect."

		echo ""
		exit 1
	fi
fi

#change to AntiVir directory
cd "${AVDIR}"

#check version of JAVABIN if found
if [ ! -z "$JAVABIN" ]
then
	checkVersion $JAVABIN
fi

#check if variable is set
if [ ! -z "$JAVA_HOME" ]
then
	searchBinary "$JAVA_HOME"
fi

#search for binary if variable is not set
if [ -z "$VERSION" ]
then
	#search all known directories
	for dir in $DIR
	do
		#check if directory exists (and is directory)
		if [ -d "$dir" ]
		then
			#search for all known keys in directory
			for key in $KEYS
			do
				for match in `ls $dir | grep $key`
				do
					if [ -d "$dir/$match" ]
					then
						searchBinary "$dir/$match"
					fi
				done
			done		
		fi
	done
fi

echo ""

if [ ! -z "$JAVABIN" ]
then
	echo "ERROR: Java found on this system ($JAVABIN),"
	echo "but it is an incompatible version."
else
	echo "ERROR: No Java found on this system."
fi

echo ""
echo "Sun Java 1.4.x or higher is needed."
echo ""

exit 1
