#!/bin/sh

if [ -e /etc/italc/keys/public/ ]; then

	ITALC_STUDENT_GROUP=`stat -c %G /etc/italc/keys/public/`

	MSG="
iTALC Client Applet

You are not allowed to launch iTALC's client applet.

Ask your system administrator to add your account to
POSIX group \"$ITALC_STUDENT_GROUP\".

Exiting...
"

else
	MSG="
iTALC Client Applet

The iTALC setup has not been complete, yet. Read

  /usr/share/doc/italc-client/README.setup.gz

or execute

  sudo dpkg-reconfigure italc-client

Exiting...
"
fi

# if -autostart is given as first cmdline option, we
# will not display any GUI error messages...
if [ "x$1" = "x-autostart" ]; then
	shift
	ITALC_AUTOSTART="true"
fi

if [ -x /etc/italc/keys/public ]; then
	if [ "x$ITALC_AUTOSTART" = "xtrue" ]; then
		if [ -r /etc/italc/keys/private/admin/key ]; then
			/usr/lib/italc/ica "$@" -role admin &
		elif [ -r /etc/italc/keys/private/supporter/key ]; then
			/usr/lib/italc/ica "$@" -role supporter &
		elif [ -r /etc/italc/keys/private/teacher/key ]; then
			/usr/lib/italc/ica "$@" -role teacher &
		else
			/usr/lib/italc/ica "$@" -role other &
		fi
	else
		/usr/lib/italc/ica "$@" &
	fi
else

	if [ -z "$DISPLAY" ]; then
		# No Display set...
		echo
		echo "iTALC Client Applet"
		echo "-------------------"
		echo "No \$DISPLAY environment variable set. Cannot launch iTALC Client Applet."
		echo
		echo "Exiting..."
		echo
	elif type kdialog 1>/dev/null && [ "x$ITALC_AUTOSTART" != "xtrue" ]; then
		kdialog --title "iTALC Client" --error "$MSG"
	elif type zenity 1>/dev/null && [ "x$ITALC_AUTOSTART" != "xtrue" ]; then
		zenity --title "iTALC Client" --error --text "$MSG"
	else
		echo
		echo "$MSG"
		echo
	fi

fi
