#!/bin/sh

if [ `uname -s` = SunOS ]; then
	ECHO="/usr/ucb/echo"
else
	ECHO="echo"
fi

$ECHO
$ECHO "Welcome to esets_setup, the ESET Security interactive automated install script."
$ECHO
$ECHO "It will help you integrate ESET Security with your system easily and can later "
$ECHO "undo all changes. In case some files must be modified, they will be "
$ECHO "backed up in your home directory."
$ECHO

LIC=/opt/eset/esets/sbin/esets_lic
INST=/opt/eset/esets/sbin/esets_inst
DOCDIR=/opt/eset/esets/share/doc
for i in /usr /usr/local /usr/pkg /opt/esets; do
	if [ -x "$i/sbin/esets_daemon" ]; then
		LIC="$i/sbin/esets_lic"
		INST="$i/sbin/esets_inst"
		DOCDIR="$i/share/doc/esets"
		break
	fi
done

LICS=`$LIC --list-fmt '%pname%\n'`
echo "$LICS" | fgrep ESETKMS > /dev/null && MS=1
echo "$LICS" | fgrep ESETMS > /dev/null && MS=1
echo "$LICS" | fgrep ESETFS > /dev/null && FS=1
echo "$LICS" | fgrep ESETGS > /dev/null && GS=1
echo "$LICS" | fgrep ESETAB > /dev/null && FS=1
echo "$LICS" | fgrep ESETSSB > /dev/null && FS=1
echo "$LICS" | fgrep 'ESET File Security' > /dev/null && FS=1
echo "$LICS" | fgrep 'ESET Mail Security' > /dev/null && MS=1
echo "$LICS" | fgrep 'ESET Gateway Security' > /dev/null && GS=1

[ -z "$MS$FS$GS" ] && $ECHO "No license found."

if [ -n "$FS" ]; then
	$ECHO "There are no automated installations for File Servers, "
	$ECHO "see the guide in $DOCDIR for general instructions."
	$ECHO
fi

[ -n "$MS" ] && OPTS="MTA POP3 IMAP SMTP"
[ -n "$GS" ] && OPTS="$OPTS HTTP FTP ICAP"

[ -z "$OPTS" ] && exit

$ECHO -n "Do you want to see commands, which will be executed during installation? (y/n, default=y) "
read r
if [ "$?" = 0 ]; then
	case "$r" in
		[Nn]*)
			INSTARGS="--hide-commands";;
	esac
fi

inst ()
{
	$INST $INSTARGS $1
	status=$?
	[ "$status" != "0" ] && { $ECHO "Installation failed with code $status"; exit 1; }
}

# select is bash specific
sel ()
{
	varname="$1"
	eval $varname=""
	shift
	[ "$#" = "0" ] && return
	i=1
	for j in "$@"; do $ECHO "$i) $j"; i=`expr $i + 1`; done
	$ECHO -n "Your selection (1-`expr $i - 1`): "
	read REPLY
	test "$REPLY" -gt 0 -a "$REPLY" -lt "$i" 2>/dev/null || return
	eval $varname="\$$REPLY"
}

while [ "$s" != "quit" ]; do
	$ECHO "Available ESETS installations/uninstallations:"
	sel s $OPTS quit
	case "$s" in
		MTA)
			inst --mta;;
		HTTP)
			inst --http;;
		FTP)
			inst --ftp;;
		ICAP)
			inst --icap;;
		POP3)
			inst --pop3;;
		IMAP)
			inst --imap;;
		SMTP)
			inst --smtp;;
		quit)
			;;
		*)
			$ECHO "Unknown selection: $REPLY";;
	esac
done

