#!/bin/bash
#
# $Header: /var/cvsroot/gentoo/src/installer/bin/installer,v 1.6 2006/07/31 17:01:53 wolf31o2 Exp $
#
# This is the installer script that we will use to determine whether or not
# we are running in X or as root.  A good portion of this script was ripped
# from the loki_setup setup.sh script, as it already did most of what I was
# looking to do.

installer_root=/opt/installer

# Allow X
xhost +localhost 2>/dev/null

# Go to the proper setup directory
cd ${installer_root}/bin

# we call installer with -auth when ran through su/xsu/sudo
auth=0
if [ "${1}" == "-auth" ]
then
	auth=1
	shift
fi

# we see if we were called with a specific front-end
launch=
if [ "${1}" == "gtk" ]
then
	launch=gtk
	shift
elif [ "${1}" == "dialog" ]
then
	launch=dialog
	shift
fi

# Find the installation program
# try_run INSTALLER_NAME [PARAMETERS_PASSED]
#   INSTALLER_NAME: installer-gtk or installer-dialog
#   PARAMETERS_PASSED: additional arguments passed to the setup script
try_run() {
	setup=${1}
	shift

	failed=0
	"${setup}" "${@}" 2>/dev/null
	failed="${?}"
	return "${failed}"
}

if [ "${auth}" -eq 0 ]
then
	GOT_ROOT=$(id -u)
	if [ "${GOT_ROOT}" != "0" ]
	then
		# first we try sudo
		try_run /usr/bin/sudo env DISPLAY=":0.0" ${installer_root}/bin/installer -auth ${launch} "$@"
		status="${?}"
		if [ "${status}" -eq 0 ]
		then
			exit 0
		else
			try_run /usr/bin/gnomesu -u root -c "sh ${installer_root}/bin/installer -auth ${launch}"
			status="$?"
			# If try_run successfully executed gnomesu, it will return
			# gnomesu's exit code.
			if [ "${status}" -eq 0 ]
			then
				exit 0
			else
				try_run /usr/bin/xsu -e -a -u root -c "sh ${installer_root}/bin/installer -auth ${launch}"
				status="${?}"
				# xsu returns 2 if ran and cancelled (i.e. the user 'doesn't
				# want' to auth).  it will return 0 if the command was executed
				# correctly
				# summing up, if we get 1, something failed
				if [ "${status}" -eq 0 ]
				then
					exit 0
				else
					# xsu wasn't found, or failed to run
					# if xsu actually ran and the auth was cancelled,
					# $status is 2
					echo "You need to run this installation as the super user."
					exit 1
#					echo "Please enter the root password."
#					try_run /bin/su root -c "export DISPLAY=${DISPLAY};sh ${installer_root}/bin/installer -auth ${launch}"
#					status="${?}"
#					if [ "${status}" -eq 0 ]
#					then
#						# the auth command was properly executed
#						exit 0
#					else
#						echo "Please run this installation as the super user"
#						exit 1
#					fi
				fi
			fi
		fi
	fi
fi

if [ -z "${launch}" ]
then
	# Try to run the gtk installer
	try_run /usr/bin/installer-gtk ${*}
	status=${?}
	if [ ${status} -ge 1 ]
	then
		try_run /usr/bin/installer-dialog ${*} || {
			if [ ${status} -ne 2 ]
			then
				echo "The setup program seems to have failed"
			fi
			status=1
		}
	fi
else
	# Try to run the installer script asked for when launched
	try_run /usr/bin/installer-${launch} ${*} || {
		echo "The setup program seems to have failed"
		status=1
	}
fi

exit ${status}
